Skip to content

Commit

Permalink
feat: Reducing time taken when wait_workflow used (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steph0 authored Jun 17, 2021
1 parent 6acf163 commit b3813dc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 54 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ When deploying an app you may need to deploy additional services, this Github Ac

### Simple

```
```yaml
- uses: convictional/[email protected]
with:
owner: keithconvictional
Expand All @@ -37,7 +37,7 @@ When deploying an app you may need to deploy additional services, this Github Ac
### All Options
```
```yaml
- uses: convictional/[email protected]
with:
owner: keithconvictional
Expand All @@ -57,7 +57,7 @@ When deploying an app you may need to deploy additional services, this Github Ac
You can test out the action locally by cloning the repository to your computer. You can run:
```
```shell
INPUT_WAITING_INTERVAL=10 \
INPUT_PROPAGATE_FAILURE=false \
INPUT_TRIGGER_WORKFLOW=true \
Expand All @@ -72,7 +72,7 @@ INPUT_WAITING_INTERVAL=10 \

You will have to create a Github Personal access token. You can create a test workflow to be executed. In a repository, add a new `main.yml` to `.github/workflows/`. The workflow will be:

```
```shell
name: Main
on:
workflow_dispatch
Expand All @@ -88,7 +88,7 @@ jobs:

You can see the example [here](https://github.com/keithconvictional/trigger-workflow-and-wait-example-repo1/blob/master/.github/workflows/main.yml). For testing a failure case, just add this line after the sleep:

```
```yaml
...
- name: Pause for 25 seconds
run: |
Expand All @@ -97,19 +97,17 @@ You can see the example [here](https://github.com/keithconvictional/trigger-work
exit 1
```

## Potential Issues
### Timing
The actions dispatch is an asynchronous job and it at times can take a few seconds to start. If you do not have a delay, it may be started after the action has checked if it was successful. ie. Start dispatch call --> No delay --> Check if successful --> Actually starts. If the workflow has run before, it will just complete immediately as a successful run. You can solve this by simply increasing the delay to a few seconds. By default it is 10 seconds. Creating a large delay between checks will help the traffic to the Github API.

### Changes
If you do not want the latest build all of the time, please use a versioned copy of the Github Action. You specify the version after the `@` sign.

```
```yaml
- uses: convictional/[email protected]
with:
owner: keithconvictional
Expand Down
3 changes: 1 addition & 2 deletions contribute.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Contribute


```
```shell
docker build . -t trigger-workflow-and-wait
```
88 changes: 44 additions & 44 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function usage_docs {
#!/bin/sh

usage_docs() {
echo ""
echo "You can use this Github Action with:"
echo "- uses: convictional/trigger-workflow-and-wait"
Expand All @@ -9,46 +11,46 @@ function usage_docs {
echo " workflow_file_name: main.yaml"
}

function validate_args {
validate_args() {
wait_interval=10 # Waits for 10 seconds
if [ "$INPUT_WAITING_INTERVAL" ]
if [ "${INPUT_WAITING_INTERVAL}" ]
then
wait_interval=$INPUT_WAITING_INTERVAL
wait_interval=${INPUT_WAITING_INTERVAL}
fi

propagate_failure=true
if [ -n "$INPUT_PROPAGATE_FAILURE" ]
if [ -n "${INPUT_PROPAGATE_FAILURE}" ]
then
propagate_failure=$INPUT_PROPAGATE_FAILURE
propagate_failure=${INPUT_PROPAGATE_FAILURE}
fi

trigger_workflow=true
if [ -n "$INPUT_TRIGGER_WORKFLOW" ]
if [ -n "${INPUT_TRIGGER_WORKFLOW}" ]
then
trigger_workflow=$INPUT_TRIGGER_WORKFLOW
trigger_workflow=${INPUT_TRIGGER_WORKFLOW}
fi

wait_workflow=true
if [ -n "$INPUT_WAIT_WORKFLOW" ]
if [ -n "${INPUT_WAIT_WORKFLOW}" ]
then
wait_workflow=$INPUT_WAIT_WORKFLOW
wait_workflow=${INPUT_WAIT_WORKFLOW}
fi

if [ -z "$INPUT_OWNER" ]
if [ -z "${INPUT_OWNER}" ]
then
echo "Error: Owner is a required argument."
usage_docs
exit 1
fi

if [ -z "$INPUT_REPO" ]
if [ -z "${INPUT_REPO}" ]
then
echo "Error: Repo is a required argument."
usage_docs
exit 1
fi

if [ -z "$INPUT_GITHUB_TOKEN" ]
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"
Expand All @@ -57,87 +59,85 @@ function validate_args {
exit 1
fi

if [ -z $INPUT_WORKFLOW_FILE_NAME ]
if [ -z "${INPUT_WORKFLOW_FILE_NAME}" ]
then
echo "Error: Workflow File Name is required"
usage_docs
exit 1
fi

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

ref="main"
if [ "$INPUT_REF" ]
then
ref=$INPUT_REF
ref="${INPUT_REF}"
fi
}

function trigger_workflow {
echo "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/$INPUT_WORKFLOW_FILE_NAME/dispatches"
trigger_workflow() {
echo "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/dispatches"

curl -X POST "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/$INPUT_WORKFLOW_FILE_NAME/dispatches" \
curl -X POST "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/dispatches" \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \
--data "{\"ref\":\"${ref}\",\"inputs\":${inputs}}"
echo "Sleeping for $wait_interval seconds"
sleep $wait_interval
}

function wait_for_workflow_to_finish {
wait_for_workflow_to_finish() {
# Find the id of the last build
last_workflow=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/actions/workflows/$INPUT_WORKFLOW_FILE_NAME/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 '[.workflow_runs[]] | first')
last_workflow_id=$(echo $last_workflow | jq '.id')
echo "The workflow id is [$last_workflow_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=$(echo $last_workflow | jq '.conclusion')
status=$(echo $last_workflow | jq '.status')
conclusion=$(echo "${last_workflow}" | jq '.conclusion')
status=$(echo "${last_workflow}" | jq '.status')

while [[ $conclusion == "null" && $status != "\"completed\"" ]]
while [[ "${conclusion}" == "null" && "${status}" != "\"completed\"" ]]
do
echo "Sleeping for $wait_interval seconds"
sleep $wait_interval
workflow=$(curl -X GET "https://api.github.com/repos/$INPUT_OWNER/$INPUT_REPO/actions/workflows/$INPUT_WORKFLOW_FILE_NAME/runs" \
echo "Sleeping for \"${wait_interval}\" seconds"
sleep "${wait_interval}"
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]"
-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\"" && $status == "\"completed\"" ]]
if [[ "${conclusion}" == "\"success\"" && "${status}" == "\"completed\"" ]]
then
echo "Yes, success"
else
# Alternative "failure"
echo "Conclusion is not success, its [$conclusion]."
if [ "$propagate_failure" = true ]
echo "Conclusion is not success, its [${conclusion}]."
if [ "${propagate_failure}" = true ]
then
echo "Propagating failure to upstream job"
exit 1
fi
fi
}

function main {
main() {
validate_args

if [ "$trigger_workflow" = true ]
if [ "${trigger_workflow}" = true ]
then
trigger_workflow
else
echo "Skipping triggering the workflow."
fi

if [ "$wait_workflow" = true ]
if [ "${wait_workflow}" = true ]
then
wait_for_workflow_to_finish
else
Expand Down

0 comments on commit b3813dc

Please sign in to comment.