-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (162 loc) · 6.64 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
name: Build
on:
push:
branches:
- main
- preprod/**
- dev/**
env:
DOCKER_REGISTRY: ghcr.io
DOCKER_IMAGE_PREFIX: ${{ github.repository }}/im-
ORG_GRADLE_PROJECT_githubPassword: ${{ secrets.GITHUB_TOKEN }}
jobs:
generate_vars:
runs-on: ubuntu-latest
outputs:
short_sha: ${{ steps.set_short_sha.outputs.short_sha }}
matrix: ${{ steps.set_matrix.outputs.matrix }}
is_matrix_empty: ${{ steps.set_matrix.outputs.is_matrix_empty }}
deploy_matrix: ${{ steps.set_matrix.outputs.deploy_matrix }}
is_deploy_matrix_empty: ${{ steps.set_matrix.outputs.is_deploy_matrix_empty }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Cache gradle wrapper
uses: actions/cache@v4
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
- name: Cache gradle caches
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/build.gradle.kts') }}
restore-keys: |
${{ runner.os }}-gradle-caches-
- name: Determine short SHA
id: set_short_sha
run: |
SHORT_SHA=$(echo ${{ github.sha }} | cut -c 1-7)
echo "short_sha=$SHORT_SHA" >> $GITHUB_OUTPUT
- name: Determine changes files
run: |
# Create a comma-separated list of changed files for use in build.gradle.kts
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r -m $GITHUB_SHA | tr '\r\n' ',' | sed -e 's/,$//' | tr -d '"')
echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV
- name: Determine projects to deploy
id: set_matrix
run: |
MATRIX=$(./gradlew -q buildMatrix --console=plain)
echo $MATRIX
DEPLOY_MATRIX=$(./gradlew -q deployMatrixDev --console=plain)
echo $DEPLOY_MATRIX
MATRIX_SIZE=$(echo $MATRIX | jq '.project|length')
DEPLOY_MATRIX_SIZE=$(echo $DEPLOY_MATRIX | jq '.project|length')
if [ "$MATRIX_SIZE" == '0' ]; then
echo "Empty matrix"
echo "matrix=[]" >> $GITHUB_OUTPUT # to prevent error because matrix is empty
echo "is_matrix_empty=true" >> $GITHUB_OUTPUT
else
echo Setting matrix to $MATRIX
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
echo "is_matrix_empty=false" >> $GITHUB_OUTPUT
fi
if [ "$DEPLOY_MATRIX_SIZE" == '0' ]; then
echo "Empty deploy matrix"
echo "deploy_matrix=[]" >> $GITHUB_OUTPUT # to prevent error because matrix is empty
echo "is_deploy_matrix_empty=true" >> $GITHUB_OUTPUT
else
echo Setting deploy matrix to $DEPLOY_MATRIX
echo "deploy_matrix=$DEPLOY_MATRIX" >> $GITHUB_OUTPUT
echo "is_deploy_matrix_empty=false" >> $GITHUB_OUTPUT
fi
build:
runs-on: ubuntu-latest
permissions:
packages: write
needs: generate_vars
if: needs.generate_vars.outputs.is_matrix_empty == 'false'
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.generate_vars.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: 21
distribution: temurin
cache: gradle
- name: Cache gradle wrapper
uses: actions/cache@v4
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
- name: Cache gradle caches
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/build.gradle.kts') }}
restore-keys: |
${{ runner.os }}-gradle-caches-
- name: Test and build
run: ./gradlew ${{ matrix.project }}:test ${{ matrix.project }}:build
- name: Check app.jar existence
id: app_jar
uses: andstor/file-existence-action@v3
with:
files: "${{ matrix.project }}/build/libs/app.jar"
- name: Create docker tag
if: steps.app_jar.outputs.files_exists == 'true'
run: |
echo "IMAGE=$DOCKER_REGISTRY/${DOCKER_IMAGE_PREFIX}${{ matrix.project }}:${{ needs.generate_vars.outputs.short_sha }}" >> $GITHUB_ENV
- name: Build docker image
if: steps.app_jar.outputs.files_exists == 'true'
run: docker build ${{ matrix.project }} --pull -t $IMAGE -f Dockerfile
- name: Push docker image
if: steps.app_jar.outputs.files_exists == 'true'
run: |
echo ${{ secrets.GITHUB_TOKEN }} | docker login --username $GITHUB_REPOSITORY --password-stdin https://$DOCKER_REGISTRY
docker push $IMAGE
deploy:
runs-on: ubuntu-latest
permissions:
id-token: write
needs: [generate_vars, build]
if: needs.generate_vars.outputs.is_deploy_matrix_empty == 'false'
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.generate_vars.outputs.deploy_matrix) }}
concurrency:
group: deploy-dev-${{ matrix.project }}
steps:
- name: Check if docker image exists
run: |
LAYERS=$(curl -X GET https://$DOCKER_REGISTRY/${DOCKER_IMAGE_PREFIX}${{ matrix.project }}/manifests/${{ needs.generate_vars.outputs.short_sha }} -u $GITHUB_ACTOR:${{ secrets.GITHUB_TOKEN }} | jq '.layers')
if [ "$LAYERS" == 'null' ]; then
echo "IMAGE_EXISTS=false" >> $GITHUB_ENV
else
echo "IMAGE_EXISTS=true" >> $GITHUB_ENV
fi
- name: Fetch sources
if: env.IMAGE_EXISTS == 'true'
uses: actions/checkout@v4
- name: Determine app configuration file
if: env.IMAGE_EXISTS == 'true'
run: |
CONFIG_FILE="config/${{ matrix.project }}/${{ matrix.cluster }}.yml"
echo "VARS=$CONFIG_FILE" >> $GITHUB_ENV # VARS-variable is used by nais/deploy/actions/deploy
- name: Confirm app configuration file existence
id: confirm_config_file
if: env.IMAGE_EXISTS == 'true'
uses: andstor/file-existence-action@v3
with:
files: "${{ env.VARS }}"
- name: Deploy
if: env.IMAGE_EXISTS == 'true' && steps.confirm_config_file.outputs.files_exists == 'true'
uses: nais/deploy/actions/deploy@v2
env:
CLUSTER: ${{ matrix.cluster }}
IMAGE: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_PREFIX }}${{ matrix.project }}:${{ needs.generate_vars.outputs.short_sha }}
RESOURCE: config/nais.yml
VAR: app=${{ matrix.project }}