|
| 1 | +name: Call Notify Internal Infrastructure Workflow |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + jobName: |
| 7 | + type: string |
| 8 | + description: The name of the job triggering the remote workflow |
| 9 | + required: true |
| 10 | + infraRepoName: |
| 11 | + type: string |
| 12 | + description: Repository (this one) in the NHSDigital Github Org to deploy from |
| 13 | + default: "" |
| 14 | + releaseVersion: |
| 15 | + type: string |
| 16 | + description: The Github release version, commit, or tag. |
| 17 | + default: main |
| 18 | + targetWorkflow: |
| 19 | + type: string |
| 20 | + description: The name of the github workflow to call. |
| 21 | + required: true |
| 22 | + targetEnvironment: |
| 23 | + type: string |
| 24 | + description: The Terraform environment to deploy |
| 25 | + default: main |
| 26 | + targetComponent: |
| 27 | + type: string |
| 28 | + description: The Terraform component to deploy |
| 29 | + required: true |
| 30 | + targetAccountGroup: |
| 31 | + type: string |
| 32 | + description: The Terraform group to deploy |
| 33 | + required: true |
| 34 | + terraformAction: |
| 35 | + type: string |
| 36 | + description: The Terraform action to run |
| 37 | + default: "" |
| 38 | + internalRef: |
| 39 | + type: string |
| 40 | + description: Internal repo reference (branch or tag) |
| 41 | + default: "main" |
| 42 | + |
| 43 | +permissions: |
| 44 | + id-token: write |
| 45 | + contents: read |
| 46 | + |
| 47 | +concurrency: |
| 48 | + group: ${{ inputs.targetEnvironment }}-${{ inputs.targetAccountGroup }}-${{ inputs.targetComponent }}-${{ inputs.terraformAction }} |
| 49 | + |
| 50 | +jobs: |
| 51 | + trigger: |
| 52 | + name: ${{ inputs.jobName }} |
| 53 | + runs-on: ubuntu-latest |
| 54 | + |
| 55 | + steps: |
| 56 | + - uses: actions/checkout@v4 |
| 57 | + |
| 58 | + - name: Trigger nhs-notify-internal workflow |
| 59 | + shell: bash |
| 60 | + run: | |
| 61 | + DISPATCH_EVENT=$(jq -ncM \ |
| 62 | + --arg infraRepoName "${{ inputs.infraRepoName }}" \ |
| 63 | + --arg releaseVersion ${{ inputs.releaseVersion }} \ |
| 64 | + --arg targetEnvironment ${{ inputs.targetEnvironment }} \ |
| 65 | + --arg targetAccountGroup ${{ inputs.targetAccountGroup }} \ |
| 66 | + --arg targetComponent ${{ inputs.targetComponent }} \ |
| 67 | + --arg terraformAction "${{ inputs.terraformAction }}" \ |
| 68 | + '{ |
| 69 | + "ref": "${{ inputs.internalRef }}", |
| 70 | + "inputs": ( |
| 71 | + (if $infraRepoName != "" then { "infraRepoName": $infraRepoName } else {} end) + |
| 72 | + (if $terraformAction != "" then { "terraformAction": $terraformAction } else {} end) + |
| 73 | + { |
| 74 | + "releaseVersion": $releaseVersion, |
| 75 | + "targetEnvironment": $targetEnvironment, |
| 76 | + "targetAccountGroup": $targetAccountGroup, |
| 77 | + "targetComponent": $targetComponent |
| 78 | + } |
| 79 | + ) |
| 80 | + }') |
| 81 | +
|
| 82 | + # Trigger The workflow |
| 83 | + curl -L \ |
| 84 | + --fail \ |
| 85 | + --silent \ |
| 86 | + -X POST \ |
| 87 | + -H "Accept: application/vnd.github+json" \ |
| 88 | + -H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \ |
| 89 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 90 | + "https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/${{ inputs.targetWorkflow }}/dispatches" \ |
| 91 | + -d "${DISPATCH_EVENT}" |
| 92 | +
|
| 93 | + echo "Workflow triggered. Waiting for the workflow to complete.." |
| 94 | +
|
| 95 | + # Poll GitHub API to check the workflow status |
| 96 | + workflow_run_url="" |
| 97 | + for i in {1..12}; do |
| 98 | + workflow_run_url=$(curl -s \ |
| 99 | + -H "Accept: application/vnd.github+json" \ |
| 100 | + -H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \ |
| 101 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 102 | + "https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs?event=workflow_dispatch&status=in_progress" \ |
| 103 | + | jq -r '.workflow_runs[] |
| 104 | + | select(.name |
| 105 | + | contains("'${{ inputs.targetEnvironment }}'") and |
| 106 | + contains("'${{ inputs.targetAccountGroup }}'") and |
| 107 | + contains("'${{ inputs.targetComponent }}'") and |
| 108 | + contains("'${{ inputs.terraformAction }}'")) |
| 109 | + | .url') |
| 110 | +
|
| 111 | + if [[ -n "$workflow_run_url" && "$workflow_run_url" != null ]]; then |
| 112 | + ui_url=${workflow_run_url/api./} |
| 113 | + ui_url=${ui_url/\/repos/} |
| 114 | + echo "Found workflow run url: $ui_url" |
| 115 | + echo "workflow_run_url=$workflow_run_url" >> $GITHUB_ENV |
| 116 | + break |
| 117 | + fi |
| 118 | +
|
| 119 | + echo "Waiting for workflow to start..." |
| 120 | + sleep 10 |
| 121 | + done |
| 122 | +
|
| 123 | + if [[ -z "$workflow_run_url" || "$workflow_run_url" == null ]]; then |
| 124 | + echo "Failed to get the workflow run url. Exiting." |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | +
|
| 128 | + # Wait for workflow completion |
| 129 | + while true; do |
| 130 | + sleep 10 |
| 131 | + response=$(curl -s -L \ |
| 132 | + -H "Authorization: Bearer ${{ secrets.PR_TRIGGER_PAT }}" \ |
| 133 | + -H "Accept: application/vnd.github+json" \ |
| 134 | + $workflow_run_url) |
| 135 | +
|
| 136 | + status=$(echo "$response" | jq -r '.status') |
| 137 | + conclusion=$(echo "$response" | jq -r '.conclusion') |
| 138 | +
|
| 139 | + if [ "$status" == "completed" ]; then |
| 140 | + if [ "$conclusion" == "success" ]; then |
| 141 | + echo "Workflow completed successfully." |
| 142 | + exit 0 |
| 143 | + else |
| 144 | + echo "Workflow failed with conclusion: $conclusion" |
| 145 | + exit 1 |
| 146 | + fi |
| 147 | + fi |
| 148 | +
|
| 149 | + echo "Workflow still running..." |
| 150 | + sleep 20 |
| 151 | + done |
0 commit comments