|
1 | | -name: Handle deployment commands in PR Comments |
| 1 | +name: Deploy Chatbot |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | issue_comment: |
5 | 5 | types: |
6 | 6 | - created |
7 | 7 |
|
8 | 8 | jobs: |
9 | | - handle-deployment-on-pr-comment: |
| 9 | + parse-command: |
10 | 10 | runs-on: ubuntu-latest |
11 | 11 | outputs: |
12 | | - artifact: ${{ steps.parse_command.outputs.infra }} |
13 | | - environment: ${{ steps.parse_command.outputs.environment }} |
14 | | - secret_name: ${{ steps.deploy-infra.outputs.secret_name}} |
| 12 | + environment: ${{ steps.parse-command.outputs.environment }} |
| 13 | + infra: ${{ steps.parse-command.outputs.infra }} |
| 14 | + project: ${{ steps.parse-command.outputs.project }} |
15 | 15 |
|
16 | | - steps: |
17 | | - - uses: actions/checkout@v4 |
| 16 | + steps: |
| 17 | + - name: Checkout Repository |
| 18 | + uses: actions/checkout@v4 |
18 | 19 |
|
19 | 20 | - name: Parse Deployment Command |
20 | 21 | uses: actions/github-script@v7 |
21 | | - id: parse_command |
| 22 | + id: parse-command |
22 | 23 | with: |
23 | 24 | script: | |
24 | 25 | const { default: parseDeployCommand } = |
25 | | - await import('${{ github.workspace }}/actions_scripts/parse_deployment_command.js'); |
26 | | - |
27 | | - parseDeployCommand({context, core}); |
28 | | - |
29 | | - - name: Get GH Zero Day Code APP token |
30 | | - if: ${{ !github.event.act }} |
31 | | - uses: actions/create-github-app-token@v1 |
32 | | - id: zdc-auth-app-token |
33 | | - with: |
34 | | - app-id: ${{ vars.ZDC_AUTH_APP_ID }} |
35 | | - private-key: ${{ secrets.ZDC_AUTH_PRIVATE_KEY }} |
36 | | - owner: ${{ github.repository_owner }} |
37 | | - |
38 | | - - name: Notify the user |
39 | | - uses: actions/github-script@v7 |
40 | | - id: notify_user |
41 | | - with: |
42 | | - script: | |
43 | | - const environment = `${{ steps.parse_command.outputs.environment }}`; |
44 | | - const project = `${{ steps.parse_command.outputs.project }}`; |
45 | | - const infra = `${{ steps.parse_command.outputs.infra }}`; |
46 | | -
|
47 | | - const { default: notifyUser } = |
48 | | - await import('${{ github.workspace }}/actions_scripts/notify_user.js'); |
49 | | - |
50 | | - return await notifyUser({github, context, core, environment, project, infra}); |
51 | | -
|
52 | | - - name: Trigger Deployment Workflow |
53 | | - uses: actions/github-script@v7 |
54 | | - id: trigger_deployment_workflow |
55 | | - with: |
56 | | - github-token: ${{ steps.zdc-auth-app-token.outputs.token || github.token }} |
57 | | - script: | |
58 | | - const environment = `${{ steps.parse_command.outputs.environment }}`; |
59 | | - const project = `${{ steps.parse_command.outputs.project }}`; |
60 | | - const workflowId = `deploy-${environment}.yml`; |
61 | | -
|
62 | | - let result = ""; |
63 | | - let details = ""; |
| 26 | + await import(`${{ github.workspace }}/actions_scripts/parse_deployment_command.js`); |
64 | 27 |
|
65 | | - try { |
66 | | - await github.rest.actions.createWorkflowDispatch({ |
67 | | - owner: context.repo.owner, |
68 | | - repo: project, |
69 | | - workflow_id: workflowId, |
70 | | - ref: 'main', // TODO: Change branch to ref if pre, otherwise PRO should be only on main |
71 | | - }); |
72 | | - status = 'OK'; |
73 | | - details = 'succedeed'; |
74 | | - } catch (ex) { |
75 | | - console.log(`FAILED TO TRIGGER WORKFLOW:\n${ex}`); |
76 | | - status = 'ERR'; |
77 | | - details = `Failed to trigger the workflow ${workflowId} on repo: ${project}`; |
78 | | - } |
| 28 | + parseDeployCommand({ context, core }); |
79 | 29 |
|
80 | | - // Return triggered details |
81 | | - return { |
82 | | - workflowId: workflowId, |
83 | | - status: status, |
84 | | - details: details, |
85 | | - }; |
| 30 | + generate-token: |
| 31 | + needs: parse-command |
| 32 | + uses: zerodaycode/app-summoners-sync/.github/workflows/generate-token.yml@develop |
86 | 33 |
|
87 | | - |
88 | | - - name: Deploy Infra |
89 | | - id: deploy-infra |
90 | | - if: steps.parse_command.outputs.infra != '' |
91 | | - run: | |
92 | | - case "${{ steps.parse_command.outputs.infra }}" in |
93 | | - postgres) |
94 | | - echo "Deploying Postgres..." |
95 | | - ;; |
96 | | - redis) |
97 | | - echo "Deploying Redis..." |
98 | | - ;; |
99 | | - *) |
100 | | - echo "Unknown infra: ${{ steps.parse_command.outputs.infra }}" |
101 | | - exit 1 |
102 | | - ;; |
103 | | - esac |
104 | | - INFRA_UPPER=$(echo "${{steps.parse_command.outputs.infra }}" | tr 'a-z' 'A-Z') |
105 | | - ENV_UPPER=$(echo "${{ steps.parse_command.outputs.environment }}" | tr 'a-z' 'A-Z') |
106 | | -
|
107 | | - secret_name="SS_${INFRA_UPPER}_${ENV_UPPER}" |
108 | | -
|
109 | | - echo "Generated secret_name: $secret_name" |
110 | | - echo "SECRET_NAME=$secret_name" >> $GITHUB_OUTPUT |
111 | | -
|
112 | | - |
113 | | - - name: Update Deployment Status |
114 | | - if: steps.parse_command.outputs.project != '' # TODO provisional ñapa while we don't add the remaining outputs |
115 | | - uses: actions/github-script@v7 |
116 | | - id: update-comment-with-deployment-status |
117 | | - with: |
118 | | - script: | |
119 | | - const steps = ${{ toJSON(steps) }}; |
120 | | -
|
121 | | - const { default: updatePrComment } = |
122 | | - await import('${{ github.workspace }}/actions_scripts/update_pr_comment.js'); |
123 | | - |
124 | | - await updatePrComment(github, context, steps); |
| 34 | + notify-user: |
| 35 | + needs: generate-token |
| 36 | + uses: zerodaycode/app-summoners-sync/.github/workflows/notify-user.yml@develop |
| 37 | + with: |
| 38 | + environment: ${{ needs.parse-command.outputs.environment }} |
| 39 | + project: ${{ needs.parse-command.outputs.project }} |
| 40 | + infra: ${{ needs.parse-command.outputs.infra }} |
| 41 | + |
| 42 | + trigger-deployment: |
| 43 | + needs: notify-user |
| 44 | + if: needs.parse-command.outputs.project != '' |
| 45 | + uses: zerodaycode/app-summoners-sync/.github/workflows/trigger-deployment.yml@develop |
| 46 | + with: |
| 47 | + project: ${{ needs.parse-command.outputs.project }} |
| 48 | + environment: ${{ needs.parse-command.outputs.environment }} |
125 | 49 |
|
126 | | - call-deploy-infra-artifact: |
127 | | - needs: handle-deployment-on-pr-comment |
| 50 | + deploy-infra: |
| 51 | + needs: notify-user |
| 52 | + if: needs.parse-command.outputs.infra != '' |
128 | 53 | uses: zerodaycode/app-summoners-sync/.github/workflows/deploy-infra.yml@develop |
129 | 54 | with: |
130 | | - artifact: ${{ needs.handle-deployment-on-pr-comment.outputs.artifact }} |
131 | | - environment: ${{ needs.handle-deployment-on-pr-comment.outputs.environment }} |
132 | | - secret_name: ${{ needs.handle-deployment-on-pr-comment.outputs.secret_name }} |
133 | | - secrets: inherit |
134 | | - |
| 55 | + artifact: ${{ needs.parse-command.outputs.infra }} |
| 56 | + environment: ${{ needs.parse-command.outputs.environment }} |