Skip to content

Release with GitHub Action #2

Release with GitHub Action

Release with GitHub Action #2

name: Release with GitHub Action
permissions:
contents: write
pull-requests: write
on:
workflow_dispatch:
inputs:
whichCrate:
description: 'Which crate you wish to release?'
required: true
type: choice
options:
- nns
- sns
semverBump:
description: 'Specify SemVer version you wish to bump (see: https://github.com/crate-ci/cargo-release/blob/master/docs/reference.md#bump-level)'
required: true
type: choice
options:
- custom
- release
- patch
- minor
- major
- alpha
- beta
- rc
semverVersion:
description: 'Specify exact SemVer version (corresponds to [version] listed here: https://github.com/crate-ci/cargo-release/blob/master/docs/reference.md#bump-level). Works only when you have selected [custom] in previous dropdox.'
default: ''
required: false
type: string
jobs:
create-release:
runs-on: ubuntu-latest
steps:
- name: check parameters
if: ${{ inputs.semverBump == 'custom' && inputs.semverVersion == '' }}
run: |
echo "You have selected [custom] in the previous dropdown, but you have not provided the exact version. Please provide the exact version."
exit 1
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: cargo-bins/cargo-binstall@main
- run: cargo binstall cargo-release -y
- name: Unify semver choice
env:
SEMVER_CHOICE: ${{ inputs.semverBump == 'custom' && inputs.semverVersion || inputs.semverBump }}
run: |
echo "SEMVER_CHOICE is $SEMVER_CHOICE"
echo "SEMVER_CHOICE=$SEMVER_CHOICE" >> "$GITHUB_ENV"
- name: Determine new version number by dry-running `cargo-release`
run: |
echo "SEMVER_CHOICE is $SEMVER_CHOICE"
NEW_VERSION="$(
cargo release version -p ${{ inputs.whichCrate}} ${{ env.SEMVER_CHOICE }} 2>&1 \
| grep "Upgrading .* from .* to .*" \
| awk '{print $6}' \
)"
echo "New version is $NEW_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_ENV"
- name: Switch to the release branch
run: |
BRANCH_NAME="release/${{ inputs.whichCrate }}-v${{ env.NEW_VERSION }}"
git switch -c "$BRANCH_NAME"
- name: Set up git config
run: |
git config author.email "${{ github.event.sender.id }}+${{ github.event.sender.login }}@users.noreply.github.com"
git config author.name "${{ github.event.sender.login }}"
git config committer.email "41898282+github-actions[bot]@users.noreply.github.com"
git config committer.name "GitHub Actions Bot"
git config user.email "${{ github.event.sender.id }}+${{ github.event.sender.login }}@users.noreply.github.com"
git config user.name "${{ github.event.sender.login }}"
- name: Roll changelog, bump version, and push branch
run: |
# see https://opensource.axo.dev/cargo-dist/book/workspaces/cargo-release-guide.html#using-cargo-release-with-pull-requests
cargo release -p ${{ inputs.whichCrate }} "${{ env.SEMVER_CHOICE }}" --execute --no-confirm --config extensions/${{ inputs.whichCrate }}/prepare-release.toml
- name: Open the release PR
env:
GH_TOKEN: ${{ github.token }}
run: |
TAG="${{ inputs.whichCrate }}-v${{ env.NEW_VERSION }}"
HEAD="release/$TAG"
TITLE="chore(${{ inputs.whichCrate }}): release v${{ env.NEW_VERSION }}"
cat >BODY.md <<EOF
PR created by this workflow: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
Link to release: https://github.com/${{ github.server_url }}/${{ github.repository }}/releases/tag/$TAG
After merging, run the following:
\`\`\`bash
git checkout main
git pull
cargo dist plan
cargo release -p ${{ inputs.whichCrate }} --execute
\`\`\`
EOF
echo 'The message "Warning: 1 uncommitted change" refers to BODY.md'
gh pr create --base main --head "$HEAD" --title "$TITLE" --body-file BODY.md