Skip to content

Commit 7369669

Browse files
keithconvictionalJonas Büttner
andauthored
add client_payload and change job_id to workflow_id and take always care of right job (#3)
Co-authored-by: Jonas Büttner <[email protected]>
1 parent 1b91bfd commit 7369669

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
TEMP.md
3+
.idea

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ When deploying an app you may need to deploy additional services, this Github Ac
99

1010
## Arguments
1111

12-
| Argument Name | Required | Default | Description |
13-
| --------------- | ---------- | ----------- | --------------------- |
14-
| `owner` | True | N/A | The owner of the repository where the workflow is contained. |
15-
| `repo` | True | N/A | The repository where the workflow is contained. |
16-
| `github_token` | True | N/A | The Github access token with access to the repository. Its recommended you put it under secrets. |
17-
| `wait_interval` | False | 10 | The number of seconds delay between checking for result of run. |
18-
| `event_type` | False | `ping` | The event type that is trigger your workflow on the secondary repository. |
19-
| `ref` | False | `master` | The reference point. This is either a commit or branch. |
12+
| Argument Name | Required | Default | Description |
13+
| --------------------- | ---------- | ----------- | --------------------- |
14+
| `owner` | True | N/A | The owner of the repository where the workflow is contained. |
15+
| `repo` | True | N/A | The repository where the workflow is contained. |
16+
| `github_token` | True | N/A | The Github access token with access to the repository. Its recommended you put it under secrets. |
17+
| `wait_interval` | False | 10 | The number of seconds delay between checking for result of run. |
18+
| `event_type` | False | `ping` | The event type that is trigger your workflow on the secondary repository. |
19+
| `workflow_file_name` | True | N/A | The reference point. For example, you could use main.yml. |
20+
| `client_payload` | False | `{}` | JSON payload with extra information about the webhook event that your action or worklow may use. |
2021

2122

2223
## Example

action.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ inputs:
2020
event_type:
2121
description: "The event type that is trigger your workflow on the secondary repository."
2222
required: false
23-
ref:
24-
description: "The reference point. This is either a commit or branch."
23+
workflow_file_name:
24+
description: "The reference point. For example, you could use main.yml."
25+
required: true
26+
client_payload:
27+
description: "JSON payload with extra information about the webhook event that your action or worklow may use. Default: {}"
2528
required: false
2629
runs:
2730
using: 'docker'

entrypoint.sh

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ function usage_docs {
66
echo " owner: keithconvictional"
77
echo " repo: myrepo"
88
echo " github_token: \${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}"
9+
echo " workflow_file_name: main.yaml"
910
}
1011

11-
# TODO - Add client_payload
12-
1312
function validate_args {
1413
wait_interval=10
1514
if [ "$INPUT_WAITING_INTERVAL" ]
@@ -46,11 +45,18 @@ function validate_args {
4645
event_type=$INPUT_EVENT_TYPE
4746
fi
4847

49-
ref="master"
50-
if [ $INPUT_REF ]
48+
if [ -z $INPUT_WORKFLOW_FILE_NAME ]
5149
then
52-
ref=$INPUT_REF
53-
fi
50+
echo "Error: Workflow File Name is required"
51+
usage_docs
52+
exit 1
53+
fi
54+
55+
client_payload=$(echo '{}' | jq)
56+
if [ "$INPUT_CLIENT_PAYLOAD" ]
57+
then
58+
client_payload=$(echo $INPUT_CLIENT_PAYLOAD | jq)
59+
fi
5460
}
5561

5662
function trigger_workflow {
@@ -59,27 +65,34 @@ function trigger_workflow {
5965
-H "Accept: application/vnd.github.everest-preview+json" \
6066
-H "Content-Type: application/json" \
6167
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \
62-
--data "{\"event_type\": \"${event_type}\", \"client_payload\": {} }"
68+
--data "{\"event_type\": \"${event_type}\", \"client_payload\": $client_payload }"
6369
sleep $wait_interval
6470
}
6571

6672
function wait_for_workflow_to_finish {
6773
# Find the id of the last build
68-
last_run_id=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/commits/$ref/check-runs" \
74+
last_workflow=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/actions/workflows/$INPUT_WORKFLOW_FILE_NAME/runs" \
6975
-H 'Accept: application/vnd.github.antiope-preview+json' \
70-
-H "Authorization: Bearer $INPUT_GITHUB_TOKEN" | jq '[.check_runs[].id] | first')
71-
echo "The job id is [$last_run_id]."
76+
-H "Authorization: Bearer $INPUT_GITHUB_TOKEN" | jq '[.workflow_runs[]] | first')
77+
last_workflow_id=$(echo $last_workflow | jq '.id')
78+
echo "The workflow id is [$last_workflow_id]."
7279
echo ""
73-
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')
80+
conclusion=$(echo $last_workflow | jq '.conclusion')
81+
status=$(echo $last_workflow | jq '.status')
7482

75-
while [[ $conclusion == "null" ]]
83+
while [[ $conclusion == "null" && $status != "\"completed\"" ]]
7684
do
7785
sleep $wait_interval
78-
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')
86+
workflow=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/actions/workflows/$INPUT_WORKFLOW_FILE_NAME/runs" \
87+
-H 'Accept: application/vnd.github.antiope-preview+json' \
88+
-H "Authorization: Bearer $INPUT_GITHUB_TOKEN" | jq '.workflow_runs[] | select(.id == '$last_workflow_id')')
89+
conclusion=$(echo $workflow | jq '.conclusion')
90+
status=$(echo $workflow | jq '.status')
7991
echo "Checking conclusion [$conclusion]"
92+
echo "Checking status [$status]"
8093
done
8194

82-
if [[ $conclusion == "\"success\"" ]]
95+
if [[ $conclusion == "\"success\"" && $status == "\"completed\"" ]]
8396
then
8497
echo "Yes, success"
8598
else

0 commit comments

Comments
 (0)