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