Skip to content

Commit

Permalink
Attempt a more automated workflow for rust crate version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGross committed Apr 13, 2024
1 parent 03c1ff9 commit b777870
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-automerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
printf "github.actor == '%s'\n" "${{ github.actor }}"
- name: merge
run: gh pr merge --auto --squash --delete-branch "$PR_URL"
if: ${{ ( github.actor == 'dependabot[bot]' ) || ( github.actor == 'github-actions[bot]' && github.event.pull_request.title == 'Rust Crate Version Bump' ) }}
if: ${{ github.actor == 'dependabot[bot]' }}
62 changes: 44 additions & 18 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ on:
# For more on how to formally release on Github, read https://help.github.com/en/articles/creating-releases

jobs:
publish:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]

publish-rust-crate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Login to crates.io
Expand All @@ -24,6 +20,13 @@ jobs:
run: cargo publish --manifest-path fiat-rust/Cargo.toml
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.crates_io_token }}

bump-rust-crate-version:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Bump Rust crate version
id: bumpRustViaPush
run: |
Expand All @@ -43,17 +46,40 @@ jobs:
git add fiat-rust/Cargo.toml
timestamp=$(date -u)
git commit -m "Automated Rust Crate Version Bump: ${timestamp} ${GITHUB_SHA}"
git push publisher master
if git push publisher master; then
echo "Push successful"
else
# If the push fails, get the short SHA of the latest commit
SHORT_SHA=$(git rev-parse --short HEAD)
# Create and push to a new branch
git checkout -b rust-crate-version-bump-$SHORT_SHA
git push --set-upstream publisher rust-crate-version-bump-$SHORT_SHA
FAILURE_MESSAGE="PR creation failed"
# Locate workflow file, remove the Git ref at the end
WORKFLOW_PATH="${GITHUB_WORKFLOW_REF%@*}"
# Split the string to insert the blob/SHA part correctly
REPO_PATH="${WORKFLOW_PATH%/.github/*}" # This extracts 'everything before /.github/'
WORKFLOW_FILE_PATH=".github/${WORKFLOW_PATH#*.github/}" # This extracts '.github/...'
WORKFLOW_URL="https://github.com/${REPO_PATH}/blob/${{ github.sha }}/${WORKFLOW_FILE_PATH}"
# Attempt to create a pull request via gh CLI, capture output even if it fails
PR_OUTPUT="$(gh pr create --title "Rust Crate Version Bump" \
--body "This PR is auto-generated by [GitHub Actions](${WORKFLOW_URL})." \
--base master --head rust-crate-version-bump-$SHORT_SHA \
--label "rust,automated pr,skip ci" \
--repo ${{ github.repository }} \
|| echo "${FAILURE_MESSAGE}")"
echo "PR output: $PR_OUTPUT"
# Check if PR creation was successful and proceed with auto-merge
if [[ "$PR_OUTPUT" != *"$FAILURE_MESSAGE" ]]; then
if ! gh pr merge --admin --squash --delete-branch $PR_OUTPUT; then
echo "Immediate merge with --admin failed, attempting auto-merge without --admin"
gh pr merge --auto --squash --delete-branch $PR_OUTPUT
fi
else
echo "Skipping auto-merge as PR creation failed"
exit 1
fi
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: always()
- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
title: 'Rust Crate Version Bump'
body: >
This PR is auto-generated by
[create-pull-request](https://github.com/peter-evans/create-pull-request).
labels: rust, automated pr
if: failure() && steps.bumpRustViaPush.outcome == 'failure'
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit b777870

Please sign in to comment.