-
-
Notifications
You must be signed in to change notification settings - Fork 3
220 lines (181 loc) · 6.92 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
name: Build microservices
on:
push:
branches: [ staging, prod ]
jobs:
changed-microservices:
name: Get changed microservices & set tags
runs-on: ubuntu-latest
outputs:
microservices: ${{ steps.microservices.outputs.list }}
microservices-spaced: ${{ steps.microservices.outputs.list-spaced }}
branch: ${{ steps.other-params.outputs.branch }}
docker-tag: ${{ steps.other-params.outputs.docker-tag }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 30
- run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
- name: Branch name & docker tag & latest release sha
id: other-params
shell: bash
run: |
# ignore errors
set +e
branch=$(echo ${GITHUB_REF#refs/heads/})
git fetch --tags origin $branch
tag=$(git describe --tags --abbrev=0 2>/dev/null)
sha=$(git --no-pager log -1 --format="%H" "$tag" 2>/dev/null || git rev-list --max-parents=0 HEAD)
echo "branch=$branch" >> $GITHUB_OUTPUT
echo "sha=$sha" >> $GITHUB_OUTPUT
# define docker tag
if [ "$branch" == "staging" ]; then
echo "docker-tag=-$branch" >> $GITHUB_OUTPUT
else
echo "docker-tag=" >> $GITHUB_OUTPUT
fi
echo "tag=$tag && sha=$sha"
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35
with:
json: true
dir_names: true
dir_names_max_depth: 3
base_sha: ${{ steps.other-params.outputs.sha }}
- id: microservices
run: npx @lomray/microservices-cli changed-microservices
env:
FILES: '${{ steps.changed-files.outputs.all_changed_files }}'
checks-compile:
name: Code checks & compile & npm publish
needs: changed-microservices
runs-on: ubuntu-latest
if: needs.changed-microservices.outputs.microservices-spaced != ''
env:
ONLY: ${{ needs.changed-microservices.outputs.microservices-spaced }}
steps:
- uses: actions/checkout@v3
- run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
- uses: actions/setup-node@v3
with:
node-version: '18.13.0'
cache: 'npm'
- name: Install global dependencies
run: npm ci
- name: Install microservices dependencies
run: npx @lomray/microservices-cli global-install
- name: Check microservice eslint
run: npx @lomray/microservices-cli lint
- name: Check microservice typescript
run: npx @lomray/microservices-cli ts-check
- name: Run microservices tests with coverage
run: npx @lomray/microservices-cli test --coverage
- name: Compile microservices
run: npx @lomray/microservices-cli build
# note: this also update package.json version
- name: Publish npm packages / create github release
run: npx @lomray/microservices-cli semantic-release
env:
GIT_BRANCH: ${{ needs.changed-microservices.outputs.branch }}
PUBLISH_PACKAGE: ${{ secrets.PUBLISH_PACKAGE }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Archive microservices artifacts
uses: actions/upload-artifact@v3
with:
name: microservice-artifacts
path: |
microservices/*/lib/*
# keep update version
microservices/*/package.json
- name: Archive Dockerfile
uses: actions/upload-artifact@v3
with:
name: dockerfile
path: node_modules/@lomray/microservice-config/Dockerfile-nodejs
- name: Archive code coverage artifacts
uses: actions/upload-artifact@v3
# only for staging
if: ${{ needs.changed-microservices.outputs.branch }} == "staging"
with:
name: coverage-artifacts
path: |
microservices/*/coverage/*
build-docker:
name: Build docker images
needs: [changed-microservices, checks-compile]
runs-on: ubuntu-latest
if: needs.changed-microservices.outputs.microservices != ''
strategy:
matrix:
microservice: ${{ fromJson(needs.changed-microservices.outputs.microservices) }}
env:
MICROSERVICE_NAME: ${{ matrix.microservice }}
WORK_DIR: "microservices/${{ matrix.microservice }}"
steps:
- name: Checkout
uses: actions/checkout@v3
- run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
- uses: actions/download-artifact@v3
with:
name: microservice-artifacts
path: microservices
- uses: actions/download-artifact@v3
with:
name: dockerfile
- id: package-version
run: npx @lomray/microservices-cli package-version
env:
WORK_DIR: ${{ env.WORK_DIR }}
- name: Detect dockerfile
id: docker-file
run: npx @lomray/microservices-cli detect-docker-file
env:
WORK_DIR: '${{ env.WORK_DIR }}'
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
env:
FROM_TAG: "latest${{ needs.changed-microservices.outputs.docker-tag }}"
with:
name: "Lomray-Software/microservices/${{ env.MICROSERVICE_NAME }}"
tags: "latest${{ needs.changed-microservices.outputs.docker-tag }},${{ steps.package-version.outputs.version}}"
dockerfile: ${{ steps.docker-file.outputs.path }}
context: ${{ env.WORK_DIR }}
buildoptions: "--compress --force-rm --no-cache"
buildargs: FROM_TAG
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
sonarcube:
name: Sonarcube
needs: [changed-microservices, checks-compile]
runs-on: ubuntu-latest
# only for staging
if: ${{ (needs.changed-microservices.outputs.microservices != '') && (needs.changed-microservices.outputs.branch == 'staging') }}
strategy:
matrix:
microservice: ${{ fromJson(needs.changed-microservices.outputs.microservices) }}
env:
MICROSERVICE_NAME: ${{ matrix.microservice }}
WORK_DIR: "microservices/${{ matrix.microservice }}"
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/download-artifact@v3
with:
name: coverage-artifacts
path: microservices
- id: package-version
run: npx @lomray/microservices-cli package-version
env:
WORK_DIR: ${{ env.WORK_DIR }}
- uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_CLOUD_TOKEN }}
with:
projectBaseDir: ${{ env.WORK_DIR }}
args: >
-Dsonar.projectVersion=${{ steps.package-version.outputs.version }}