From 1aab09f5507f9a2c7905e8c7ebd38dd649d1bb96 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 16 Sep 2024 20:06:15 +0000 Subject: [PATCH] feat: update release and prelease to our standards prerelease label, use semantic-release several todos still --- prerelease/action.yaml | 147 +++++++++++++++++++++++++++++++++++++++-- release/action.yaml | 64 +++++++++++++++--- 2 files changed, 194 insertions(+), 17 deletions(-) diff --git a/prerelease/action.yaml b/prerelease/action.yaml index 23991aa..78a3457 100644 --- a/prerelease/action.yaml +++ b/prerelease/action.yaml @@ -5,6 +5,14 @@ inputs: required: false description: Perform checkout as first step of action default: "true" + checkout-fetch-depth: + required: false + description: The number of commits to fetch. 0 indicates all history for all branches and tags + default: "0" + create-prerelease: + required: false + description: Whether semantic-release should create a prerelease or do a dry run. This can be useful to set to true when a prerelease requires pushing artifacts semantic-release is in charge of generating + default: "false" github-token: description: GitHub token that can checkout the consumer repository as well as create tags/releases against it. e.g. 'secrets.GITHUB_TOKEN' required: true @@ -12,6 +20,7 @@ inputs: description: Go version to use for building required: true default: 1.17.3 + # TODO Could this be replaced by create-prerelease + the presence of the docker credentials? push-docker-snapshot: description: If a docker snapshot image is generated, push it to the to the registry required: false @@ -22,6 +31,11 @@ inputs: docker-password: description: Docker password to push the snapshot image to the registry required: false + extra-plugins: + required: false + description: Extra plugins for pre-install. You can also specify specifying version range for the extra plugins if you prefer. Defaults to install @open-turo/semantic-release-config. + default: | + @open-turo/semantic-release-config outputs: version: description: Version of the project @@ -29,30 +43,103 @@ outputs: runs: using: composite steps: + - name: Set vars + id: source-vars + shell: bash + env: + event_name: ${{ github.event_name }} + dispatch_client_payload_ref: ${{ github.event.client_payload.ref }} + dispatch_client_payload_sha: ${{ github.event.client_payload.sha }} + push_ref_name: ${{ github.ref_name }} + push_sha: ${{ github.sha }} + pull_request_ref_name: ${{ github.event.pull_request.head.ref }} + pull_request_sha: ${{ github.event.pull_request.head.sha }} + run: | + echo "event_name=$event_name" + + if [ "$event_name" == "push" ]; then + branch=$push_ref_name + sha=$push_sha + elif [ "$event_name" == "repository_dispatch" ]; then + branch=$dispatch_client_payload_ref + sha=$dispatch_client_payload_sha + elif [ "$event_name" == "pull_request" ]; then + branch=$pull_request_ref_name + sha=$pull_request_sha + else + echo "::error::Unsupported event type '$event_name'" + exit 1 + fi + + echo "branch=$branch" + echo "branch=$branch" >> $GITHUB_OUTPUT + + echo "sha=$sha" + echo "sha=$sha" >> $GITHUB_OUTPUT + - name: Checkout uses: actions/checkout@v4 if: inputs.checkout-repo == 'true' with: - fetch-depth: 0 + fetch-depth: ${{ inputs.checkout-fetch-depth }} + ref: ${{ steps.source-vars.outputs.branch }} + + # Find PR + - uses: 8BitJonny/gh-get-current-pr@3.0.0 + id: PR + with: + sha: ${{ steps.source-vars.outputs.sha }} + + - id: check-pr + shell: bash + run: | + if [ -z "${{ steps.PR.outputs.number }}" ]; then + echo "pr_found=false" >> $GITHUB_OUTPUT + echo "has_prerelease_label=false" >> $GITHUB_OUTPUT + else + echo "pr_found=true" >> $GITHUB_OUTPUT + echo "has_prerelease_label=${{ contains(toJSON(fromJSON(steps.PR.outputs.pr).labels.*.name), 'prerelease') }}" >> $GITHUB_OUTPUT + fi + git branch + echo "branch: ${{ steps.source-vars.outputs.branch }}" + + # Allow the action to download any private dependency in the go library - name: Authorize - uses: open-turo/action-git-auth@v2 + uses: open-turo/action-git-auth@v3 with: github-personal-access-token: ${{ inputs.github-token }} + - name: Setup tools ## Installs version of golang found in .go-version uses: open-turo/action-setup-tools@v2 - - name: Semantic release - uses: go-semantic-release/action@v1 - id: release + + - name: Prerelease + id: prerelease + uses: open-turo/actions-release/semantic-release@v5 + if: steps.check-pr.outputs.has_prerelease_label == 'true' with: + branches: '["${{ github.event.repository.default_branch }}", {"name": "${{ steps.source-vars.outputs.branch }}","channel": "next","prerelease": "pr-${{ steps.PR.outputs.number }}.${{ github.run_number }}.${{ github.run_attempt }}"}]' + dry-run: ${{ inputs.create-prerelease == 'false' }} + extra-plugins: ${{ inputs.extra-plugins }} github-token: ${{ inputs.github-token }} + override-github-ref-name: ${{ steps.source-vars.outputs.branch }} + env: + GITHUB_TOKEN: ${{ inputs.github-token }} + - id: vars + if: steps.check-pr.outputs.has_prerelease_label == 'true' + shell: bash + run: | + echo "version=${{ steps.prerelease.outputs.new-release-version }}" >> $GITHUB_OUTPUT + echo "run-url=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> "$GITHUB_OUTPUT" + - name: Goreleaser uses: goreleaser/goreleaser-action@v6 env: GITHUB_TOKEN: ${{ inputs.github-token }} GOVERSION: ${{ inputs.go-version }} with: - args: release --snapshot + args: release + - name: Docker login if: inputs.push-docker-snapshot == 'true' uses: docker/login-action@v3 @@ -69,3 +156,51 @@ runs: for image in $DOCKER_IMAGES; do docker push $image done + + - name: Add new version to summary + shell: bash + if: steps.prerelease.outputs.new-release-published == 'true' + env: + NEW_VERSION: ${{ steps.vars.outputs.version }} + DOCKER_IMAGE: ${{ steps.build-docker.outputs.image-with-tag }} + run: | + echo "::notice::new version: \`${NEW_VERSION}\`" + echo "#### New version: \`${NEW_VERSION}\`" >> $GITHUB_STEP_SUMMARY + echo "#### Docker image: \`${DOCKER_IMAGE}\`" >> $GITHUB_STEP_SUMMARY + + - name: Add no new version to summary + shell: bash + if: steps.prerelease.outputs.new-release-published != 'true' + run: | + echo "::notice::no new version" + echo "### New version: 'NONE" >> $GITHUB_STEP_SUMMARY + + - name: Check for release notes comment + uses: peter-evans/find-comment@v3 + id: fc-prerelease + if: steps.prerelease.outputs.new-release-published == 'true' + with: + issue-number: ${{ steps.PR.outputs.number }} + comment-author: "github-actions[bot]" + body-includes: "" + - name: Delete previous release note + if: steps.fc-prerelease.outputs.comment-id != '' + uses: winterjung/comment@v1 + with: + type: delete + comment_id: ${{ steps.fc-prerelease.outputs.comment-id }} + token: ${{ inputs.github-token }} + + - name: Upsert build version + if: steps.prerelease.outputs.new-release-published == 'true' + uses: peter-evans/create-or-update-comment@v4 + with: + issue-number: ${{ steps.PR.outputs.number }} + body: | + + ## Prerelease build + + **Build version:** `${{ steps.vars.outputs.version }}` + **Docker image:** `${{ steps.build-docker.outputs.image-with-tag }}` + + [Build output](${{ steps.vars.outputs.run-url }}) diff --git a/release/action.yaml b/release/action.yaml index 99af90e..1a8a77f 100644 --- a/release/action.yaml +++ b/release/action.yaml @@ -5,6 +5,10 @@ inputs: required: false description: Perform checkout as first step of action default: "true" + checkout-fetch-depth: + required: false + description: The number of commits to fetch. 0 indicates all history for all branches and tags + default: "0" github-token: description: GitHub token that can checkout the consumer repository as well as create tags/releases against it. e.g. 'secrets.GITHUB_TOKEN' required: true @@ -12,6 +16,15 @@ inputs: description: Go version to use for building required: true default: 1.17.3 + dry-run: + required: false + description: Whether to run semantic release in `dry-run` mode. It will override the `dryRun` attribute in your configuration file + default: "false" + extra-plugins: + required: false + description: Extra plugins for pre-install. You can also specify specifying version range for the extra plugins if you prefer. Defaults to install @open-turo/semantic-release-config. + default: | + @open-turo/semantic-release-config outputs: version: description: Version of the project @@ -23,29 +36,58 @@ runs: uses: actions/checkout@v4 if: inputs.checkout-repo == 'true' with: - fetch-depth: 0 + fetch-depth: ${{ inputs.checkout-fetch-depth }} + persist-credentials: false + - uses: 8BitJonny/gh-get-current-pr@3.0.0 + id: PR + with: + sha: ${{ github.event.pull_request.head.sha }} + - name: Branches configuration + id: branches-configuration + shell: bash + run: | + if [ -z "${{ steps.PR.outputs.number }}" ]; then + echo "branches=${{ github.event.repository.default_branch }}" >> $GITHUB_OUTPUT + else + echo "branches=[\"${{ github.event.repository.default_branch }}\", {\"name\": \"${{ github.ref_name }}\",\"channel\": \"next\",\"prerelease\": \"pr-${{ steps.PR.outputs.number }}.${{ github.run_number }}.${{ github.run_attempt }}\"}]" >> $GITHUB_OUTPUT + fi + # Allow the action to download any private dependency in the go library - name: Authorize - uses: open-turo/action-git-auth@v2 + uses: open-turo/action-git-auth@v3 with: github-personal-access-token: ${{ inputs.github-token }} - - name: Setup tools - ## Installs version of golang found in .go-version - uses: open-turo/action-setup-tools@v2 - - name: Semantic release - uses: go-semantic-release/action@v1 + - name: Release id: release + uses: open-turo/actions-release/semantic-release@v5 with: + branches: ${{ steps.branches-configuration.outputs.branches }} + dry-run: ${{ inputs.dry-run }} + extra-plugins: ${{ inputs.extra-plugins }} github-token: ${{ inputs.github-token }} - - name: Fetch tags + env: + GITHUB_TOKEN: ${{ inputs.github-token }} + # Since the release is created with semantic-release, we need to fetch the tags + # TODO Is this really needed + # - name: Fetch tags + # shell: bash + # run: | + # git fetch --tags + # git clean -fd + - name: Set args + id: goreleaser-args shell: bash run: | - git fetch --tags - git clean -fd + if [[ "${{ inputs.dry-run }}" == "true" ]]; then + echo "args=--skip-publish" >> $GITHUB_OUTPUT + fi - name: Goreleaser uses: goreleaser/goreleaser-action@v6 env: GITHUB_TOKEN: ${{ inputs.github-token }} GOPRIVATE: github.com/turo/ + # TODO Read this from the .go-version file instead GOVERSION: ${{ inputs.go-version }} + # TODO Handle the prerelease value? How can we get this out of the semantic-release output? + # This is done in the goreleaser config file with the prerelease: auto option with: - args: release + args: release ${{ steps.goreleaser-args.outputs.args }}