Skip to content

Commit

Permalink
Automatically create git tag and GitHub release on "publish"
Browse files Browse the repository at this point in the history
When we release a new version to crates.io, it makes sense to also tag
the git commit in the repository and create a proper GitHub release.
Doing so eliminates a bunch of work that otherwise has to be done
manually. Implement the necessary logic.
  • Loading branch information
d-e-s-o committed Aug 16, 2024
1 parent a4ce5f3 commit eb473dd
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,59 @@ on:
workflow_dispatch:

jobs:
version:
name: Retrieve version
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v4
- id: version
shell: bash
run: |
cargo generate-lockfile
pkgid="$(cargo pkgid)"
# Format is typically
# file://<path>/<crate>#<version>
# but could also be along the lines of
# file://<path>/<crate>#<actual-crate-name>@<version>
version="$(echo ${pkgid} | cut -d '#' -f2 | cut -d '@' -f2 | grep -o '[^:]*$')"
if [ -z "${version}" ]; then
echo "Invalid version string: ${pkgid}"
exit 1
fi
echo "Determined crate version: ${version}"
echo "version=${version}" >> $GITHUB_OUTPUT
test:
uses: ./.github/workflows/test.yml
secrets: inherit
publish:
needs: [test]
needs: [test, version]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Dry-run package creation
run: cargo package --no-verify
- name: Create git tag
env:
version: ${{ needs.version.outputs.version }}
run: |
curl --location \
--fail-with-body \
--request POST \
--url https://api.github.com/repos/${{ github.repository }}/releases \
--header "Accept: application/vnd.github+json" \
--header "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
--header "X-GitHub-Api-Version: 2022-11-28" \
--data "{
\"tag_name\":\"v${version}\",
\"target_commitish\":\"${{ github.ref }}\",
\"name\":\"v${version}\",
\"draft\":false,
\"prerelease\":false,
\"generate_release_notes\":false
}"
- name: Publish
run: cargo publish --no-verify --token "${CARGO_REGISTRY_TOKEN}"
env:
Expand Down

0 comments on commit eb473dd

Please sign in to comment.