|
30 | 30 | set -e |
31 | 31 |
|
32 | 32 | while [[ $# -gt 0 ]]; do |
33 | | - case $1 in |
34 | | - --jobName) # Name of the job triggering the remote workflow (optional) |
35 | | - jobName="$2" |
36 | | - shift 2 |
37 | | - ;; |
38 | | - --infraRepoName) # Name of the infrastructure repo in NHSDigital org (required) |
39 | | - infraRepoName="$2" |
40 | | - shift 2 |
41 | | - ;; |
42 | | - --releaseVersion) # Release version, commit, or tag to deploy (required) |
43 | | - releaseVersion="$2" |
44 | | - shift 2 |
45 | | - ;; |
46 | | - --targetWorkflow) # Name of the workflow file to call in nhs-notify-internal (required) |
47 | | - targetWorkflow="$2" |
48 | | - shift 2 |
49 | | - ;; |
50 | | - --targetEnvironment) # Terraform environment to deploy (required) |
51 | | - targetEnvironment="$2" |
52 | | - shift 2 |
53 | | - ;; |
54 | | - --targetComponent) # Terraform component to deploy (required) |
55 | | - targetComponent="$2" |
56 | | - shift 2 |
57 | | - ;; |
58 | | - --targetAccountGroup) # Terraform account group to deploy (required) |
59 | | - targetAccountGroup="$2" |
60 | | - shift 2 |
61 | | - ;; |
62 | | - --terraformAction) # Terraform action to run (optional, default: "plan") |
63 | | - terraformAction="$2" |
64 | | - shift 2 |
65 | | - ;; |
66 | | - --internalRef) # Internal repo reference branch or tag (optional, default: "main") |
67 | | - internalRef="$2" |
68 | | - shift 2 |
69 | | - ;; |
70 | | - *) |
71 | | - echo "Unknown argument: $1" |
72 | | - exit 1 |
73 | | - ;; |
74 | | - esac |
| 33 | + case $1 in |
| 34 | + --jobName) # Name of the job triggering the remote workflow (optional) |
| 35 | + jobName="$2" |
| 36 | + shift 2 |
| 37 | + ;; |
| 38 | + --infraRepoName) # Name of the infrastructure repo in NHSDigital org (required) |
| 39 | + infraRepoName="$2" |
| 40 | + shift 2 |
| 41 | + ;; |
| 42 | + --releaseVersion) # Release version, commit, or tag to deploy (required) |
| 43 | + releaseVersion="$2" |
| 44 | + shift 2 |
| 45 | + ;; |
| 46 | + --targetWorkflow) # Name of the workflow file to call in nhs-notify-internal (required) |
| 47 | + targetWorkflow="$2" |
| 48 | + shift 2 |
| 49 | + ;; |
| 50 | + --targetEnvironment) # Terraform environment to deploy (required) |
| 51 | + targetEnvironment="$2" |
| 52 | + shift 2 |
| 53 | + ;; |
| 54 | + --targetComponent) # Terraform component to deploy (required) |
| 55 | + targetComponent="$2" |
| 56 | + shift 2 |
| 57 | + ;; |
| 58 | + --targetAccountGroup) # Terraform account group to deploy (required) |
| 59 | + targetAccountGroup="$2" |
| 60 | + shift 2 |
| 61 | + ;; |
| 62 | + --terraformAction) # Terraform action to run (optional, default: "plan") |
| 63 | + terraformAction="$2" |
| 64 | + shift 2 |
| 65 | + ;; |
| 66 | + --internalRef) # Internal repo reference branch or tag (optional, default: "main") |
| 67 | + internalRef="$2" |
| 68 | + shift 2 |
| 69 | + ;; |
| 70 | + *) |
| 71 | + echo "Unknown argument: $1" |
| 72 | + exit 1 |
| 73 | + ;; |
| 74 | + esac |
75 | 75 | done |
76 | 76 |
|
77 | 77 | # Set default values if not provided |
78 | 78 | if [[ -z "$PR_TRIGGER_PAT" ]]; then |
79 | | - echo "Error: PR_TRIGGER_PAT environment variable is not set or is empty." |
80 | | - exit 1 |
| 79 | + echo "Error: PR_TRIGGER_PAT environment variable is not set or is empty." |
| 80 | + exit 1 |
81 | 81 | fi |
82 | 82 |
|
83 | 83 | if [[ -z "$jobName" ]]; then |
84 | | - jobName="${infraRepoName}-${targetComponent}-${terraformAction}" |
| 84 | + jobName="${infraRepoName}-${targetComponent}-${terraformAction}" |
85 | 85 | fi |
86 | 86 |
|
87 | 87 | if [[ -z "$terraformAction" ]]; then |
88 | | - terraformAction="plan" |
| 88 | + terraformAction="plan" |
89 | 89 | fi |
90 | 90 |
|
91 | 91 | if [[ -z "$internalRef" ]]; then |
92 | | - internalRef="main" |
| 92 | + internalRef="main" |
93 | 93 | fi |
94 | 94 |
|
95 | 95 |
|
96 | 96 | callerRunId="${GITHUB_RUN_ID}-${jobName}-${GITHUB_RUN_ATTEMPT}" |
97 | 97 |
|
98 | 98 | DISPATCH_EVENT=$(jq -ncM \ |
99 | | - --arg infraRepoName "$infraRepoName" \ |
100 | | - --arg releaseVersion "$releaseVersion" \ |
101 | | - --arg targetEnvironment "$targetEnvironment" \ |
102 | | - --arg targetAccountGroup "$targetAccountGroup" \ |
103 | | - --arg targetComponent "$targetComponent" \ |
104 | | - --arg terraformAction "$terraformAction" \ |
105 | | - --arg callerRunId "$callerRunId" \ |
106 | | - --arg targetWorkflow "$targetWorkflow" \ |
107 | | - '{ |
108 | | - "ref": "'"$internalRef"'", |
109 | | - "inputs": ( |
110 | | - (if $infraRepoName != "" then { "infraRepoName": $infraRepoName } else {} end) + |
111 | | - (if $terraformAction != "" then { "terraformAction": $terraformAction } else {} end) + |
112 | | - { |
113 | | - "releaseVersion": $releaseVersion, |
114 | | - "targetEnvironment": $targetEnvironment, |
115 | | - "targetAccountGroup": $targetAccountGroup, |
116 | | - "targetComponent": $targetComponent |
117 | | - } + |
118 | | - (if ($targetWorkflow | test("dispatch-(acceptance|contextual|product|security)-tests-.*\\.yaml")) |
119 | | - then { "callerRunId": $callerRunId } else {} end) |
120 | | - ) |
121 | | - }') |
| 99 | + --arg infraRepoName "$infraRepoName" \ |
| 100 | + --arg releaseVersion "$releaseVersion" \ |
| 101 | + --arg targetEnvironment "$targetEnvironment" \ |
| 102 | + --arg targetAccountGroup "$targetAccountGroup" \ |
| 103 | + --arg targetComponent "$targetComponent" \ |
| 104 | + --arg terraformAction "$terraformAction" \ |
| 105 | + --arg callerRunId "$callerRunId" \ |
| 106 | + --arg targetWorkflow "$targetWorkflow" \ |
| 107 | + '{ |
| 108 | + "ref": "'"$internalRef"'", |
| 109 | + "inputs": ( |
| 110 | + (if $infraRepoName != "" then { "infraRepoName": $infraRepoName } else {} end) + |
| 111 | + (if $terraformAction != "" then { "terraformAction": $terraformAction } else {} end) + |
| 112 | + { |
| 113 | + "releaseVersion": $releaseVersion, |
| 114 | + "targetEnvironment": $targetEnvironment, |
| 115 | + "targetAccountGroup": $targetAccountGroup, |
| 116 | + "targetComponent": $targetComponent |
| 117 | + } + |
| 118 | + (if ($targetWorkflow | test("dispatch-(acceptance|contextual|product|security)-tests-.*\\.yaml")) |
| 119 | + then { "callerRunId": $callerRunId } else {} end) |
| 120 | + ) |
| 121 | + }') |
122 | 122 |
|
123 | 123 | # Trigger the workflow |
124 | 124 | curl -L \ |
125 | | - --fail \ |
126 | | - --silent \ |
127 | | - -X POST \ |
128 | | - -H "Accept: application/vnd.github+json" \ |
129 | | - -H "Authorization: Bearer ${PR_TRIGGER_PAT}" \ |
130 | | - -H "X-GitHub-Api-Version: 2022-11-28" \ |
131 | | - "https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/$targetWorkflow/dispatches" \ |
132 | | - -d "$DISPATCH_EVENT" |
| 125 | + --fail \ |
| 126 | + --silent \ |
| 127 | + -X POST \ |
| 128 | + -H "Accept: application/vnd.github+json" \ |
| 129 | + -H "Authorization: Bearer ${PR_TRIGGER_PAT}" \ |
| 130 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 131 | + "https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/workflows/$targetWorkflow/dispatches" \ |
| 132 | + -d "$DISPATCH_EVENT" |
133 | 133 |
|
134 | 134 | echo "Workflow triggered. Waiting for the workflow to complete.." |
135 | 135 |
|
136 | 136 | # Poll GitHub API to check the workflow status |
137 | 137 | workflow_run_url="" |
138 | 138 | for _ in {1..18}; do |
139 | | - workflow_run_url=$(curl -s \ |
140 | | - -H "Accept: application/vnd.github+json" \ |
141 | | - -H "Authorization: Bearer ${PR_TRIGGER_PAT}" \ |
142 | | - -H "X-GitHub-Api-Version: 2022-11-28" \ |
143 | | - "https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs?event=workflow_dispatch" \ |
144 | | - | jq -r \ |
145 | | - --arg callerRunId "$callerRunId" \ |
146 | | - --arg targetWorkflow "$targetWorkflow" \ |
147 | | - --arg targetEnvironment "$targetEnvironment" \ |
148 | | - --arg targetAccountGroup "$targetAccountGroup" \ |
149 | | - --arg targetComponent "$targetComponent" \ |
150 | | - --arg terraformAction "$terraformAction" \ |
151 | | - '.workflow_runs[] |
152 | | - | select(.path == ".github/workflows/" + $targetWorkflow) |
153 | | - | select(.name |
154 | | - | contains($targetEnvironment) |
155 | | - and contains($targetAccountGroup) |
156 | | - and contains($targetComponent) |
157 | | - and contains($terraformAction) |
158 | | - ) |
159 | | - | if ($targetWorkflow | test("dispatch-(acceptance|contextual|product|security)-tests-.*\\.yaml")) |
160 | | - then select(.name | contains("caller:" + $callerRunId)) |
161 | | - else . |
162 | | - end |
163 | | - | .url') |
164 | | - |
165 | | - if [[ -n "$workflow_run_url" && "$workflow_run_url" != null ]]; then |
166 | | - ui_url=${workflow_run_url/api./} |
167 | | - ui_url=${ui_url/\/repos/} |
168 | | - echo "Found workflow run url: $ui_url" |
169 | | - echo "workflow_run_url=$workflow_run_url" >> "$GITHUB_ENV" |
170 | | - break |
171 | | - fi |
172 | | - |
173 | | - echo "Waiting for workflow to start..." |
174 | | - sleep 10 |
| 139 | + workflow_run_url=$(curl -s \ |
| 140 | + -H "Accept: application/vnd.github+json" \ |
| 141 | + -H "Authorization: Bearer ${PR_TRIGGER_PAT}" \ |
| 142 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 143 | + "https://api.github.com/repos/NHSDigital/nhs-notify-internal/actions/runs?event=workflow_dispatch" \ |
| 144 | + | jq -r \ |
| 145 | + --arg callerRunId "$callerRunId" \ |
| 146 | + --arg targetWorkflow "$targetWorkflow" \ |
| 147 | + --arg targetEnvironment "$targetEnvironment" \ |
| 148 | + --arg targetAccountGroup "$targetAccountGroup" \ |
| 149 | + --arg targetComponent "$targetComponent" \ |
| 150 | + --arg terraformAction "$terraformAction" \ |
| 151 | + '.workflow_runs[] |
| 152 | + | select(.path == ".github/workflows/" + $targetWorkflow) |
| 153 | + | select(.name |
| 154 | + | contains($targetEnvironment) |
| 155 | + and contains($targetAccountGroup) |
| 156 | + and contains($targetComponent) |
| 157 | + and contains($terraformAction) |
| 158 | + ) |
| 159 | + | if ($targetWorkflow | test("dispatch-(acceptance|contextual|product|security)-tests-.*\\.yaml")) |
| 160 | + then select(.name | contains("caller:" + $callerRunId)) |
| 161 | + else . |
| 162 | + end |
| 163 | + | .url') |
| 164 | + |
| 165 | + if [[ -n "$workflow_run_url" && "$workflow_run_url" != null ]]; then |
| 166 | + ui_url=${workflow_run_url/api./} |
| 167 | + ui_url=${ui_url/\/repos/} |
| 168 | + echo "Found workflow run url: $ui_url" |
| 169 | + echo "workflow_run_url=$workflow_run_url" >> "$GITHUB_ENV" |
| 170 | + break |
| 171 | + fi |
| 172 | + |
| 173 | + echo "Waiting for workflow to start..." |
| 174 | + sleep 10 |
175 | 175 | done |
176 | 176 |
|
177 | 177 | if [[ -z "$workflow_run_url" || "$workflow_run_url" == null ]]; then |
178 | | - echo "Failed to get the workflow run url. Exiting." |
179 | | - exit 1 |
| 178 | + echo "Failed to get the workflow run url. Exiting." |
| 179 | + exit 1 |
180 | 180 | fi |
181 | 181 |
|
182 | 182 | # Wait for workflow completion |
183 | 183 | while true; do |
184 | | - sleep 10 |
185 | | - response=$(curl -s -L \ |
186 | | - -H "Authorization: Bearer ${PR_TRIGGER_PAT}" \ |
187 | | - -H "Accept: application/vnd.github+json" \ |
188 | | - "$workflow_run_url") |
189 | | - |
190 | | - status=$(echo "$response" | jq -r '.status') |
191 | | - conclusion=$(echo "$response" | jq -r '.conclusion') |
192 | | - |
193 | | - if [ "$status" == "completed" ]; then |
194 | | - if [ -z "$conclusion" ] || [ "$conclusion" == "null" ]; then |
195 | | - echo "Workflow marked completed but conclusion not yet available, retrying..." |
196 | | - sleep 5 |
197 | | - continue |
198 | | - fi |
199 | | - |
200 | | - if [ "$conclusion" == "success" ]; then |
201 | | - echo "Workflow completed successfully." |
202 | | - exit 0 |
203 | | - else |
204 | | - echo "Workflow failed with conclusion: $conclusion" |
205 | | - exit 1 |
206 | | - fi |
207 | | - fi |
208 | | - |
209 | | - echo "Workflow still running..." |
210 | | - sleep 20 |
| 184 | + sleep 10 |
| 185 | + response=$(curl -s -L \ |
| 186 | + -H "Authorization: Bearer ${PR_TRIGGER_PAT}" \ |
| 187 | + -H "Accept: application/vnd.github+json" \ |
| 188 | + "$workflow_run_url") |
| 189 | + |
| 190 | + status=$(echo "$response" | jq -r '.status') |
| 191 | + conclusion=$(echo "$response" | jq -r '.conclusion') |
| 192 | + |
| 193 | + if [ "$status" == "completed" ]; then |
| 194 | + if [ -z "$conclusion" ] || [ "$conclusion" == "null" ]; then |
| 195 | + echo "Workflow marked completed but conclusion not yet available, retrying..." |
| 196 | + sleep 5 |
| 197 | + continue |
| 198 | + fi |
| 199 | + |
| 200 | + if [ "$conclusion" == "success" ]; then |
| 201 | + echo "Workflow completed successfully." |
| 202 | + exit 0 |
| 203 | + else |
| 204 | + echo "Workflow failed with conclusion: $conclusion" |
| 205 | + exit 1 |
| 206 | + fi |
| 207 | + fi |
| 208 | + |
| 209 | + echo "Workflow still running..." |
| 210 | + sleep 20 |
211 | 211 | done |
0 commit comments