Skip to content

Commit

Permalink
Add optional output to result (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored Mar 25, 2021
1 parent b0876c3 commit 6dd04d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# verify-ci-status-action

Verifies that a CI status (check run) is passing before proceeding.

Used by [JEP-229](https://jenkins.io/jep/229).
12 changes: 11 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ name: verify-ci-status
description: Verifies that a CI status is passing before proceeding.
inputs:
GITHUB_TOKEN:
description: Token to run with, defaults to the repository GITHUB_TOKEN
required: true
output_result:
description: Outputs the result as 'result = success' if check is passed or an error result if not
required: false
name:
required: false
description: Name of the check to validate against
default: Jenkins
outputs:
result:
value: ${{ steps.verify-ci-status.outputs.result }}
description: success if the check succeeded otherwise a message describing the result
runs:
using: composite
steps:
- run: GITHUB_TOKEN=${{ inputs.GITHUB_TOKEN }} NAME=${{ inputs.name }} $GITHUB_ACTION_PATH/run.sh
- run: GITHUB_TOKEN=${{ inputs.GITHUB_TOKEN }} OUTPUT_RESULT=${{ inputs.output_result }} NAME=${{ inputs.name }} $GITHUB_ACTION_PATH/run.sh
id: verify-ci-status
shell: bash
42 changes: 29 additions & 13 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
#!/bin/bash
set -euxo pipefail
set -euo pipefail

function outputResult() {
local result=$1
local exitCode=$2

echo $result

if [ $OUTPUT_RESULT = true ]; then
echo "::set-output name=result::$result"
exit 0
else
exit $exitCode
fi
}

if [ $GITHUB_EVENT_NAME = check_run ]
then
if [ "$(jq -r .check_run.name < $GITHUB_EVENT_PATH)" \!= "$NAME" ]
then
echo wrong check
exit 1
outputResult wrong-check 1
elif [ "$(jq -r .check_run.status < $GITHUB_EVENT_PATH)" \!= completed ]
then
echo not completed
exit 1
outputResult not-completed 1
elif [ "$(jq -r .check_run.conclusion < $GITHUB_EVENT_PATH)" \!= success ]
then
echo did not succeed
exit 1
outputResult check-failed 1
elif [ "$(jq -r .check_run.head_sha < $GITHUB_EVENT_PATH)" \!= $GITHUB_SHA ]
then
echo unexpected commit
exit 1
outputResult unexpected-commit 1
else
echo passing
outputResult success 0
fi
elif [ $GITHUB_EVENT_NAME = workflow_dispatch ]
then
gh api -X GET -F check_name="$NAME" -F status=completed /repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/check-runs | jq -e '.check_runs | .[] | select(.conclusion == "success") | .id'
ID=$(gh api -X GET -F check_name="$NAME" -F status=completed /repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/check-runs | jq -e '.check_runs | .[] | select(.conclusion == "success") | .id' || echo 'failed')

if [[ $ID != 'failed' ]]; then
outputResult success 0
else
outputResult failed 1
fi
else
echo unknown event type $GITHUB_EVENT_NAME
exit 1
outputResult unknown-event-type-$GITHUB_EVENT_NAME 1
fi

0 comments on commit 6dd04d9

Please sign in to comment.