Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Github Action for trigger a workflow from another workflow. The action then waits for a response.
see: <https://github.com/datavisyn/github-action-trigger-workflow/blob/main/README.md>

see also <https://github.com/convictional/github-action-trigger-workflow>
see also <https://github.com/convictional/trigger-workflow-and-wait>
based on <https://github.com/convictional/github-action-trigger-workflow>

**When would you use it?**

Expand All @@ -24,6 +25,16 @@ When deploying an app you may need to deploy additional services, this Github Ac
| `propagate_failure` | False | `true` | Fail current job if downstream job fails. |
| `trigger_workflow` | False | `true` | Trigger the specified workflow. |
| `wait_workflow` | False | `true` | Wait for workflow to finish. |
| `comment_downstream_url` | False | `` | A comments API URL to comment the current downstream job URL to. Default: no comment |
| `comment_github_token` | False | `${{github.token}}` | token used for pull_request comments |

## Outputs

| Output Name | Description |
| ------------- | --------------------- |
| `workflow_id` | The ID of the workflow that was triggered by this action |
| `workflow_url` | The URL of the workflow that was triggered by this action |
| `conclusion` | The conclusion of the workflow that was triggered by this action |

## Example

Expand Down Expand Up @@ -55,6 +66,17 @@ When deploying an app you may need to deploy additional services, this Github Ac
wait_workflow: true
```

### Comment the current running workflow URL for a PR

```yaml
- uses: ./.github/actions/github-action-trigger-workflow
with:
owner: datavisyn
repo: myrepo
github_token: ${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}
comment_downstream_url: ${{ github.event.pull_request.comments_url }}
```

## Testing

You can test out the action locally by cloning the repository to your computer. You can run:
Expand Down Expand Up @@ -90,7 +112,7 @@ jobs:
sleep 25
```

For testing a failure case, just add this line after the sleep:
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
...
Expand All @@ -100,6 +122,8 @@ For testing a failure case, just add this line after the sleep:
echo "For testing failure"
exit 1
```
exit 1
```

## Potential Issues

Expand Down
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ inputs:
wait_workflow:
description: 'Wait for workflow to finish. default: true'
required: false
comment_downstream_url:
description: 'A comments API URL to comment the current downstream job URL to. Default: no comment'
required: false
comment_github_token:
description: 'token used for pull_request comments'
required: false
default: ${{github.token}}
outputs:
workflow_id:
description: The ID of the workflow that was triggered by this action
workflow_url:
description: The URL of the workflow that was triggered by this action
conclusion:
description: The conclusion of the workflow that was triggered by this action
runs:
using: 'docker'
image: 'Dockerfile'
36 changes: 28 additions & 8 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ validate_args() {
}

lets_wait() {
echo "Sleeping for ${wait_interval} seconds"
sleep "$wait_interval"
local interval=${1:-$wait_interval}
echo >&2 "Sleeping for $interval seconds"
sleep "$interval"
}

api() {
Expand All @@ -101,7 +102,11 @@ api() {
echo >&2 "api failed:"
echo >&2 "path: $path"
echo >&2 "response: $response"
exit 1
if [[ "$response" == *'"Server Error"'* ]]; then
echo "Server error - trying again"
else
exit 1
fi
fi
}

Expand Down Expand Up @@ -142,17 +147,34 @@ trigger_workflow() {
join -v2 <(echo "$OLD_RUNS") <(echo "$NEW_RUNS")
}

comment_downstream_link() {
if response=$(curl --fail-with-body -sSL -X POST \
"${INPUT_COMMENT_DOWNSTREAM_URL}" \
-H "Authorization: Bearer ${INPUT_COMMENT_GITHUB_TOKEN}" \
-H 'Accept: application/vnd.github.v3+json' \
-d "{\"body\": \"Running downstream job at $1\"}")
then
echo "$response"
else
echo >&2 "failed to comment to ${INPUT_COMMENT_DOWNSTREAM_URL}:"
fi
}

wait_for_workflow_to_finish() {
last_workflow_id=${1:?}
last_workflow_url="${GITHUB_SERVER_URL}/${INPUT_OWNER}/${INPUT_REPO}/actions/runs/${last_workflow_id}"

echo "Waiting for workflow to finish:"
echo "The workflow id is [${last_workflow_id}]."
echo "The workflow logs can be found at ${last_workflow_url}"
echo "workflow_id=$last_workflow_id" >> "$GITHUB_OUTPUT"
echo "workflow_url=$last_workflow_url" >> "$GITHUB_OUTPUT"
echo "workflow_id=${last_workflow_id}" >> $GITHUB_OUTPUT
echo "workflow_url=${last_workflow_url}" >> $GITHUB_OUTPUT
echo ""

if [ -n "${INPUT_COMMENT_DOWNSTREAM_URL}" ]; then
comment_downstream_link ${last_workflow_url}
fi

conclusion=null
status=

Expand All @@ -166,6 +188,7 @@ wait_for_workflow_to_finish() {

echo "Checking conclusion [${conclusion}]"
echo "Checking status [${status}]"
echo "conclusion=${conclusion}" >> $GITHUB_OUTPUT
done

if [[ "${conclusion}" == "success" && "${status}" == "completed" ]]
Expand Down Expand Up @@ -203,8 +226,5 @@ main() {
echo "Skipping waiting for workflow."
fi
}
echo "${GITHUB_API_URL}"
echo "${INPUT_OWNER}"
echo "${INPUT_REPO}"

main