-
Notifications
You must be signed in to change notification settings - Fork 8
184 lines (154 loc) · 5.43 KB
/
ci-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
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
types: [ opened, reopened, synchronize ]
workflow_call:
workflow_dispatch:
name: ci-build
env:
DOTNET_VERSION: 9.0.x
REGISTRY: ghcr.io
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup .NET SDK ${{ env.DOTNET_VERSION }}
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore
- name: Publish API
run: dotnet publish API/API.csproj -c Release -o ./publish/API
- name: Publish LiveControlGateway
run: dotnet publish LiveControlGateway/LiveControlGateway.csproj -c Release -o ./publish/LiveControlGateway
- name: Publish Cron
run: dotnet publish Cron/Cron.csproj -c Release -o ./publish/Cron
- name: Upload API artifacts
uses: actions/upload-artifact@v4
with:
name: API
path: publish/API/*
retention-days: 1
if-no-files-found: error
- name: Upload LiveControlGateway artifacts
uses: actions/upload-artifact@v4
with:
name: LiveControlGateway
path: publish/LiveControlGateway/*
retention-days: 1
if-no-files-found: error
- name: Upload Cron artifacts
uses: actions/upload-artifact@v4
with:
name: Cron
path: publish/Cron/*
retention-days: 1
if-no-files-found: error
containerize:
runs-on: ubuntu-latest
needs: build
strategy:
matrix:
project: [ API, LiveControlGateway, Cron ]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
sparse-checkout: |
${{ matrix.project }}.Dockerfile
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: ${{ matrix.project }}
path: publish/
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Get container image name. sed does two things here:
# - Replaces "aB" with "a-b";
# - Replaces "AAA" with "aaa".
# The end result is:
# - "API" becomes "api"
# - "LiveControlGateway" becomes "live-control-gateway"
- name: Get image name
id: name
run: echo "name=$(echo "${{ matrix.project }}" | sed -r 's|([a-z0-9])([A-Z])|\1-\L\2|g;s|([A-Z])|\L\1|g')" >> $GITHUB_OUTPUT
- name: Find latest tag
uses: oprypin/find-latest-tag@v1
id: latest-tag
with:
repository: ${{ github.repository }}
regex: '^[0-9]+.[0-9]+.[0-9]+$'
releases-only: false
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ steps.name.outputs.name }}
flavor: |
latest=false
tags: |
type=raw,value={{branch}},enable=${{ github.ref_type == 'branch' && github.event_name != 'pull_request' }}
type=raw,value=latest,enable=${{ steps.latest-tag.outputs.tag == github.ref_name }}
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}.{{minor}}.{{patch}}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ${{ matrix.project }}.Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
deploy-production:
runs-on: ubuntu-latest
needs: containerize
if: ${{ github.repository_owner == 'OpenShock' && github.repository_owner == 'OpenShock' && github.ref_type == 'branch' && github.event_name != 'pull_request' && github.ref_name == 'master' }}
environment: production
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
.github
- uses: ./.github/actions/kubernetes-rollout-restart
with:
apiurl: ${{ secrets.KUBERNETES_APIURL }}
token: ${{ secrets.KUBERNETES_TOKEN }}
deployments: ${{ vars.DEPLOYMENT_NAMES }}
deploy-staging:
runs-on: ubuntu-latest
needs: containerize
if: ${{ github.repository_owner == 'OpenShock' && github.ref_type == 'branch' && github.event_name != 'pull_request' && github.ref_name == 'develop' }}
environment: staging
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
.github
- uses: ./.github/actions/kubernetes-rollout-restart
with:
apiurl: ${{ secrets.KUBERNETES_APIURL }}
token: ${{ secrets.KUBERNETES_TOKEN }}
deployments: ${{ vars.DEPLOYMENT_NAMES }}