From 06e6b80279dbe8e8fbbcf68abd71db485a6c33e7 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Wed, 8 Apr 2020 16:55:41 +1000 Subject: [PATCH] Improve log_url by requiring context data to be specified (#3) * Add required run_id input to improve log_url * Add deployment_status_url input * Make status input required Closes #3 --- README.md | 12 +++++++++--- action.yml | 5 +++++ entrypoint.sh | 31 ++++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c7d1a48..c416ca3 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,12 @@ This action lets you easily update status of a Deployment on GitHub. Learn more | Name | Required | Default value | Description | |-----------------|----------|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| status | no | `in_progress` | Desired status of the Deployment. Can be one of `error`, `failure`, `inactive`, `in_progress`, `queued`, `pending` or `success` | +| run_id | **yes** | | Set this to `${{ github.run_id }}` so the deployment status log URL can be properly set. | +| status | **yes** | | Desired status of the Deployment. Can be one of `error`, `failure`, `inactive`, `in_progress`, `queued`, `pending` or `success` | | description | no | | A short description of the status. The maximum description length is 140 characters. | | auto_inactive | no | true | Adds a new inactive status to all prior non-transient, non-production environment deployments with the same repository and environment name as the created status's deployment. An inactive status is only added to deployments that had a success state. | | environment_url | no | | Sets the URL for accessing your environment. | +| deployment_status_url | no | (loaded from event) | Explicitly specify the deployment status URL to post the deployment status to. Normally, this will be auto-detected from the GitHub Actions [`DeploymentEvent`](https://developer.github.com/v3/activity/events/types/#deploymentevent) payload. However, if you are runnning from another event type or want to override the deployment to update, you can manually specify the full `https://api.github.com/repos/:owner/:repo/deployments/:deployment_id/statuses` URL here. | ## Action outputs @@ -38,7 +40,10 @@ jobs: - uses: actions/checkout@v1 - id: set_state_in_progress name: Set deployment status to [in_progress] - uses: rsotnychenko/deployment-status-update@0.1.3 + uses: rsotnychenko/deployment-status-update@0.2.0 + with: + run_id: ${{ github.run_id }} + status: in_progress env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Deploy to GAE @@ -51,9 +56,10 @@ jobs: - id: set_state_final if: always() name: Set deployment status - uses: rsotnychenko/deployment-status-update@0.1.3 + uses: rsotnychenko/deployment-status-update@0.2.0 with: status: ${{ job.status }} + run_id: ${{ github.run_id }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # TODO: Add rollback operations diff --git a/action.yml b/action.yml index bdd62e8..fd40329 100644 --- a/action.yml +++ b/action.yml @@ -5,6 +5,9 @@ branding: icon: 'rss' color: 'gray-dark' inputs: + run_id: + description: 'Set this to `github.run_id` so the deployment status log URL can be properly set.' + required: true status: description: 'desired deployment status' required: true @@ -17,6 +20,8 @@ inputs: environment_url: default: '' description: 'Sets the URL for accessing your environment' + deployment_status_url: + description: Override the default deployment status URL. outputs: deployment_id: description: 'An ID of the deployment in GitHub' diff --git a/entrypoint.sh b/entrypoint.sh index 17b26fc..cc98ca9 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,22 +1,35 @@ #!/bin/ash -xe -if [ "${GITHUB_EVENT_NAME}" != "deployment" ]; then - echo "Expected GITHUB_EVENT_NAME=deployment, got [${GITHUB_EVENT_NAME}]" - exit 1 -fi - get_from_event() { jq -r "$1" "${GITHUB_EVENT_PATH}" } -GITHUB_API_DEPLOYMENTS_URL="$(get_from_event '.deployment.statuses_url')" -GITHUB_ACTIONS_URL="$(get_from_event '.repository.html_url')/actions" +# Test required inputs are set +if [ -z "${INPUT_STATUS}" ]; then + echo "Missing input status" + exit 1 +fi +if [ -z "${INPUT_RUN_ID:-}" ]; then + echo "Missing input run_id" + exit 1 +fi +if [ -n "${INPUT_DEPLOYMENT_STATUS_URL:-}" ]; then + GITHUB_API_DEPLOYMENTS_URL=$INPUT_DEPLOYMENT_STATUS_URL +else + GITHUB_API_DEPLOYMENTS_URL="$(get_from_event '.deployment.statuses_url')" + if [ "$GITHUB_API_DEPLOYMENTS_URL" = "null" ]; then + echo "Couldn't detect deployment URL from the GitHub Actions workflow event. If you aren't running from a 'deployment' event, you must set the 'deployment_status_url' input." + exit 1 + fi +fi + +# Set variables +GITHUB_ACTIONS_RUN_URL="$(get_from_event '.repository.html_url')/actions/runs/$INPUT_RUN_ID" INPUT_STATUS=$(echo "$INPUT_STATUS" | tr '[:upper:]' '[:lower:]') if [ "$INPUT_STATUS" = cancelled ] ; then echo "Rewriting status from cancelled to error" INPUT_STATUS=error fi -: ${INPUT_STATUS:=in_progress} # set default curl --fail \ -X POST "${GITHUB_API_DEPLOYMENTS_URL}" \ @@ -26,7 +39,7 @@ curl --fail \ -d @- <