1
+ # # This workflow is either triggered when:
2
+ on :
3
+ release :
4
+ types : [ published ]
5
+ push :
6
+ branches :
7
+ - master
8
+ paths-ignore :
9
+ - " **/*.md"
10
+ - " Makefile"
11
+ - " .mvn"
12
+ - " .gitignore"
13
+
14
+ env :
15
+ REGISTRY : europe-north1-docker.pkg.dev/artifact-registry-5n/dapla-stat-docker/maskinporten-guardian
16
+ IMAGE : maskinporten-guardian
17
+
18
+ jobs :
19
+ setup-build-push-deploy :
20
+ name : Set up variables to build/push or deploy
21
+ runs-on : ubuntu-latest
22
+ permissions :
23
+ contents : read
24
+ id-token : write
25
+
26
+ outputs :
27
+ nais-image : ${{steps.image-tag.outputs.nais_image}}
28
+ nais-cluster : ${{steps.nais-deploy-vars.outputs.cluster}}
29
+ nais-config-path : ${{steps.nais-deploy-vars.outputs.nais_config_path}}
30
+ tags : ${{steps.image-tag.outputs.tags}}
31
+
32
+ steps :
33
+ - uses : actions/checkout@v4
34
+
35
+ - name : Generate image tags
36
+ id : image-tag
37
+ run : |
38
+ if [ ${{ github.event_name }} == "release" ]; then
39
+ RELEASE_VERSION=${GITHUB_REF#refs/*/}
40
+ semver=${REGISTRY}/${IMAGE}:v${RELEASE_VERSION}
41
+ major_minor_version=${REGISTRY}/${IMAGE}:v$(echo "$RELEASE_VERSION" | cut -d'.' -f1-2)
42
+ major_version=${REGISTRY}/${IMAGE}:v$(echo "$RELEASE_VERSION" | cut -d'.' -f1)
43
+ latest=${REGISTRY}/${IMAGE}:latest
44
+
45
+ ## NAIS image is the image used by NAIS for deployment
46
+ echo "nais_image=${semver}" >> "$GITHUB_OUTPUT"
47
+
48
+ tags=${latest},${semver},${major_minor_version},${major_version}
49
+ echo "tags=${tags}" >> "$GITHUB_OUTPUT"
50
+ else
51
+ git_sha_short="$(git rev-parse --short ${{github.sha}})"
52
+ current_sha_tag=${REGISTRY}/${IMAGE}:${{github.event.repository.default_branch}}-$git_sha_short
53
+ latest=${REGISTRY}/${IMAGE}:latest
54
+
55
+ ## NAIS image is the image used by NAIS for deployment
56
+ echo "nais_image=${current_sha_tag}" >> "$GITHUB_OUTPUT"
57
+ tags=${latest},${current_sha_tag}
58
+ echo "tags=${tags}" >> "$GITHUB_OUTPUT"
59
+ fi
60
+
61
+ - name : Generate NAIS deploy variables
62
+ id : nais-deploy-vars
63
+ run : |
64
+ if [[ ${{github.event_name}} == "release" ]]; then
65
+ echo "cluster=prod" >> "$GITHUB_OUTPUT"
66
+ echo "nais_config_path=.nais/prod.yaml" >> "$GITHUB_OUTPUT"
67
+ else
68
+ echo "cluster=test" >> "$GITHUB_OUTPUT"
69
+ echo "nais_config_path=.nais/test.yaml" >> "$GITHUB_OUTPUT"
70
+ fi
71
+
72
+ build-push :
73
+ name : Build and push to registries
74
+ # If triggering event is release, the commits on 'master' should build
75
+ # the image
76
+ needs : setup-build-push-deploy
77
+ runs-on : ubuntu-latest
78
+ permissions :
79
+ contents : read
80
+ id-token : write
81
+
82
+ steps :
83
+ - uses : actions/checkout@v4
84
+
85
+ - name : Set up JDK 21
86
+ uses : actions/setup-java@v4
87
+ with :
88
+ java-version : 21
89
+ distribution : temurin
90
+ cache : maven
91
+
92
+ - name : Authenticate to Google Cloud
93
+ id : auth
94
+ uses : google-github-actions/auth@v2
95
+ with :
96
+ workload_identity_provider : " projects/848539402404/locations/global/workloadIdentityPools/gh-actions/providers/gh-actions"
97
+ service_account : " gh-actions-dapla-stat@artifact-registry-5n.iam.gserviceaccount.com"
98
+ token_format : access_token
99
+
100
+ - name : Set up Docker Buildx
101
+ id : buildx
102
+ uses : docker/setup-buildx-action@v3
103
+
104
+ - name : Login to Artifact Registry
105
+ uses : docker/login-action@v3
106
+ with :
107
+ registry : ${{ env.REGISTRY }}
108
+ username : " oauth2accesstoken"
109
+ password : " ${{ steps.auth.outputs.access_token }}"
110
+
111
+ - name : Maven build and install
112
+ run : |
113
+ if [[ ${{github.event_name}} == "push" ]]; then
114
+ mvn --batch-mode -P artifact-registry,github deploy
115
+ else
116
+ mvn --batch-mode clean install
117
+ fi
118
+
119
+ - name : Docker meta
120
+ id : docker_metadata
121
+ uses : docker/metadata-action@v5
122
+ with :
123
+ images : ${{ env.REGISTRY }}/${{ env.IMAGE }}
124
+
125
+ - name : Build and push docker image to Artifact Registry
126
+ id : docker_build
127
+ uses : docker/build-push-action@v5
128
+ with :
129
+ context : .
130
+ file : Dockerfile
131
+ push : true
132
+ tags : ${{ needs.setup-build-push-deploy.outputs.tags }}
133
+ labels : ${{ steps.docker_metadata.outputs.labels }}
134
+
135
+ deploy :
136
+ name : Deploy to NAIS
137
+ needs : [build-push, setup-build-push-deploy]
138
+ uses : ./.github/workflows/deploy-to-nais.yml
139
+ with :
140
+ image : ${{needs.setup-build-push-deploy.outputs.nais-image}}
141
+ cluster : ${{needs.setup-build-push-deploy.outputs.nais-cluster}}
142
+ nais-config-path : ${{needs.setup-build-push-deploy.outputs.nais-config-path}}
0 commit comments