Skip to content

Commit b4be2ef

Browse files
committed
feat: Introducing a new step on the action to retrieve the running workflows on the target project
1 parent 9618e30 commit b4be2ef

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

.github/workflows/deploy-chatbot.yml

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
steps:
1313
- name: Parse Deployment Command
14-
id: parse_command
14+
id: parse-command
1515
run: |
1616
WORKFLOW_LOCAL_RUN="${{ !github.event.act }}"
1717
COMMENT_BODY="${{ github.event.comment.body }}"
@@ -64,12 +64,12 @@ jobs:
6464

6565
- name: Notify the user
6666
uses: actions/github-script@v7
67-
id: notify_user
67+
id: notify-user
6868
with:
6969
script: |
70-
const environment = `${{ steps.parse_command.outputs.environment }}`;
71-
const project = `${{ steps.parse_command.outputs.project }}`;
72-
const infra = `${{ steps.parse_command.outputs.infra }}`;
70+
const environment = `${{ steps.parse-command.outputs.environment }}`;
71+
const project = `${{ steps.parse-command.outputs.project }}`;
72+
const infra = `${{ steps.parse-command.outputs.infra }}`;
7373
7474
const actor = `${{ github.event.comment.user.login }}`;
7575
const username = (actor !== "") ? actor : 'Unknown';
@@ -91,7 +91,7 @@ jobs:
9191
console.log(`Action is being runned locally by 'ACT'.
9292
Skipping the notify user on PR, but output would have been:
9393
${message}`);
94-
return { comment_id: 10 } // arbitraty mocked comment number;
94+
return { comment_id: 10, message: message } // arbitraty mocked comment number;
9595
} else {
9696
let comment = {};
9797
try {
@@ -101,7 +101,7 @@ jobs:
101101
issue_number: prNumber,
102102
body: message,
103103
});
104-
return { "comment_id": comment.data.id };
104+
return { comment_id: comment.data.id, message: message };
105105
} catch (ex) {
106106
console.log("Failed to POST the comment on the PR to notify the user due to =[> " + ex + "]");
107107
return { "comment_id": null };
@@ -114,12 +114,12 @@ jobs:
114114

115115
- name: Trigger Deployment Workflow
116116
uses: actions/github-script@v7
117-
id: trigger_deployment_workflow
117+
id: trigger-deployment-workflow
118118
with:
119119
github-token: ${{ steps.zdc-auth-app-token.outputs.token || github.token }}
120120
script: |
121-
const environment = `${{ steps.parse_command.outputs.environment }}`;
122-
const project = `${{ steps.parse_command.outputs.project }}`;
121+
const environment = `${{ steps.parse-command.outputs.environment }}`;
122+
const project = `${{ steps.parse-command.outputs.project }}`;
123123
const workflowId = `deploy-${environment}.yml`;
124124
125125
let result = "";
@@ -142,20 +142,20 @@ jobs:
142142
143143
// Return triggered details
144144
return {
145-
workflow_name: workflowId,
145+
workflowId: workflowId,
146146
status: status,
147147
details: details,
148148
};
149149
150150
- name: Deploy Infra
151-
if: steps.parse_command.outputs.infra != ''
151+
if: steps.parse-command.outputs.infra != ''
152152
uses: appleboy/[email protected]
153153
with:
154154
host: ${{ secrets.SSH_HOST }}
155155
username: ${{ secrets.SSH_USERNAME }}
156156
key: ${{ secrets.SSH_KEY }}
157157
script: |
158-
case "${{ steps.parse_command.outputs.infra }}" in
158+
case "${{ steps.parse-command.outputs.infra }}" in
159159
postgres)
160160
echo "Deploying Postgres..."
161161
docker run -d --name postgres --restart always -e POSTGRES_PASSWORD=mysecretpassword postgres
@@ -170,7 +170,7 @@ jobs:
170170
docker run -d --name redis --restart always redis
171171
;;
172172
*)
173-
echo "Unknown infra: ${{ steps.parse_command.outputs.infra }}"
173+
echo "Unknown infra: ${{ steps.parse-command.outputs.infra }}"
174174
;;
175175
esac
176176
@@ -181,19 +181,20 @@ jobs:
181181
script: |
182182
const workflowLocalRun = `${{ github.event.act }}` === 'true';
183183
184-
const commentOnPr = `${{ steps.notify_user.outputs.result }}`;
185-
const commentId = JSON.parse(commentOnPr).comment_id;
184+
const commentOnPr = ${{ steps.notify-user.outputs.result }};
185+
const commentId = commentOnPr.comment_id;
186186
console.log(`Updating comment with id ${commentId} for workflow status`);
187187
188-
const workflowDispatchResult = `${{ steps.trigger_deployment_workflow.outputs.result }}`;
189-
console.log(`Trigger deployment status: ${workflowDispatchResult}`);
190-
const workflowDetails = JSON.parse(workflowDispatchResult);
188+
const workflowDetails = ${{ steps.trigger-deployment-workflow.outputs.result }};
189+
console.log(`Trigger deployment status: ${JSON.stringify(workflowDetails, null, 2)}`);
191190
192191
const runUrl = ""; // TODO: empty for now
193192
194193
const statusIcon = workflowDetails.status === 'OK' ? '✅' : '❌';
195194
const statusMsg = workflowDetails.details;
196-
const message = `${statusIcon} Deployment ${statusMsg}. [View Workflow](${runUrl})`;
195+
196+
const previousMsgData = commentOnPr.message;
197+
const message = `${previousMsgData}\n${statusIcon} Deployment ${statusMsg}. [View Workflow](${runUrl})`;
197198
console.log(message);
198199
199200
if (!workflowLocalRun) {
@@ -204,3 +205,39 @@ jobs:
204205
body: message,
205206
});
206207
}
208+
209+
- name: Wait for Deployment
210+
id: wait-for-deployment
211+
uses: actions/github-script@v7
212+
with:
213+
script: |
214+
const project = `${{ steps.trigger-deployment-workflow.outputs.project }}`;
215+
const workflowName = `${{ steps.trigger-deployment-workflow.outputs.workflowId }}`;
216+
const pollInterval = 10 * 1000; // 10 seconds
217+
218+
console.log(`Polling for workflow status: ${workflowName} in ${project}`);
219+
220+
let status = 'in_progress';
221+
let conclusion = '';
222+
while (status === 'in_progress') {
223+
const runs = await github.rest.actions.listWorkflowRuns({
224+
owner: context.repo.owner,
225+
repo: project,
226+
workflow_id: workflowName,
227+
branch: 'main',
228+
status: 'in_progress',
229+
});
230+
231+
if (runs.data.workflow_runs.length > 0) {
232+
const run = runs.data.workflow_runs[0]; // TODO: not really take the first one, should be fix later
233+
status = run.status;
234+
conclusion = run.conclusion;
235+
236+
if (status === 'completed') {
237+
console.log(`Workflow completed with status: ${conclusion}`);
238+
return { status: conclusion, run_url: run.html_url };
239+
}
240+
}
241+
242+
await new Promise(resolve => setTimeout(resolve, pollInterval));
243+
}

0 commit comments

Comments
 (0)