Skip to content

Commit

Permalink
add client_payload and change job_id to workflow_id and take always c…
Browse files Browse the repository at this point in the history
…are of right job (#3)

Co-authored-by: Jonas Büttner <[email protected]>
  • Loading branch information
keithconvictional and Jonas Büttner authored Dec 2, 2020
1 parent 1b91bfd commit 7369669
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
TEMP.md
.idea
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ When deploying an app you may need to deploy additional services, this Github Ac

## 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. |
| 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. |
| `workflow_file_name` | True | N/A | The reference point. For example, you could use main.yml. |
| `client_payload` | False | `{}` | JSON payload with extra information about the webhook event that your action or worklow may use. |


## Example
Expand Down
7 changes: 5 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ inputs:
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."
workflow_file_name:
description: "The reference point. For example, you could use main.yml."
required: true
client_payload:
description: "JSON payload with extra information about the webhook event that your action or worklow may use. Default: {}"
required: false
runs:
using: 'docker'
Expand Down
41 changes: 27 additions & 14 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ function usage_docs {
echo " owner: keithconvictional"
echo " repo: myrepo"
echo " github_token: \${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}"
echo " workflow_file_name: main.yaml"
}

# TODO - Add client_payload

function validate_args {
wait_interval=10
if [ "$INPUT_WAITING_INTERVAL" ]
Expand Down Expand Up @@ -46,11 +45,18 @@ function validate_args {
event_type=$INPUT_EVENT_TYPE
fi

ref="master"
if [ $INPUT_REF ]
if [ -z $INPUT_WORKFLOW_FILE_NAME ]
then
ref=$INPUT_REF
fi
echo "Error: Workflow File Name is required"
usage_docs
exit 1
fi

client_payload=$(echo '{}' | jq)
if [ "$INPUT_CLIENT_PAYLOAD" ]
then
client_payload=$(echo $INPUT_CLIENT_PAYLOAD | jq)
fi
}

function trigger_workflow {
Expand All @@ -59,27 +65,34 @@ function trigger_workflow {
-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\": {} }"
--data "{\"event_type\": \"${event_type}\", \"client_payload\": $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" \
last_workflow=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/actions/workflows/$INPUT_WORKFLOW_FILE_NAME/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]."
-H "Authorization: Bearer $INPUT_GITHUB_TOKEN" | jq '[.workflow_runs[]] | first')
last_workflow_id=$(echo $last_workflow | jq '.id')
echo "The workflow id is [$last_workflow_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')
conclusion=$(echo $last_workflow | jq '.conclusion')
status=$(echo $last_workflow | jq '.status')

while [[ $conclusion == "null" ]]
while [[ $conclusion == "null" && $status != "\"completed\"" ]]
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')
workflow=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/actions/workflows/$INPUT_WORKFLOW_FILE_NAME/runs" \
-H 'Accept: application/vnd.github.antiope-preview+json' \
-H "Authorization: Bearer $INPUT_GITHUB_TOKEN" | jq '.workflow_runs[] | select(.id == '$last_workflow_id')')
conclusion=$(echo $workflow | jq '.conclusion')
status=$(echo $workflow | jq '.status')
echo "Checking conclusion [$conclusion]"
echo "Checking status [$status]"
done

if [[ $conclusion == "\"success\"" ]]
if [[ $conclusion == "\"success\"" && $status == "\"completed\"" ]]
then
echo "Yes, success"
else
Expand Down

0 comments on commit 7369669

Please sign in to comment.