-
Notifications
You must be signed in to change notification settings - Fork 2
141 lines (136 loc) · 5.17 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
name: Build
on:
push:
branches:
- main
env:
DOCKER_REGISTRY: europe-north1-docker.pkg.dev
RESOURCE: config/nais.yml
TEAM: tbd
jobs:
generate_vars:
runs-on: ubuntu-latest
outputs:
team: ${{ steps.set-vars.outputs.team }}
tag: ${{ steps.set-vars.outputs.tag }}
matrix: ${{ steps.set-matrix.outputs.matrix }}
deployMatrix: ${{ steps.set-matrix.outputs.deployMatrix }}
emptyMatrix: ${{ steps.set-matrix.outputs.emptyMatrix }}
emptyDeployMatrix: ${{ steps.set-matrix.outputs.emptyDeployMatrix }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21.x'
cache: 'gradle'
- name: cache gradle wrapper
uses: actions/cache@v3
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
- id: set-vars
run: |
TAG=$(echo ${{ github.sha }} | cut -c1-7)
echo "team=${TEAM}" >> $GITHUB_OUTPUT
echo "tag=${TAG}" >> $GITHUB_OUTPUT
- id: set-changed-files
run: |
# create a comma-separated list of changed files
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r $GITHUB_SHA | tr '\r\n' ',' | sed -e 's/,$//')
echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV
- id: set-matrix
run: |
MATRIX=$(./gradlew -q buildMatrix --console=plain)
DEPLOY_MATRIX=$(./gradlew -q deployMatrix --console=plain)
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
echo "emptyMatrix=true" >> $GITHUB_OUTPUT
else
echo Setting matrix to $MATRIX
echo "matrix=${MATRIX}" >> $GITHUB_OUTPUT
echo "emptyMatrix=false" >> $GITHUB_OUTPUT
fi
if [ "$DEPLOY_MATRIX_SIZE" == '0' ]; then
echo "Empty deploy matrix"
echo "deployMatrix=[]" >> $GITHUB_OUTPUT
echo "emptyDeployMatrix=true" >> $GITHUB_OUTPUT
else
echo Setting deploy matrix to $DEPLOY_MATRIX
echo "deployMatrix=${DEPLOY_MATRIX}" >> $GITHUB_OUTPUT
echo "emptyDeployMatrix=false" >> $GITHUB_OUTPUT
fi
build:
needs: generate_vars
permissions:
packages: write
id-token: write
name: build
runs-on: ubuntu-latest
if: needs.generate_vars.outputs.emptyMatrix == 'false'
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.generate_vars.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21.x'
cache: 'gradle'
- name: cache gradle wrapper
uses: actions/cache@v3
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
- name: test and build
run: ./gradlew ${{ matrix.project }}:build
- name: Check app.jar existence
id: app_jar
uses: andstor/file-existence-action@v2
with:
files: "${{ 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: ${{ needs.generate_vars.outputs.team }}
image_suffix: ${{ matrix.project }}
tag: ${{ needs.generate_vars.outputs.tag }}
docker_context: ${{ matrix.project }}
project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }}
identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }}
deploy:
needs: [generate_vars, build]
name: deploy
if: always() && needs.generate_vars.outputs.emptyDeployMatrix == 'false'
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.generate_vars.outputs.deployMatrix) }}
runs-on: ubuntu-latest
steps:
- name: create env vars
run: |
VARS_FILE="config/${{ matrix.project }}/${{ matrix.cluster }}.yml"
echo "VARS=$VARS_FILE" >> $GITHUB_ENV
echo "IMAGE=${{ env.DOCKER_REGISTRY }}/${{ vars.NAIS_MANAGEMENT_PROJECT_ID }}/${{ needs.generate_vars.outputs.team }}/${{ github.event.repository.name }}-${{ matrix.project }}:${{ needs.generate_vars.outputs.tag }}" >> $GITHUB_ENV
- name: Fetch sources
uses: actions/checkout@v4
- name: Check for vars yml
id: vars_file
uses: andstor/file-existence-action@v2
with:
files: "${{ env.VARS }}"
- name: deploy
if: steps.vars_file.outputs.files_exists == 'true'
uses: nais/deploy/actions/deploy@v1
env:
CLUSTER: ${{ matrix.cluster }}
APIKEY: ${{ secrets.NAIS_DEPLOY_APIKEY }}
VAR: team=${{ needs.generate_vars.outputs.team }},app=${{ matrix.project }},git_sha=${{ github.sha }}