name: CI
on:
push:
branches: [main]
env:
CARGO_TERM_COLOR: always
permissions:
contents: write
jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Cache cargo registry
uses: actions/cache@v4
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v4
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v4
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
- name: Formatting
run: cargo fmt --all -- --check
- name: Clippy
run: cargo clippy --workspace --all-targets --all-features -- -D warnings
- name: Tests
run: cargo test --workspace --all-features
- name: Examples
run: cargo build --examples
- name: Documentation
run: cargo doc --workspace --no-deps --all-features
env:
RUSTDOCFLAGS: -D warnings
- name: Package
run: cargo package --allow-dirty
publish:
name: Publish
runs-on: ubuntu-latest
needs: [check]
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Configure git user
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Extract version
id: get_version
run: |
CARGO_VERSION=$(grep -m1 '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/' | tr -d '\n')
echo "version=$CARGO_VERSION" >> $GITHUB_OUTPUT
echo "Crate version: $CARGO_VERSION"
- name: Check if already published
id: check_published
run: |
CRATE_NAME=$(grep -m1 '^name = ' Cargo.toml | sed -E 's/name = "([^"]+)"/\1/' | tr -d '\n')
VERSION="${{ steps.get_version.outputs.version }}"
HTTP_CODE=$(
curl -sSL -o /dev/null -w "%{http_code}" \
-H "Accept: application/json" \
-H "User-Agent: pingid-ci (+https://github.com/${{ github.repository }})" \
"https://crates.io/api/v1/crates/${CRATE_NAME}/${VERSION}"
)
echo "HTTP response code: ${HTTP_CODE}"
if [ "$HTTP_CODE" = "200" ]; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "Version ${VERSION} for ${CRATE_NAME} already exists on crates.io, skipping publish"
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "Version ${VERSION} for ${CRATE_NAME} does not exist on crates.io, will publish"
fi
- name: Publish
if: steps.check_published.outputs.exists != 'true'
run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}
- name: Check if release already exists
id: check_release
run: |
TAG_NAME="v${{ steps.get_version.outputs.version }}"
if curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/tags/${TAG_NAME}" \
| grep -q '"id"'; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "GitHub release for ${TAG_NAME} already exists, skipping release creation"
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "GitHub release for ${TAG_NAME} does not exist, will create"
fi
- name: Generate release notes
id: generate_release_notes
run: |
notes_file="./RELEASE_NOTES.md"
commit_base="https://github.com/${{ github.repository }}/commit"
# Try to get last tag; fall back to first commit if none exist
if ! last_tag=$(git describe --tags --abbrev=0 2>/dev/null); then
last_tag=$(git rev-list --max-parents=0 HEAD) # first commit
fi
echo "Changes (since tag ${last_tag}):" >> ${notes_file}
git log "${last_tag}..HEAD" --reverse --pretty=format:'- [%h]('"${commit_base}"'/%h): %s' >> ${notes_file}
echo "notes_file=${notes_file}" >> $GITHUB_OUTPUT;
cat ${notes_file}
- name: Create GitHub Release
if: steps.check_release.outputs.exists != 'true'
uses: softprops/action-gh-release@v1
with:
name: Release ${{ steps.get_version.outputs.version }}
draft: false
body_path: ${{ steps.generate_release_notes.outputs.notes_file }}
tag_name: v${{ steps.get_version.outputs.version }}
target_commitish: ${{ github.sha }}
prerelease: ${{ contains(steps.get_version.outputs.version, '-') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}