-
Notifications
You must be signed in to change notification settings - Fork 0
174 lines (154 loc) · 6.05 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
name: Build
on:
push:
branches:
- main
- preprod/**
- dev/**
env:
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
- uses: gradle/actions/setup-gradle@v4
- 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
id-token: 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
- uses: gradle/actions/setup-gradle@v4
- 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: "apps/${{ matrix.project }}/build/libs/app.jar"
- name: build docker image
if: steps.app_jar.outputs.files_exists == 'true'
uses: nais/docker-build-push@v0
id: docker-build-push
with:
team: helsearbeidsgiver
image_suffix: im-${{ matrix.project }}
tag: ${{ needs.generate_vars.outputs.short_sha }}
dockerfile: Dockerfile
docker_context: apps/${{ matrix.project }}
- name: lag build fil
if: ${{ always() }}
run: |
echo ${{ steps.docker-build-push.outputs.image }} > "${{ matrix.project }}_build_${{ steps.docker-build-push.outcome }}.txt"
- name: laste opp build-fil
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
overwrite: true
retention-days: 1
name: "${{ matrix.project }}_build_result"
path: "${{ matrix.project }}_build_${{ steps.docker-build-push.outcome }}.txt"
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: Determine app configuration file
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
uses: andstor/file-existence-action@v3
with:
files: "${{ env.VARS }}"
- name: Fetch sources
uses: actions/checkout@v4
- name: create env vars
run: |
# VARS og IMAGE er miljøvariabler som leses automatisk av nais/deploy/actions/deploy
VARS_FILE="config/${{ matrix.project }}/${{ matrix.cluster }}.yml"
echo "VARS=$VARS_FILE" >> $GITHUB_ENV
- name: laste ned build-fil
uses: actions/download-artifact@v4
with:
name: "${{ matrix.project }}_build_result"
- name: ser om vi har nais config og at bygget gikk bra
id: deploy-files
uses: andstor/file-existence-action@v3
with:
files: "${{ env.VARS }}, ${{ matrix.project }}_build_success.txt"
- name: Sett docker image som env-var
id: sett_docker_image_var
run: echo "docker_image=$(cat ${{ matrix.project }}_build_success.txt)" >> $GITHUB_ENV
- name: Deploy
if: steps.deploy-files.outputs.files_exists == 'true'
uses: nais/deploy/actions/deploy@v2
env:
CLUSTER: ${{ matrix.cluster }}
IMAGE: ${{ env.docker_image }}
RESOURCE: config/nais.yml
VAR: team=helsearbeidsgiver,app=${{ matrix.project }}