From a25775a3afce305adccc7e93db377dae4b335682 Mon Sep 17 00:00:00 2001 From: Yuval Shavit Date: Sun, 2 Mar 2025 04:39:09 -0500 Subject: [PATCH] create action to prepare releases --- .github/workflows/build-release.yml | 2 +- .github/workflows/prepare-new-release.yml | 149 ++++++++++++++++++++++ 2 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/prepare-new-release.yml diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index 8535433..e06c19e 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -1,4 +1,4 @@ -name: Release +name: Binaries on: push: branches: [ "main" ] diff --git a/.github/workflows/prepare-new-release.yml b/.github/workflows/prepare-new-release.yml new file mode 100644 index 0000000..32ba374 --- /dev/null +++ b/.github/workflows/prepare-new-release.yml @@ -0,0 +1,149 @@ +name: Prepare New Release + +on: + workflow_dispatch: + inputs: + version_to_release: + description: 'version to cut a release as' + required: true + type: string + next_version: + description: 'the new dev to prepare (w/o "-dev" suffix)' + required: true + type: string + base_ref: + description: 'the git ref to go against' + required: false + type: string + default: main + base_ref_version: + description: 'verify current dev version (w/o "-dev" suffix)' + required: true + type: string + +jobs: + run: + runs-on: ubuntu-latest + env: + GH_TOKEN: ${{ github.token }} + RELEASE_VERSION: ${{ github.event.inputs.version_to_release }} + NEXT_DEV_VERSION: "${{ github.event.inputs.next_version }}-dev" + TAG_NAME: "v${{ github.event.inputs.version_to_release }}" + permissions: + contents: write + pull-requests: write + + steps: + - name: Configure git + run: | + set -euo pipefail + git config --global user.name 'github-actions[bot]' + git config --global user.email 'github-actions[bot]@users.noreply.github.com' + + - uses: actions/checkout@v2 + with: + ref: ${{ github.event.inputs.base_ref }} + + - name: Check that tag is available + run: | + if gh release view "$TAG_NAME" ; then + echo "::error ::tag $TAG_NAME already exists" + exit 1 + fi + + - name: Check Cargo.toml version + run: | + set -euo pipefail + toml_current_version=$(grep '^version' Cargo.toml | sed 's/version = "\(.*\)"/\1/') + expect_version="${VERIFY_VERSION}-dev" + if [[ "$toml_current_version" != "$expect_version" ]]; then + echo "::error title=bad version::Expected version $expect_version does not match current version $toml_current_version." + exit 1 + fi + env: + VERIFY_VERSION: ${{ github.event.inputs.base_ref_version }} + + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + + - name: Release Cargo.toml + run: | + sed -i 's/^version = ".*"/version = "${{ env.RELEASE_VERSION }}"/' Cargo.toml + cargo metadata >/dev/null + + - name: Release Commit + run: | + set -euo pipefail + git checkout -b "release-$RELEASE_VERSION" + git commit -am "bump version to $RELEASE_VERSION" + git push --set-upstream origin "release-$RELEASE_VERSION" + + - name: Release PR + run: | + gh pr create --title "Release v${RELEASE_VERSION}" --body "Bump version to $RELEASE_VERSION" --base "$TARGET_REF" + env: + TARGET_REF: ${{ github.event.inputs.base_ref }} + + - name: Dev Cargo.toml + run: | + sed -i 's/^version = ".*"/version = "${{ env.NEXT_DEV_VERSION }}"/' Cargo.toml + cargo metadata >/dev/null + + - name: Dev Commit + run: | + set -euo pipefail + git checkout -b "release-$RELEASE_VERSION-post" + git commit -am "bump version to $NEXT_DEV_VERSION" + git push --set-upstream origin "release-$RELEASE_VERSION-post" + + - name: Dev PR + run: | + gh pr create --title "Prepare v${NEXT_DEV_VERSION}" --body "Bump version to $NEXT_DEV_VERSION" --base "release-$RELEASE_VERSION" + + - name: Prepare random dir + id: random-dir + run: echo "path=$(mktemp -d)" >> "$GITHUB_OUTPUT" + + - name: Find latest build + id: latest-build + run: | + set -euo pipefail + run_info="$(gh run list -b "$RUN_REF" -w build-release.yml --limit 1 --json status,conclusion,databaseId)" + run_id="$(echo "$run_info" | jq -re '.[].databaseId' )" + + if [[ "$(echo "$run_info" | jq -r '.[].conclusion')" != success ]]; then + echo "::error ::Found run $run_id but its conclusion was $(echo "$run_info" | jq '.[].conclusion')" + exit 1 + fi + echo "run_id=$run_id" >> "$GITHUB_OUTPUT" + env: + RUN_REF: ${{ github.event.inputs.base_ref }} + + - name: Download build artifacts + run: gh run download "$RUN_ID" -p 'mdq-*' --dir "$TMP_DIR" + env: + TMP_DIR: ${{ steps.random-dir.outputs.path }} + RUN_ID: ${{ steps.latest-build.outputs.run_id }} + + - name: Re-zip build artifacts + run: | + set -euo pipefail + for artifact_name in $(ls -1) ; do + zip "${artifact_name}.zip" "$artifact_name"/* + rm -rf "$artifact_name"/ + done + working-directory: ${{ steps.random-dir.outputs.path }} + + - name: Create new release + id: create_release + run: | + release_url="$(gh release create "$TAG_NAME" --draft --title "$TAG_NAME" --generate-notes --target "release-$RELEASE_VERSION")" + echo "release_url=$release_url" >> "$GITHUB_OUTPUT" + + - name: Upload artifacts + run: gh release upload "$TAG_NAME" "$TMP_DIR"/* + env: + TMP_DIR: ${{ steps.random-dir.outputs.path }}