From afc4909599c59e29fc849cb562e6b55945abad23 Mon Sep 17 00:00:00 2001 From: Simon Jagoe Date: Fri, 11 Oct 2024 10:13:39 +0300 Subject: [PATCH 1/6] Allow attaching commit status to specific commit. Useful in workflow dispatch runs where the version operated on is not the latest (e.g. during deploy/rollback from shipit) --- create/action.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/create/action.yml b/create/action.yml index 2beca52..1b1d1ec 100644 --- a/create/action.yml +++ b/create/action.yml @@ -11,6 +11,10 @@ inputs: target-url: description: "URL to include in the status" default: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" + sha: + description: "Commit sha to attach the status to if it's different from the workflow run commit" + required: false + default: "" outputs: commit_status_sha: @@ -23,10 +27,14 @@ runs: - name: Determine commit to put the build status on id: commit_status_sha shell: bash + env: + COMMIT_STATUS_SHA: ${{ inputs.sha }} run: | set -eu -o pipefail - if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then + if [ -n "$COMMIT_STATUS_SHA" ]; then + SHA="$COMMIT_STATUS_SHA" + elif [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then SHA="${{ github.event.pull_request.head.sha }}" else SHA="${{ github.sha }}" From 05b02a09a607cd2bfbe5ddb0e20e0355ea9a2140 Mon Sep 17 00:00:00 2001 From: Simon Jagoe Date: Fri, 11 Oct 2024 10:19:57 +0300 Subject: [PATCH 2/6] Update templates --- create/action.yml.erb | 6 ++++++ create/get-commit-sha.bash | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/create/action.yml.erb b/create/action.yml.erb index be7fb75..f3003c4 100644 --- a/create/action.yml.erb +++ b/create/action.yml.erb @@ -11,6 +11,10 @@ inputs: target-url: description: "URL to include in the status" default: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" + sha: + description: "Commit sha to attach the status to if it's different from the workflow run commit" + required: false + default: "" outputs: commit_status_sha: @@ -23,6 +27,8 @@ runs: - name: Determine commit to put the build status on id: commit_status_sha shell: bash + env: + COMMIT_STATUS_SHA: ${{ inputs.sha }} run: | @import ../script-import-helper (script_name: "get-commit-sha.bash") - name: Create commit status diff --git a/create/get-commit-sha.bash b/create/get-commit-sha.bash index c0ed538..0f55565 100644 --- a/create/get-commit-sha.bash +++ b/create/get-commit-sha.bash @@ -1,6 +1,8 @@ set -eu -o pipefail -if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then +if [ -n "$COMMIT_STATUS_SHA" ]; then + SHA="$COMMIT_STATUS_SHA" +elif [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then SHA="${{ github.event.pull_request.head.sha }}" else SHA="${{ github.sha }}" From dc6564e313452e034339a3199aa39a827dde8f40 Mon Sep 17 00:00:00 2001 From: Simon Jagoe Date: Fri, 11 Oct 2024 13:48:47 +0300 Subject: [PATCH 3/6] Update CI --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c59acee..077d6b5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,7 +21,7 @@ jobs: shellcheck: # Use newer os for more up-to-date shellcheck - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Run shellcheck on .sh and .bash files files @@ -35,7 +35,7 @@ jobs: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: - ruby-version: "2.7" + ruby-version: "3.2" - name: Install kojo run: gem install kojo - run: make From e844109cc5602f96c404cccc80ba71df94c18845 Mon Sep 17 00:00:00 2001 From: Simon Jagoe Date: Fri, 11 Oct 2024 13:54:43 +0300 Subject: [PATCH 4/6] Fix shellcheck failures and remove silly templating --- .github/workflows/test.yml | 22 +------------- create/action.yml | 28 +++-------------- create/action.yml.erb | 43 --------------------------- create/get-commit-sha.bash | 4 +-- update/action.yml | 61 ++------------------------------------ update/action.yml.erb | 47 ----------------------------- 6 files changed, 9 insertions(+), 196 deletions(-) delete mode 100644 create/action.yml.erb delete mode 100644 update/action.yml.erb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 077d6b5..07650b3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,29 +29,9 @@ jobs: find . -name '*.sh' -exec shellcheck -s bash '{}' \+ find . -name '*.bash' -exec shellcheck -s bash '{}' \+ - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: ruby/setup-ruby@v1 - with: - ruby-version: "3.2" - - name: Install kojo - run: gem install kojo - - run: make - - name: Check for changes - run: | - git status - git_changes="$(git status --porcelain || true)" - # If the output is not empty, there are changes; fail the action - if [ -n "$git_changes" ]; then - echo "Changes found; build the action with 'make' and commit the changes" - exit 1 - fi - finalize_status: runs-on: ubuntu-latest - needs: [shellcheck, build, create_status] + needs: [shellcheck, create_status] if: ${{ always() }} steps: - uses: actions/checkout@v4 diff --git a/create/action.yml b/create/action.yml index 1b1d1ec..6673d83 100644 --- a/create/action.yml +++ b/create/action.yml @@ -29,18 +29,10 @@ runs: shell: bash env: COMMIT_STATUS_SHA: ${{ inputs.sha }} + PULL_REQUEST_SHA: ${{ github.event.pull_request.head.sha }} + GITHUB_SHA: ${{ github.sha }} run: | - set -eu -o pipefail - - if [ -n "$COMMIT_STATUS_SHA" ]; then - SHA="$COMMIT_STATUS_SHA" - elif [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then - SHA="${{ github.event.pull_request.head.sha }}" - else - SHA="${{ github.sha }}" - fi - echo sha="$SHA" >> "$GITHUB_OUTPUT" - + "${GITHUB_ACTION_PATH}/get-commit-sha.bash" - name: Create commit status shell: bash env: @@ -50,16 +42,4 @@ runs: DESCRIPTION: "Build status is pending" CONTEXT: ${{ inputs.context }} run: | - set -eu -o pipefail - - declare -a ARGS - if [[ -n "$TARGET_URL" ]]; then - ARGS=("-f" "target_url=${TARGET_URL}") - fi - - gh api "/repos/${GITHUB_REPOSITORY}/statuses/${COMMIT_STATUS_SHA}" \ - -X POST \ - -f state=pending \ - -f description="$DESCRIPTION" \ - -f context="$CONTEXT" \ - "${ARGS[@]}" + "${GITHUB_ACTION_PATH}/"create-commit-status.bash" diff --git a/create/action.yml.erb b/create/action.yml.erb deleted file mode 100644 index f3003c4..0000000 --- a/create/action.yml.erb +++ /dev/null @@ -1,43 +0,0 @@ -name: 'Create pending github commit statuses' -description: Create a github commit status context in the pending state - -inputs: - github-token: - description: "Github authentication token" - required: true - context: - description: "Github status context" - default: continuous-integration/github-actions/status - target-url: - description: "URL to include in the status" - default: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" - sha: - description: "Commit sha to attach the status to if it's different from the workflow run commit" - required: false - default: "" - -outputs: - commit_status_sha: - description: "The commit sha that the status is attached to" - value: ${{ steps.commit_status_sha.outputs.sha }} - -runs: - using: "composite" - steps: - - name: Determine commit to put the build status on - id: commit_status_sha - shell: bash - env: - COMMIT_STATUS_SHA: ${{ inputs.sha }} - run: | - @import ../script-import-helper (script_name: "get-commit-sha.bash") - - name: Create commit status - shell: bash - env: - GITHUB_TOKEN: ${{ inputs.github-token }} - TARGET_URL: ${{ inputs.target-url }} - COMMIT_STATUS_SHA: ${{ steps.commit_status_sha.outputs.sha }} - DESCRIPTION: "Build status is pending" - CONTEXT: ${{ inputs.context }} - run: | - @import ../script-import-helper (script_name: "create-commit-status.bash") diff --git a/create/get-commit-sha.bash b/create/get-commit-sha.bash index 0f55565..36e4adb 100644 --- a/create/get-commit-sha.bash +++ b/create/get-commit-sha.bash @@ -3,8 +3,8 @@ set -eu -o pipefail if [ -n "$COMMIT_STATUS_SHA" ]; then SHA="$COMMIT_STATUS_SHA" elif [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then - SHA="${{ github.event.pull_request.head.sha }}" + SHA="$PULL_REQUEST_SHA" else - SHA="${{ github.sha }}" + SHA="$GITHUB_SHA" fi echo sha="$SHA" >> "$GITHUB_OUTPUT" diff --git a/update/action.yml b/update/action.yml index 4449dbb..11dc7e4 100644 --- a/update/action.yml +++ b/update/action.yml @@ -33,52 +33,7 @@ runs: env: GITHUB_NEEDS: ${{ inputs.github-needs }} run: | - set -eu -o pipefail - - cat > update_commit_status-needs.json <&2 - count="$(echo "$with_status" | jq '. | length')" - echo "$count" - } - function jobsWithStatus() { - local status="$1" - jq -r '. | to_entries | .[] | select(.value.result == "'"$status"'") | .key' > "$GITHUB_OUTPUT" - echo description="$description" >> "$GITHUB_OUTPUT" - + "${GITHUB_ACTION_PATH}/get-commit-status.bash" - name: Update commit status shell: bash env: @@ -89,16 +44,4 @@ runs: CONTEXT: ${{ inputs.context }} STATUS: ${{ steps.commit_status.outputs.status }} run: | - set -eu -o pipefail - - declare -a ARGS - if [[ -n "$TARGET_URL" ]]; then - ARGS=("-f" "target_url=${TARGET_URL}") - fi - - gh api "/repos/${GITHUB_REPOSITORY}/statuses/${COMMIT_STATUS_SHA}" \ - -XPOST \ - -f state="$STATUS" \ - -f description="$DESCRIPTION" \ - -f context="$CONTEXT" \ - "${ARGS[@]}" + "${GITHUB_ACTION_PATH}/update-commit-status.bash" diff --git a/update/action.yml.erb b/update/action.yml.erb deleted file mode 100644 index a150bb5..0000000 --- a/update/action.yml.erb +++ /dev/null @@ -1,47 +0,0 @@ -name: 'Create pending github commit statuses' -description: Update a pending commit status context with the final result - -inputs: - github-token: - description: "Github authentication token" - required: true - github-needs: - description: >- - "needs" object from github actions as JSON format. - - MUST be passed as the github templated toJson(needs) - required: true - context: - description: "Github status context" - default: continuous-integration/github-actions/status - target-url: - description: "URL to include in the status" - default: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" - sha: - description: >- - The git commit sha to attach the status to. This should be - provided from the `commit_status_sha` output of the create - action from this same repository. - required: true - -runs: - using: "composite" - steps: - - name: Get final commit status - id: commit_status - shell: bash - env: - GITHUB_NEEDS: ${{ inputs.github-needs }} - run: | - @import ../script-import-helper (script_name: "get-commit-status.bash") - - name: Update commit status - shell: bash - env: - GITHUB_TOKEN: ${{ inputs.github-token }} - TARGET_URL: ${{ inputs.target-url }} - COMMIT_STATUS_SHA: ${{ inputs.sha }} - DESCRIPTION: ${{ steps.commit_status.outputs.description }} - CONTEXT: ${{ inputs.context }} - STATUS: ${{ steps.commit_status.outputs.status }} - run: | - @import ../script-import-helper (script_name: "update-commit-status.bash") From e694a03310832c9c2c83aaa9218f05ef6c1ff75c Mon Sep 17 00:00:00 2001 From: Simon Jagoe Date: Fri, 11 Oct 2024 13:59:10 +0300 Subject: [PATCH 5/6] Fix scripts --- create/create-commit-status.bash | 2 ++ create/get-commit-sha.bash | 2 ++ update/get-commit-status.bash | 2 ++ update/update-commit-status.bash | 2 ++ 4 files changed, 8 insertions(+) mode change 100644 => 100755 create/create-commit-status.bash mode change 100644 => 100755 create/get-commit-sha.bash mode change 100644 => 100755 update/get-commit-status.bash mode change 100644 => 100755 update/update-commit-status.bash diff --git a/create/create-commit-status.bash b/create/create-commit-status.bash old mode 100644 new mode 100755 index 28145ac..420a57e --- a/create/create-commit-status.bash +++ b/create/create-commit-status.bash @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + set -eu -o pipefail declare -a ARGS diff --git a/create/get-commit-sha.bash b/create/get-commit-sha.bash old mode 100644 new mode 100755 index 36e4adb..ce0bf52 --- a/create/get-commit-sha.bash +++ b/create/get-commit-sha.bash @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + set -eu -o pipefail if [ -n "$COMMIT_STATUS_SHA" ]; then diff --git a/update/get-commit-status.bash b/update/get-commit-status.bash old mode 100644 new mode 100755 index e21aea1..8528c81 --- a/update/get-commit-status.bash +++ b/update/get-commit-status.bash @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + set -eu -o pipefail cat > update_commit_status-needs.json < Date: Fri, 11 Oct 2024 14:00:02 +0300 Subject: [PATCH 6/6] Fix copy error --- create/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/create/action.yml b/create/action.yml index 6673d83..4d3af3c 100644 --- a/create/action.yml +++ b/create/action.yml @@ -42,4 +42,4 @@ runs: DESCRIPTION: "Build status is pending" CONTEXT: ${{ inputs.context }} run: | - "${GITHUB_ACTION_PATH}/"create-commit-status.bash" + "${GITHUB_ACTION_PATH}/create-commit-status.bash"