Skip to content

Commit

Permalink
action: add option to post the workflow URL (#57)
Browse files Browse the repository at this point in the history
as a comment to a configurable comments API URL.
For instance when having very long running workflows called by the
action, it's hard for the user to see the actual pipeline and its
results.
For better tracebility and user expierence post the link to
the acquired workflow URL as a comment.

Typical setting would be to set it to
${{ github.event.pull_request.comments_url }} for pull requests.

As this may not be wanted by all users set the default to empty,
meaning no comments will be made
  • Loading branch information
kwavnet committed Oct 8, 2022
1 parent 2fcaa46 commit dc8fc82
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 |


## Example
Expand Down Expand Up @@ -54,6 +55,16 @@ 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: convictional/trigger-workflow-and-wait@v1.6.1
with:
owner: keithconvictional
repo: myrepo
github_token: ${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}
comment_downstream_url: ${{ github.event.pull_request.comments_url }}
```

## Testing

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ inputs:
wait_workflow:
description: 'Wait for workflow to finish. default: true'
required: false
comment_downstream_url:
description: 'Comment API link for commenting the downstream job URL'
required: false
default: ''
outputs:
workflow_id:
description: The ID of the workflow that was triggered by this action
Expand Down
17 changes: 17 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ 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_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}"
Expand All @@ -159,6 +172,10 @@ wait_for_workflow_to_finish() {
echo "::set-output name=workflow_url::${last_workflow_url}"
echo ""

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

conclusion=null
status=

Expand Down

0 comments on commit dc8fc82

Please sign in to comment.