Skip to content

Commit e963d01

Browse files
committed
ci: updating the message with the workflow status after being triggered
1 parent 3f985a1 commit e963d01

File tree

3 files changed

+92
-22
lines changed

3 files changed

+92
-22
lines changed

.actrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
--action-offline-mode
1+
--action-offline-mode
2+
-e .github/workflows/mock_payloads/deploy_act.json

.github/workflows/deploy-chatbot.yml

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ jobs:
1313
- name: Parse Deployment Command
1414
id: parse_command
1515
run: |
16+
WORKFLOW_LOCAL_RUN="${{ !github.event.act }}"
1617
COMMENT_BODY="${{ github.event.comment.body }}"
17-
if [[ "$env.ACT" ]]; then
18+
if [[ WORKFLOW_LOCAL_RUN ]]; then
1819
echo "Setting the command as a mock since it's running locally with ACT"
1920
COMMENT_BODY='/deploy --environment pre --project ag-summoners-sync'
2021
fi
@@ -53,7 +54,7 @@ jobs:
5354
echo "infra=$INFRA" >> $GITHUB_OUTPUT
5455
5556
- name: Get GH Zero Day Code APP token
56-
if: ${{ !env.ACT }}
57+
if: ${{ !github.event.act }}
5758
uses: actions/create-github-app-token@v1
5859
id: zdc-auth-app-token
5960
with:
@@ -63,6 +64,7 @@ jobs:
6364

6465
- name: Notify the user
6566
uses: actions/github-script@v7
67+
id: notify_user
6668
with:
6769
script: |
6870
const environment = `${{ steps.parse_command.outputs.environment }}`;
@@ -72,6 +74,8 @@ jobs:
7274
const actor = `${{ github.event.comment.user.login }}`;
7375
const username = (actor !== "") ? actor : 'Unknown';
7476
77+
const prNumber = context.payload.issue.number;
78+
7579
let message = `🚀 Deployment action request received from user: ${username}\n`;
7680
if (project) {
7781
message += `- Project: \`${project}\`\n`;
@@ -83,43 +87,60 @@ jobs:
8387
message += `- Infrastructure: \`${infra}\`\n`;
8488
}
8589
86-
const localRun = `${{ env.ACT }}` === "true";
87-
88-
if (localRun) {
90+
if (${{ github.event.act }}) {
8991
console.log(`Action is being runned locally by 'ACT'.
9092
Skipping the notify user on PR, but output would have been:
9193
${message}`);
92-
return;
94+
return { comment_id: 10 } // arbitraty mocked comment number;
95+
} else {
96+
github.rest.issues.createComment({
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
issue_number: prNumber,
100+
body: message,
101+
});
102+
console.log(`Comment REST returned data: ${JSON.stringify(comment, null, 2)}`);
103+
return { "comment_id": comment.data.id };
93104
}
94105
95-
const prNumber = context.payload.issue.number;
96-
97-
github.rest.issues.createComment({
98-
owner: context.repo.owner,
99-
repo: context.repo.repo,
100-
issue_number: prNumber,
101-
body: message,
102-
});
103-
104106
# TODO: from here, isolate them in independent workflows
105107
# and just call them depending on the input by having just
106108
# a simple and unique trigger deployment
107109

108110
- name: Trigger Deployment Workflow
109111
uses: actions/github-script@v7
112+
id: trigger_deployment_workflow
110113
with:
111114
github-token: ${{ steps.zdc-auth-app-token.outputs.token || github.token }}
112115
script: |
113116
const environment = `${{ steps.parse_command.outputs.environment }}`;
114117
const project = `${{ steps.parse_command.outputs.project }}`;
115118
const workflowId = `deploy-${environment}.yml`;
116119
117-
github.rest.actions.createWorkflowDispatch({
118-
owner: context.repo.owner,
119-
repo: project,
120-
workflow_id: workflowId,
121-
ref: 'main', // TODO: Change branch to ref if pre, otherwise PRO should be only on main
122-
});
120+
let result = "";
121+
let details = "";
122+
123+
try {
124+
await github.rest.actions.createWorkflowDispatch({
125+
owner: context.repo.owner,
126+
repo: project,
127+
workflow_id: workflowId,
128+
ref: 'main', // TODO: Change branch to ref if pre, otherwise PRO should be only on main
129+
});
130+
status = 'OK';
131+
details = 'succedeed';
132+
} catch (ex) {
133+
console.log(`FAILED TO TRIGGER WORKFLOW:\n${ex}`);
134+
status = 'ERR';
135+
details = `Failed to trigger the workflow ${workflowId} on repo: ${project}`;
136+
}
137+
138+
// Return triggered details
139+
return {
140+
workflow_name: workflowId,
141+
status: status,
142+
details: details,
143+
};
123144
124145
- name: Deploy Infra
125146
if: steps.parse_command.outputs.infra != ''
@@ -148,3 +169,33 @@ jobs:
148169
;;
149170
esac
150171
172+
- name: Update Deployment Status
173+
uses: actions/github-script@v7
174+
id: update-comment-with-deployment-status
175+
with:
176+
script: |
177+
const workflowLocalRun = ${{ github.event.act }};
178+
179+
const commentOnPr = `${{ steps.notify_user.outputs.result }}`;
180+
const commentId = JSON.parse(commentOnPr).comment_id;
181+
console.log(`Updating comment with id ${commentId} for workflow status`);
182+
183+
const workflowDispatchResult = `${{ steps.trigger_deployment_workflow.outputs.result }}`;
184+
console.log(`Trigger deployment status: ${workflowDispatchResult}`);
185+
const workflowDetails = JSON.parse(workflowDispatchResult);
186+
187+
const runUrl = ""; // TODO: empty for now
188+
189+
const statusIcon = workflowDetails.status === 'OK' ? '✅' : '❌';
190+
const statusMsg = workflowDetails.details;
191+
const message = `${statusIcon} Deployment ${statusMsg}. [View Workflow](${runUrl})`;
192+
console.log(message);
193+
194+
if (!workflowLocalRun) {
195+
await github.rest.issues.updateComment({
196+
owner: context.repo.owner,
197+
repo: context.repo.repo,
198+
comment_id: commentId,
199+
body: message,
200+
});
201+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"act": true,
3+
"comment": {
4+
"body": "/deploy --environment pre --project ag-summoners-sync",
5+
"user": {
6+
"login": "TheRustifyer"
7+
}
8+
},
9+
"issue": {
10+
"number": 4
11+
},
12+
"repository": {
13+
"name": "app-summoners-sync",
14+
"owner": {
15+
"login": "zerodaycode"
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)