-
Notifications
You must be signed in to change notification settings - Fork 1
executable file
·132 lines (106 loc) · 4.17 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: Deploy
on:
workflow_dispatch:
inputs:
components:
description: "The list of components to deploy"
required: true
type: string
nomad_environment:
description: "The environment to deploy to"
required: true
default: "production"
type: choice
options:
- staging
- production
# In the form of "component,cpu:ram;component2,cpu:ram"
nomad_resources:
description: "The resources to use when deploying"
required: false
default: "grid-bot,2000:1024"
type: string
permissions:
deployments: write
jobs:
parse-input-components:
name: Validate and find component directories
runs-on: ubuntu-latest
outputs:
nomad-files: ${{ steps.components-to-nomad-jobs.outputs.nomad-files }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Validate and Find components
uses: mfdlabs/component-finder-action@v8
id: find-component-directories
with:
components: ${{ github.event.inputs.components }}
component-search-directories: services
- name: Components to Nomad Jobs
uses: mfdlabs/component-nomad-parser-action@v14
id: components-to-nomad-jobs
env:
NOMAD_ENVIRONMENT: ${{ github.event.inputs.nomad_environment }}
NOMAD_SHORT_ENVIRONMENT: ${{ github.event.inputs.nomad_environment == 'production' && 'prod' || 'stage' }}
with:
components: ${{ steps.find-component-directories.outputs.components }}
resources: ${{ github.event.inputs.nomad_resources }}
deploy-nomad-jobs:
name: Deploy Nomad Jobs
runs-on: grid-bot-infra
needs: parse-input-components
if: ${{ needs.parse-input-components.outputs.nomad-files != '{}' && needs.parse-input-components.outputs.nomad-files != '' }}
env:
NOMAD_ADDR: ${{ vars.NOMAD_ADDR }}
NOMAD_TOKEN: ${{ secrets.NOMAD_TOKEN }}
steps:
- name: Setup Nomad CLI
uses: nferch/setup-nomad@v4.0.0
env:
NOMAD_TLS_SKIP_VERIFY: 1
- name: Set Initial GitHub Deployment Status
uses: chrnorm/deployment-action@v2
id: deployment
continue-on-error: true
with:
token: "${{ secrets.DEPLOYER_TOKEN }}"
environment: "${{ github.event.inputs.nomad_environment }}"
description: "Components: ${{ github.event.inputs.components }}"
- name: Deploy Nomad Jobs
uses: actions/github-script@v7
id: deploy-nomad-jobs
continue-on-error: true
with:
script: |
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
let jobs = ${{ needs.parse-input-components.outputs.nomad-files }};
jobs = new Map(Object.entries(jobs));
const failedJobs = [];
for (const [jobName, jobDefinition] of jobs) {
const [componentName] = jobName.split(':');
// Equivalent to mktemp
const tempFile = path.join(process.env.RUNNER_TEMP, `nomad-job-${componentName}.hcl`);
fs.writeFileSync(tempFile, jobDefinition);
// Poll the job status
try {
child_process.execSync(`nomad job run ${tempFile}`, { stdio: 'inherit' });
} catch (error) {
failedJobs.push(jobName);
}
}
if (failedJobs.length > 0) {
core.setFailed(`Failed to deploy the following jobs: ${failedJobs.join(', ')}`);
}
- name: Set GitHub Deployment Status
uses: chrnorm/deployment-status@v2
continue-on-error: true
with:
token: "${{ secrets.DEPLOYER_TOKEN }}"
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
description: "Components: ${{ github.event.inputs.components }}"
# If the "deploy" step fails, the deployment status will be set to "failure"
# If the "deploy" step succeeds, the deployment status will be set to "success"
state: "${{ steps.deploy-nomad-jobs.outcome }}"