-
Notifications
You must be signed in to change notification settings - Fork 2
257 lines (238 loc) · 10.7 KB
/
release.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
name: Release
permissions:
packages: write
contents: write
on:
# Triggered on new GitHub Release
release:
types: [published]
# Triggered on every successful Build action
workflow_run:
workflows: ["Build"]
branches: [main,master]
types:
- completed
# Manual trigger for rollback to specific release or redeploy latest
workflow_dispatch:
inputs:
version:
default: latest
description: Tag you want to release.
required: true
jobs:
push_to_registry:
runs-on: ubuntu-20.04
if: ${{ github.event.workflow_run.conclusion != 'failure' }}
steps:
# Checkout latest or specific tag
- name: checkout
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
uses: actions/checkout@v2
- name: checkout tag
if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }}
uses: actions/checkout@v2
with:
ref: refs/tags/${{ github.event.inputs.version }}
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0'
# Assign environment variables used in subsequent steps
- name: repository name fix
run: echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
# TAG_NAME defaults to 'latest' if not a release or manual deployment
- name: Assign version
run: |
echo "TAG_NAME=latest" >> $GITHUB_ENV
if [ "${{ github.event.release.tag_name }}" != "" ]; then
echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
fi;
if [ "${{ github.event.inputs.version }}" != "" ]; then
echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV
fi;
- name: Run CI Prebuild Script
env:
deploy_api: ${{ secrets.DEPLOY_API }}
deploy_cdn: ${{ secrets.DEPLOY_CDN }}
run: |
if [ -e ./.deploy/ci.prebuild.sh ]
then
chmod +x ./.deploy/ci.prebuild.sh
./.deploy/ci.prebuild.sh
else
echo "Skipping CI prebuild"
fi
# Publish .NET Project
- name: Publish dotnet project
working-directory: ./BlazorDiffusion
run: |
dotnet nuget add source "https://nuget.pkg.github.com/ServiceStack/index.json" --username ${{ github.actor }} --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github
dotnet publish -c Release /p:DEPLOY_API=${{ secrets.DEPLOY_API }} /p:DEPLOY_CDN=${{ secrets.DEPLOY_CDN }} /p:APP_TASKS=prerender
# Authenticate, build and push to GitHub Container Registry (ghcr.io)
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build and push new docker image, skip for manual redeploy other than 'latest'
- name: Build and push API Docker image
uses: docker/build-push-action@v4
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
with:
file: Dockerfile
context: .
push: true
tags: ghcr.io/${{ env.image_repository_name }}:${{ env.TAG_NAME }}
secrets: |
github_actor=${{ github.actor }}
github_token=${{ secrets.GITHUB_TOKEN }}
- name: Create env.deploy_cdn
run: |
echo "deploy_cdn=${{ secrets.DEPLOY_CDN }}" >> $GITHUB_ENV
# Deploy UI to GitHub Pages
- name: Deploy to GitHub Pages
if: ${{ env.deploy_cdn != '' }}
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./BlazorDiffusion/bin/Release/net6.0/publish/wwwroot
user_name: 'GitHub Action'
user_email: '[email protected]'
deploy_via_ssh:
needs: push_to_registry
runs-on: ubuntu-20.04
if: ${{ github.event.workflow_run.conclusion != 'failure' }}
steps:
- name: checkout
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
uses: actions/checkout@v2
- name: checkout tag
if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }}
uses: actions/checkout@v2
with:
ref: refs/tags/${{ github.event.inputs.version }}
# Assign environment variables used in subsequent steps
- name: repository name fix and env
run: |
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
echo "TAG_NAME=latest" >> $GITHUB_ENV
if [ "${{ github.event.release.tag_name }}" != "" ]; then
echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
fi;
if [ "${{ github.event.inputs.version }}" != "" ]; then
echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV
fi;
# Populate docker-compose.yml with variables from build process, including TAG_NAME.
- name: docker-compose file prep
uses: danielr1996/[email protected]
env:
RELEASE_VERSION: ${{ env.TAG_NAME }}
IMAGE_REPO: ${{ env.image_repository_name }}
APP_NAME: ${{ github.event.repository.name }}
HOST_DOMAIN: ${{ secrets.DEPLOY_API }}
LETSENCRYPT_EMAIL: ${{ secrets.LETSENCRYPT_EMAIL }}
DEPLOY_API: ${{ secrets.DEPLOY_API }}
DEPLOY_CDN: ${{ secrets.DEPLOY_CDN }}
DREAMAI_APIKEY: ${{ secrets.DREAMAI_APIKEY }}
SERVICESTACK_LICENSE: ${{ secrets.SERVICESTACK_LICENSE }}
# Uncomment to use GitHub Action Secrets
AUTH_KEY: ${{ secrets.AUTH_KEY }}
R2_ACCESS_KEY_ID: #${{ secrets.R2_ACCESS_KEY_ID }}
R2_SECRET_ACCESS_KEY: #${{ secrets.R2_SECRET_ACCESS_KEY }}
AZURE_APP_ID: ${{ secrets.AZURE_APP_ID }}
AZURE_APP_SECRET: ${{ secrets.AZURE_APP_SECRET }}
GOOGLE_CONSUMER_KEY: ${{ secrets.GOOGLE_CONSUMER_KEY }}
GOOGLE_CONSUMER_SECRET: ${{ secrets.GOOGLE_CONSUMER_SECRET }}
FACEBOOK_APP_ID: ${{ secrets.FACEBOOK_APP_ID }}
FACEBOOK_APP_SECRET: ${{ secrets.FACEBOOK_APP_SECRET }}
with:
input: .deploy/docker-compose-template.yml
output: .deploy/${{ github.event.repository.name }}-docker-compose.yml
# Copy only the docker-compose.yml to remote server home folder
- name: copy compose file via scp
uses: appleboy/[email protected]
with:
host: ${{ secrets.DEPLOY_API }}
username: ${{ secrets.DEPLOY_USERNAME }}
port: 22
key: ${{ secrets.DEPLOY_KEY }}
source: ".deploy/${{ github.event.repository.name }}-docker-compose.yml"
target: "~/"
# Populate litestream.yml with variables from build process, including TAG_NAME.
- name: litestream.yml file prep
uses: danielr1996/[email protected]
env:
R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
with:
input: .deploy/litestream-template.yml
output: .deploy/litestream.yml
- name: copy litestream.yml file via scp
uses: appleboy/[email protected]
with:
host: ${{ secrets.DEPLOY_API }}
username: ${{ secrets.DEPLOY_USERNAME }}
port: 22
strip_components: 1
key: ${{ secrets.DEPLOY_KEY }}
source: ".deploy/litestream.yml"
target: "/var/lib/docker/volumes/deploy_BlazorDiffusion-assets/_data"
- name: Restore db using litestream if needed
uses: appleboy/[email protected]
env:
APPTOKEN: ${{ secrets.GITHUB_TOKEN }}
USERNAME: ${{ secrets.DEPLOY_USERNAME }}
with:
host: ${{ secrets.DEPLOY_API }}
username: ${{ secrets.DEPLOY_USERNAME }}
key: ${{ secrets.DEPLOY_KEY }}
port: 22
envs: APPTOKEN,USERNAME
script: |
echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up ${{ github.event.repository.name }}-restore --exit-code-from ${{ github.event.repository.name }}-restore
docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up ${{ github.event.repository.name }}-restore-analytics --exit-code-from ${{ github.event.repository.name }}-restore-analytics
- name: Copy BlazorDiffusion Seed Data to Remote Server
timeout-minutes: 30
uses: appleboy/[email protected]
with:
host: ${{ secrets.DEPLOY_API }}
username: ${{ secrets.DEPLOY_USERNAME }}
port: 22
key: ${{ secrets.DEPLOY_KEY }}
strip_components: 3
source: "./BlazorDiffusion/App_Data/seed/*"
target: "/var/lib/docker/volumes/deploy_BlazorDiffusion-mydb/_data/seed/"
- name: Run remote db migrations
uses: appleboy/[email protected]
env:
APPTOKEN: ${{ secrets.GITHUB_TOKEN }}
USERNAME: ${{ secrets.DEPLOY_USERNAME }}
with:
host: ${{ secrets.DEPLOY_API }}
username: ${{ secrets.DEPLOY_USERNAME }}
key: ${{ secrets.DEPLOY_KEY }}
port: 22
envs: APPTOKEN,USERNAME
script: |
echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up ${{ github.event.repository.name }}-migration --exit-code-from ${{ github.event.repository.name }}-migration
# Deploy Docker image with ServiceStack application using `docker compose up` remotely
- name: remote docker-compose up via ssh
uses: appleboy/[email protected]
env:
APPTOKEN: ${{ secrets.GITHUB_TOKEN }}
USERNAME: ${{ secrets.DEPLOY_USERNAME }}
with:
host: ${{ secrets.DEPLOY_API }}
username: ${{ secrets.DEPLOY_USERNAME }}
key: ${{ secrets.DEPLOY_KEY }}
port: 22
envs: APPTOKEN,USERNAME
script: |
echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up -d