-
Notifications
You must be signed in to change notification settings - Fork 2
192 lines (156 loc) · 7.34 KB
/
deploy.yml
File metadata and controls
192 lines (156 loc) · 7.34 KB
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
# This workflow will Deploy a .NET project that has been built and track the latest build's via artifacts
name: Deploy
on:
# scheduled to execute every Monday - Thursday at 11:59 pm CDT ( translated into UTC )
schedule:
# see schedule docs: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onschedule
# see cron syntax: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#tag_20_25_07
# see CDT to UTC conversion: https://savvytime.com/converter/cdt-to-utc
- cron: '59 4 * * 2-5'
# Can be manually triggered from the github actions page: https://github.com/SensitTechnologies/MESS/actions/workflows/deploy.yml
workflow_dispatch:
env:
BUILD_COMMIT: 'last-build-version'
LAST_DEPLOY_COMMIT: 'last-deploy-version'
APP_ARTIFACT: .net-app
jobs:
# read last deployed artifact
download-last-deployed:
runs-on: ubuntu-latest
outputs:
last-version: ${{ steps.last-version.outputs.sha }}
steps: # Steps to read last deployed artifact
- name: Get run ID of last Deploy workflow
id: get-prev-run-id
run: |
OTHER_REPO="${{ github.repository }}"
WF_NAME="Deploy"
RUN_ID=`gh run --repo ${OTHER_REPO} list --workflow ${WF_NAME} --json databaseId --jq .[1].databaseId`
echo "Detected latest run id of ${RUN_ID} for workflow ${WF_NAME}"
echo "run-id=${RUN_ID}" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ github.token }}
- name: Download last artifact from this workflow
uses: actions/download-artifact@v4
with:
name: ${{ env.LAST_DEPLOY_COMMIT }} # env variable with artifact name
github-token: ${{ github.token }} # only need basic github token
repository: ${{ github.repository }} # use this repository
run-id: ${{ steps.get-prev-run-id.outputs.run-id }} # run id from previous step
- name: Archive default deploy version number # (default=current) in case no new version or job doesn't finish)
uses: actions/upload-artifact@v4 # If there is a new version to deploy, this will be overwritten later
with:
name: ${{ env.LAST_DEPLOY_COMMIT }}
path: ${{ env.LAST_DEPLOY_COMMIT }}.txt
# write file contents into output to be used in "compare-results" job
- name: Read last deployed SHA (if exists)
id: last-version
run: |
if [ -f ${{ env.LAST_DEPLOY_COMMIT }}.txt ]; then
echo "sha=$(cat ${{ env.LAST_DEPLOY_COMMIT }}.txt)" >> $GITHUB_OUTPUT
else
echo "sha=" >> $GITHUB_OUTPUT
fi
# read last built artifact
download-last-built:
runs-on: ubuntu-latest
outputs:
built-version: ${{ steps.built-version.outputs.sha }}
run-id: ${{ steps.get-run-id.outputs.run-id }}
steps: # Steps to download Build artifact
- name: Get run ID of Build workflow
id: get-run-id
run: |
OTHER_REPO="${{ github.repository }}"
WF_NAME="Build"
RUN_ID=`gh run --repo ${OTHER_REPO} list --workflow ${WF_NAME} --json databaseId --jq .[0].databaseId`
echo "Detected latest run id of ${RUN_ID} for workflow ${WF_NAME}"
echo "run-id=${RUN_ID}" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ github.token }}
- name: Download artifact from "Build .Net Project" workflow
uses: actions/download-artifact@v4
with:
name: ${{ env.BUILD_COMMIT }} # env variable with artifact name
github-token: ${{ github.token }} # only need basic github token
repository: ${{ github.repository }} # use this repository
run-id: ${{ steps.get-run-id.outputs.run-id }} # run id from previous step
# write file contents into output to be used in "compare-results" job
- name: Read built commit SHA
id: built-version
run: |
echo "sha=$(cat ${{ env.BUILD_COMMIT }}.txt)" >> $GITHUB_OUTPUT
# compares the SHA outputs from the previous two jobs to be used in the rest of the program
compare-results:
runs-on: ubuntu-latest
# wait for these jobs to finish first
needs:
- download-last-deployed # needed for last deployment commit SHA
- download-last-built # needed for commit SHA of build action
outputs:
new-build: ${{ steps.compare.outputs.deploy }}
steps:
- name: Compare SHAs
id: compare
run: |
if [ "${{ needs.download-last-deployed.outputs.last-version }}" == "${{ needs.download-last-built.outputs.built-version }}" ]; then
echo "No new build to deploy."
echo "deploy=false" >> $GITHUB_OUTPUT
else
echo "New build detected!"
echo "deploy=true" >> $GITHUB_OUTPUT
fi
# There is a new version. upload an updated artifact
upload-new-artifacts:
runs-on: ubuntu-latest
# wait for these jobs to finish first
needs:
- compare-results # needed for results comparrison
- download-last-built # needed for commit SHA of build action
steps:
# write SHA output from download-last-build job into a txt file
- name: Record build SHA into last deployed sha
if: needs.compare-results.outputs.new-build == 'true'
run: echo "${{ needs.download-last-built.outputs.built-version }}" >> ${{ env.LAST_DEPLOY_COMMIT }}.txt
# overwrite the default artifact with our new txt file
- name: Archive New version number
if: needs.compare-results.outputs.new-build == 'true'
uses: actions/upload-artifact@v4
with:
name: ${{ env.LAST_DEPLOY_COMMIT }}
path: ${{ env.LAST_DEPLOY_COMMIT }}.txt
overwrite: true
# There is a new version. Deploy it to Azure
deploy:
runs-on: ubuntu-latest
# wait for these jobs to finish first
needs:
- compare-results # needed for results comparrison
- download-last-built # needed for run-id of build action
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps: # There is a new version. Deploy!
- name: Download artifact from "Build .Net Project" workflow
if: needs.compare-results.outputs.new-build == 'true'
uses: actions/download-artifact@v4
with:
name: ${{ env.APP_ARTIFACT }} # env variable with artifact name
github-token: ${{ github.token }} # only need basic github token
repository: ${{ github.repository }} # use this repository
run-id: ${{ needs.download-last-built.outputs.run-id }} # run id from previous step
- name: Deploy to Azure Web App
if: needs.compare-results.outputs.new-build == 'true'
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: 'sensit-mess'
slot-name: 'Production'
package: .
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_AFD862453FEA47FF843B6F50347257E2 }}
Call-Documentation:
needs:
- deploy
- compare-results
if: needs.compare-results.outputs.new-build == 'true'
uses: ./.github/workflows/deploy_documentation.yml