-
Notifications
You must be signed in to change notification settings - Fork 1
269 lines (226 loc) · 10.1 KB
/
build.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
name: Build & Deploy
on:
push:
paths:
- "src/**"
- "lib/**"
- "shared/**"
- "targets/**"
- "scripts/**"
- "nomad/**"
- ".github/workflows/build.yml"
- "Dockerfile"
- "Makefile"
- ".dockerignore"
- "grid-bot-bare.sln"
branches:
- "**"
workflow_dispatch:
inputs:
build_configuration:
description: "Build Configuration"
required: true
default: "Release"
type: choice
options:
- "Release"
- "Debug"
image:
description: "Image Name"
required: true
default: "cloud-registry.simulprod.com/grid/grid-bot"
registry:
description: "Docker Registry"
required: true
default: "cloud-registry.simulprod.com"
create_release:
description: "Create Release"
required: true
default: true
type: boolean
upload_artifact:
description: "Upload Artifact"
required: true
default: false
type: boolean
create_image:
description: "Create Image"
required: true
default: true
type: boolean
deploy_to_nomad:
description: "Deploy to Nomad"
required: true
default: false
type: boolean
nomad_job_name:
description: "Nomad Job Name"
required: true
default: "grid-bot"
nomad_enviroment:
description: "Nomad Environment"
required: true
default: "production"
type: choice
options:
- "production"
- "staging"
nomad_resources:
description: "Nomad Resources (CPU:Memory)"
required: true
default: "2000:1024"
permissions:
contents: write
deployments: write
jobs:
build:
if: ${{ !contains(github.event.head_commit.message, '#!skip-build-and-deploy!#') }}
runs-on: grid-bot-infra
env:
BUILD_CONFIGURATION: ${{ github.event.inputs.build_configuration || (github.event_name == 'push' && github.ref == 'refs/heads/master' && 'Release') || 'Debug' }}
IMAGE: ${{ github.event.inputs.image || (github.event_name == 'push' && github.ref == 'refs/heads/master' && 'cloud-registry.simulprod.com/grid/grid-bot') || 'registry.simulpong.com/grid/grid-bot' }}
DOCKER_REGISTRY: ${{ github.event.inputs.registry || (github.event_name == 'push' && github.ref == 'refs/heads/master' && 'cloud-registry.simulprod.com') || 'registry.simulpong.com' }}
CREATE_RELEASE: ${{ github.event.inputs.create_release || !contains(github.event.head_commit.message, '#!skip-release!#') }}
CREATE_IMAGE: ${{ github.event.inputs.create_image || !contains(github.event.head_commit.message, '#!skip-image!#') }}
UPLOAD_ARTIFACT: ${{ github.event.inputs.upload_artifact || contains(github.event.head_commit.message, '#!upload-artifact!#') }}
DEPLOY_TO_NOMAD: ${{ github.event.inputs.deploy_to_nomad || (github.event_name == 'push' && github.ref == 'refs/heads/master' && !contains(github.event.head_commit.message, '#!skip-deploy!#')) }}
NOMAD_ADDR: ${{ vars.NOMAD_ADDR }}
NOMAD_TOKEN: ${{ secrets.NOMAD_TOKEN }}
NOMAD_ENVIRONMENT: ${{ github.event.inputs.nomad_enviroment || 'production' }}
NOMAD_JOB_NAME: ${{ github.event.inputs.nomad_job_name || 'grid-bot' }}-${{ (github.event.inputs.nomad_enviroment || 'production') == 'production' && 'prod' || 'stage' }}
NOMAD_RESOURCES: ${{ github.event.inputs.nomad_resources || '2000:1024' }}
DOCKER_USERNAME: ${{ vars.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Move to Checkout Path
run: |
mkdir -p /_/_work/${{ github.repository }}/${{ github.sha }}
(shopt -s dotglob; mv * /_/_work/${{ github.repository }}/${{ github.sha }})
- name: Setup .NET 6
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Create Deployment Directory
run: |
mkdir -p /_/_work/${{ github.repository }}/${{ github.sha }}/deploy
# Generate version and allow it as an output
- name: Generate Version
id: version
run: |
DATE=$(date +"%Y.%m.%d-%H.%M.%S")
# Format: yyyy.mm.dd-hh.mm.ss-<short sha>
VERSION="$DATE-$(echo $GITHUB_SHA | cut -c1-7)"
# If we are building debug, append -dev
if [ "${{ env.BUILD_CONFIGURATION }}" == "Debug" ]; then
VERSION="$VERSION-dev"
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Build
run: |
cd /_/_work/${{ github.repository }}/${{ github.sha }} && \
dotnet publish /_/_work/${{ github.repository }}/${{ github.sha }}/src/Grid.Bot.csproj \
-c ${{ env.BUILD_CONFIGURATION }} \
-o /_/_work/${{ github.repository }}/${{ github.sha }}/deploy/${{ steps.version.outputs.version }} \
-p:IMAGE_TAG=${{ steps.version.outputs.version }} \
-p:CI=true
- name: Create Release Artifact
if: ${{ env.CREATE_RELEASE == 'true' }}
id: zip
run: |
cd /_/_work/${{ github.repository }}/${{ github.sha }}/deploy/${{ steps.version.outputs.version }} && \
zip -r /_/_work/${{ github.repository }}/${{ github.sha }}/deploy/${{ steps.version.outputs.version }}.zip \
.
- name: Write Release Artifact
if: ${{ env.CREATE_RELEASE == 'true' && steps.zip.outcome == 'success' }}
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.DEPLOYER_TOKEN }}
with:
files: |
/_/_work/${{ github.repository }}/${{ github.sha }}/deploy/${{ steps.version.outputs.version }}.zip
tag_name: ${{ steps.version.outputs.version }}
generate_release_notes: true
prerelease: ${{ env.BUILD_CONFIGURATION == 'Debug' }}
draft: false
target_commitish: ${{ github.sha }}
- name: Upload Release Artifact
if: ${{ env.UPLOAD_ARTIFACT == 'true' && steps.zip.outcome == 'success' }}
uses: actions/upload-artifact@v3
with:
name: ${{ steps.version.outputs.version }}.zip
path: /_/_work/${{ github.repository }}/${{ github.sha }}/deploy/${{ steps.version.outputs.version }}.zip
retention-days: 1
- name: Create Image
if: ${{ env.CREATE_IMAGE == 'true' && env.DOCKER_REGISTRY && env.DOCKER_USERNAME && env.DOCKER_PASSWORD }}
id: image
run: |
echo "${{ env.DOCKER_PASSWORD }}" | docker login -u "${{ env.DOCKER_USERNAME }}" --password-stdin "${{ env.DOCKER_REGISTRY }}"
cd /_/_work/${{ github.repository }}/${{ github.sha }} && \
docker build -t ${{ env.IMAGE }}:${{ steps.version.outputs.version }} --build-arg IMAGE_TAG=${{ steps.version.outputs.version }} .
if [ "${{ github.ref }}" == "refs/heads/master" ]; then
docker tag ${{ env.IMAGE }}:${{ steps.version.outputs.version }} ${{ env.IMAGE }}:latest
fi
docker push ${{ env.IMAGE }}:${{ steps.version.outputs.version }}
if [ "${{ github.ref }}" == "refs/heads/master" ]; then
docker push ${{ env.IMAGE }}:latest
fi
- name: Setup Nomad CLI
if: ${{ env.DEPLOY_TO_NOMAD == 'true' && env.NOMAD_ADDR && steps.image.outcome == 'success' }}
uses: nferch/setup-nomad@v4.0.0
env:
NOMAD_TLS_SKIP_VERIFY: 1
- name: Set Initial GitHub Deployment Status
if: ${{ env.DEPLOY_TO_NOMAD == 'true' && env.NOMAD_ADDR && steps.image.outcome == 'success' }}
uses: chrnorm/deployment-action@v2.0.7
id: deployment
continue-on-error: true
with:
token: "${{ github.token }}"
environment: ${{ env.NOMAD_ENVIRONMENT }}
environment-url: "${{ env.NOMAD_ADDR }}/ui/jobs/${{ env.NOMAD_JOB_NAME }}"
description: "Version: ${{ steps.version.outputs.version }}"
- name: Deploy to Nomad
if: ${{ env.DEPLOY_TO_NOMAD == 'true' && env.NOMAD_ADDR && steps.image.outcome == 'success' }}
env:
NOMAD_TLS_SKIP_VERIFY: 1
id: deploy
continue-on-error: true
run: |
# Template file is at ${workspace}/nomad/grid-bot.nomad
# Replace {{{IMAGE_NAME}}} and {{{IMAGE_TAG}}} with the actual values
# Create a temporary file
NOMAD_JOB_FILE=$(mktemp)
# Replace the template with the actual values
sed -e "s,{{{IMAGE_NAME}}},${{ env.IMAGE }}," \
-e "s/{{{IMAGE_TAG}}}/${{ steps.version.outputs.version }}/" \
-e "s/{{{NOMAD_JOB_NAME}}}/${{ env.NOMAD_JOB_NAME }}/" \
-e "s/{{{NOMAD_ENVIRONMENT}}}/${{ env.NOMAD_ENVIRONMENT }}/" \
-e "s/{{{NOMAD_CPU}}}/$(echo ${{ env.NOMAD_RESOURCES }} | cut -d: -f1)/" \
-e "s/{{{NOMAD_MEMORY}}}/$(echo ${{ env.NOMAD_RESOURCES }} | cut -d: -f2)/" \
-e "s/{{{NOMAD_LOG_LEVEL}}}/Information/" \
/_/_work/${{ github.repository }}/${{ github.sha }}/nomad/grid-bot.nomad > $NOMAD_JOB_FILE
# Run the job but do not wait for longer than 5 minutes
nomad job run $NOMAD_JOB_FILE
# Cleanup
rm $NOMAD_JOB_FILE
- name: Set GitHub Deployment Status
if: ${{ env.DEPLOY_TO_NOMAD == 'true' && env.NOMAD_ADDR && steps.image.outcome == 'success' }}
uses: chrnorm/deployment-status@v2
continue-on-error: true
with:
token: "${{ github.token }}"
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
environment-url: "${{ steps.deployment.outputs.environment_url }}"
description: "Version: ${{ steps.version.outputs.version }}"
# 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.outcome }}"
- name: Cleanup
if: always()
continue-on-error: true
run: |
rm -rf /_/_work/${{ github.repository }}/${{ github.sha }}