diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99dce8e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +TEMP.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d8d790b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +# https://hub.docker.com/_/alpine +FROM alpine:latest + +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["sh", "/entrypoint.sh"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c795648 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Convictional, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a2f06f0 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Trigger Workflow and Wait + +Github Action for trigger a workflow from another workflow. The action then waits for a response. + +**When would you use it?** + +When deploying an app you may need to deploy additional services, this Github Action helps with that. + + +## Arguments + +| Argument Name | Required | Default | Description | +| --------------- | ---------- | ----------- | --------------------- | +| `owner` | True | N/A | The owner of the repository where the workflow is contained. | +| `repo` | True | N/A | The repository where the workflow is contained. | +| `github_token` | True | N/A | The Github access token with access to the repository. Its recommended you put it under secrets. | +| `wait_interval` | False | 10 | The number of seconds delay between checking for result of run. | +| `event_type` | False | `ping` | The event type that is trigger your workflow on the secondary repository. | +| `ref` | False | `master` | The reference point. This is either a commit or branch. | + + +## Example + +``` +- uses: convictional/trigger-workflow-and-wait + with:" + owner: keithconvictional + repo: myrepo + github_token: ${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }} +``` diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..f6bb892 --- /dev/null +++ b/action.yml @@ -0,0 +1,28 @@ +name: 'Trigger Workflow and Wait' +description: 'This action triggers a workflow in another repository and waits for the result.' +author: 'Convictional' +branding: + icon: 'arrow-right' + color: 'yellow' +inputs: + owner: + description: "The owner of the repository where the workflow is contained." + required: true + repo: + description: "The repository where the workflow is contained." + required: true + github_token: + description: "The Github access token with access to the repository. Its recommended you put it under secrets." + required: true + wait_interval: + description: "The number of seconds delay between checking for result of run." + required: false + event_type: + description: "The event type that is trigger your workflow on the secondary repository." + required: false + ref: + description: "The reference point. This is either a commit or branch." + required: false +runs: + using: 'docker' + image: 'Dockerfile' diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..1203a9a --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,98 @@ +function usage_docs { + echo "" + echo "You can use this Github Action with:" + echo "- uses: convictional/trigger-workflow-and-wait" + echo " with:" + echo " owner: keithconvictional" + echo " repo: myrepo" + echo " github_token: \${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}" +} + +# TODO - Add client_payload + +function validate_args { + wait_interval=10 + if [ "$INPUT_WAITING_INTERVAL" ] + then + wait_interval=$INPUT_WAITING_INTERVAL + fi + + if [ -z "$INPUT_OWNER" ] + then + echo "Error: Owner is a required arugment." + usage_docs + exit 1 + fi + + if [ -z "$INPUT_REPO" ] + then + echo "Error: Repo is a required arugment." + usage_docs + exit 1 + fi + + if [ -z "$INPUT_GITHUB_TOKEN" ] + then + echo "Error: Github token is required. You can head over settings and" + echo "under developer, you can create a personal access tokens. The" + echo "token requires repo access." + usage_docs + exit 1 + fi + + event_type="ping" + if [ "$INPUT_EVENT_TYPE" ] + then + event_type=$INPUT_EVENT_TYPE + fi + + ref="master" + if [ $INPUT_REF ] + then + ref=$INPUT_REF + fi +} + +function trigger_workflow { + echo "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/dispatches" + curl -X POST "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/dispatches" \ + -H "Accept: application/vnd.github.everest-preview+json" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \ + --data "{\"event_type\": \"${event_type}\", \"client_payload\": {} }" + sleep $wait_interval +} + +function wait_for_workflow_to_finish { + # Find the id of the last build + last_run_id=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/commits/$ref/check-runs" \ + -H 'Accept: application/vnd.github.antiope-preview+json' \ + -H "Authorization: Bearer $INPUT_GITHUB_TOKEN" | jq '[.check_runs[].id] | first') + echo "The job id is [$last_run_id]." + echo "" + conclusion=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/check-runs/$last_run_id" -H 'Accept: application/vnd.github.antiope-preview+json' -H "Authorization: Bearer $INPUT_GITHUB_TOKEN" | jq '.conclusion') + + while [[ $conclusion == "null" ]] + do + sleep $wait_interval + conclusion=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/check-runs/$last_run_id" -H 'Accept: application/vnd.github.antiope-preview+json' -H "Authorization: Bearer $INPUT_GITHUB_TOKEN" | jq '.conclusion') + echo "Checking conclusion [$conclusion]" + done + + if [[ $conclusion == "\"success\"" ]] + then + echo "Yes, success" + else + # Alternative "failure" + echo "Conclusion is not success, its [$conclusion]." + exit 1 + fi +} + +function main { + validate_args + trigger_workflow + wait_for_workflow_to_finish +} + +main