Skip to content

Commit

Permalink
Introduce actions\spring-dispatch-workflow-and-wait
Browse files Browse the repository at this point in the history
* Modify `test-call-workflow-and-wait.yml` to test this action
* Add an optional input for the `test-other-workflow-to-call.yml`
  • Loading branch information
artembilan committed Aug 27, 2024
1 parent 2fba991 commit a01a65d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 36 deletions.
66 changes: 66 additions & 0 deletions .github/actions/spring-dispatch-workflow-and-wait/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Dispatch another workflow and wait for its completion

description: 'Dispatch another workflow and wait for its completion'

inputs:
workflow:
description: 'The workflow YAML file name to call'
required: true
token:
description: 'A GitHub token for gh api calls'
required: true
inputs:
description: 'The target workflow input in JSON format'
required: false

runs:
using: composite
steps:

- uses: actions/checkout@v4
with:
show-progress: false

- name: Dispatch workflow and wait
shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
run: |
dispatchDate=$(date -Iseconds)
workflowFile=${{ inputs.workflow }}
if [ '${{ inputs.inputs }}' ]; then
echo '${{ inputs.inputs }}' | gh workflow run $workflowFile --json
else
gh workflow run $workflowFile
fi
sleep 60
run_data=$(gh api repos/$GITHUB_REPOSITORY/actions/workflows/$workflowFile/runs?event=workflow_dispatch&created=>=$dispatchDate)
currentWorkflowRunId=$(echo "$run_data" | jq -r '.workflow_runs[0].id')
run_data=$(gh api repos/$GITHUB_REPOSITORY/actions/runs/$currentWorkflowRunId)
currentWorkflowRunUrl=$(echo "$run_data" | jq -r '.html_url')
echo "::notice title=Dispatched workflow run::$currentWorkflowRunUrl"
while true; do
run_data=$(gh api repos/$GITHUB_REPOSITORY/actions/runs/$currentWorkflowRunId)
status=$(echo "$run_data" | jq -r '.status')
if [ $status = "completed" ]; then
conclusion=$(echo "$run_data" | jq -r '.conclusion')
if [ $conclusion != "success" ]; then
echo "::error file=$workflowFile::The Dispatched workflow has not completed successfully"
exit 1
else
echo "::notice file=$workflowFile::The Dispatched workflow completed successfully"
break
fi
fi
sleep 60
done
57 changes: 22 additions & 35 deletions .github/workflows/test-call-workflow-and-wait.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ on:
description: 'Other workflow'
required: true
type: string
anInput:
description: 'Some input'
required: false
type: string

env:
WORKFLOWS_REF: main

jobs:
call-other-workflow-and-wait:
Expand All @@ -15,45 +22,25 @@ jobs:
- uses: actions/checkout@v4
with:
show-progress: false
- name: Call tests workflow
timeout-minutes: 30
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
dispatchDate=$(date -Iseconds)
workflowFile=${{ inputs.otherWorkflow }}
gh workflow run $workflowFile
sleep 60
run_data=$(gh api repos/$GITHUB_REPOSITORY/actions/workflows/$workflowFile/runs?event=workflow_dispatch&created=>=$dispatchDate)
currentWorkflowRunId=$(echo "$run_data" | jq -r '.workflow_runs[0].id')
run_data=$(gh api repos/$GITHUB_REPOSITORY/actions/runs/$currentWorkflowRunId)
echo Other workflow current run: $(echo "$run_data" | jq -r '.html_url')

while true; do
run_data=$(gh api repos/$GITHUB_REPOSITORY/actions/runs/$currentWorkflowRunId)
status=$(echo "$run_data" | jq -r '.status')
if [ $status = "completed" ]; then
conclusion=$(echo "$run_data" | jq -r '.conclusion')
if [ $conclusion != "success" ]; then
echo "❌ The other workflow has not completed successfully"
exit 1
else
echo "✅ The other workflow completed successfully"
break
fi
fi
- name: Checkout Common Repo
uses: actions/checkout@v4
with:
repository: spring-io/spring-github-workflows
path: spring-github-workflows
show-progress: false
ref: ${{ env.WORKFLOWS_REF }}

sleep 60
done
- name: Build and Publish
timeout-minutes: 30
uses: ./spring-github-workflows/.github/actions/spring-dispatch-workflow-and-wait
with:
workflow: ${{ inputs.otherWorkflow }}
token: ${{ secrets.GITHUB_TOKEN }}
inputs: '{ "anInput": "${{ inputs.anInput }}" }'

next-job:
needs: call-other-workflow-and-wait
runs-on: ubuntu-latest
steps:
- run: echo " The main workflow completed successfully"
- run: echo "::notice The main workflow completed successfully"
6 changes: 5 additions & 1 deletion .github/workflows/test-other-workflow-to-call.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ name: Test workflow to be called from another

on:
workflow_dispatch:
inputs:
anInput:
description: Some input
required: false

jobs:
other-workflow:
runs-on: ubuntu-latest
steps:
- run: |
sleep 60
echo " The other workflow is done"
echo "::notice The other workflow is done for ${{ inputs.anInput }}"

0 comments on commit a01a65d

Please sign in to comment.