-
Notifications
You must be signed in to change notification settings - Fork 0
164 lines (157 loc) · 6.56 KB
/
maven-deploy.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
name: Deploy
on:
workflow_call:
inputs:
image:
required: true
type: string
description: 'Image tag to deploy (example 2023.03.10.080433-f821119)'
cluster:
required: true
type: string
description: 'The cluster to deploy to (dev-fss|dev-gcp|prod-fss|prod-gcp)'
namespace:
required: false
type: string
description: 'The namespace to deploy to'
default: 'k9saksbehandling'
gar:
required: false
type: string
description: 'Deploy using Google Artifact Registry'
default: 'false'
naiserator_file:
required: false
type: string
description: 'The naiserator.yaml file name in .deploy/ to use'
default: 'nais/naiserator.yaml'
input_vars_file:
required: false
type: string
description: 'The naiserator.yaml file name in .deploy/ to use'
image_suffix:
required: false
type: string
description: 'The suffix of the image to be deployed'
default: ''
branch:
required: false
type: string
description: 'Alternative branch to deploy from'
override_image_name:
required: false
type: string
description: 'Complete image name to deploy. When set, image and image_suffix inputs are ignored. For use when image to upload don't have navikt/ prefix'
default: ''
env:
IMAGE: ${{ inputs.image }} # Brukes i naiserator.yaml
PROD_TAG: latest_${{ inputs.cluster }}_${{ inputs.namespace }}${{ inputs.image_suffix }}
jobs:
commit_url:
name: comparison_url
runs-on: 'ubuntu-latest'
if: contains(inputs.cluster, 'prod')
steps:
- name: Construct comparison url for last deploy
id: compare_commits
run: |
IMAGE_SHA=$(echo '${{ inputs.image }}' | awk -F'-' '{print $2}')
url="https://github.com/${{ github.repository }}/compare/${{ env.PROD_TAG }}...${IMAGE_SHA}" >> $GITHUB_OUTPUT
echo "Diff last deploy to ${{ env.PROD_TAG }} : <${url}>" >> $GITHUB_STEP_SUMMARY
deploy:
name: deploy
#Tillater én aktiv deployment per miljø. Dette fordi deployments som aldri blir kjørt feiler etter 30 dager og notifikasjon om feilet bygg
#cuncurrency gjør at pending deployments blir kansellert og får status cancel istedenfor failed, som da kan filtreres vekk (i mailklienten).
concurrency: ${{ inputs.cluster }}:${{ inputs.namespace }}${{ inputs.image_suffix }}
environment: ${{ inputs.cluster }}:${{ inputs.namespace }}${{ inputs.image_suffix }}
runs-on: 'ubuntu-latest'
permissions:
contents: write #for å lage tag
id-token: write
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # ratchet:actions/checkout@v4
with:
ref: ${{ (inputs.branch != '' && inputs.branch) || '' }}
- name: Deploy ${{ inputs.cluster }} fra Github
if: inputs.gar == 'false'
uses: nais/deploy/actions/deploy@v2
env:
PRINT_PAYLOAD: true
CLUSTER: ${{ inputs.cluster }}
RESOURCE: ${{ inputs.naiserator_file }}
VARS: ${{ inputs.input_vars_file }}
- name: Login GAR
uses: nais/login@4ce7445c84f09114e481a1d2c164f563e9e2ba8e # ratchet:nais/login@v0
if: inputs.gar == 'true'
id: login
with:
project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }}
identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }}
team: ${{ inputs.namespace }}
- name: Deploy ${{ inputs.cluster }} fra GAR
if: inputs.gar == 'true'
uses: nais/deploy/actions/deploy@v2
env:
PRINT_PAYLOAD: true
CLUSTER: ${{ inputs.cluster }}
IMAGE: ${{ inputs.override_image_name != '' && inputs.override_image_name || format('{0}/{1}{2}:{3}', steps.login.outputs.registry, github.repository, inputs.image_suffix, inputs.image) }}
RESOURCE: ${{ inputs.naiserator_file }}
VARS: ${{ inputs.input_vars_file }}
- name: Finn commit sha fra image navn
if: contains(inputs.cluster, 'prod') && success()
run: |
echo "TAG_COMMIT=$(echo '${{ inputs.image }}' | awk -F'-' '{print $2}')" >> $GITHUB_ENV
- name: Tagg prodsatt commit
if: contains(inputs.cluster, 'prod') && success()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const partial_sha = '${{ env.TAG_COMMIT }}'
const latestName = '${{ env.PROD_TAG }}'
const latestRef = 'tags/' + latestName
let tag_commit;
try {
const commit_path = 'GET /repos/' + context.repo.owner + '/' + context.repo.repo + '/commits/' + partial_sha
let commitDetails = await github.request(commit_path, {
owner: context.repo.owner,
repo: context.repo.repo,
ref: partial_sha
});
tag_commit = commitDetails.data.sha;
console.log("Fant full sha: " + tag_commit);
} catch (err) {
console.log("feilet ved henting av commit", err);
throw Error("feilet ved henting av commit: " + partial_sha);
}
console.log("ser om tagg=" + latestRef + " finnes...")
try {
let previous = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: latestRef
})
console.log(latestRef + " finnes! sletter...")
let slettet = await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: latestRef
})
console.log("slettet gamle " + latestName)
} catch (err) {
if (err.status == 404) {
console.log("fant ikke ref " + latestRef)
} else {
console.log(err)
throw Error("Feil ved henting/sletting av " + latestRef + ": " + err.status)
}
}
console.log("lager tagg=" + latestName + " på commit " + tag_commit)
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'refs/tags/' + latestName,
sha: tag_commit
}).then((res) => {
console.log("laget tagg=" + latestName)
})