This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (143 loc) · 5.84 KB
/
build_and_deploy_dev.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
name: Build and deploy to dev
on:
push:
branches:
- 'master'
workflow_dispatch:
inputs:
environment:
description: 'Miljø'
required: true
default: 'q2'
type: choice
options:
- q1
- q2
bump_patch:
description: 'Velg "bump" her for å bygge release og øke patchversjon på snapshot. Kun for master branch.'
default: ''
type: choice
options:
- ''
- bump
env:
REPONAME: ${{ github.event.repository.name }}
APPNAME: ${{ github.event.repository.name }}-frontend
ENVIRONMENT: ${{ github.event.inputs.environment }}
CLUSTER: dev-gcp
NAIS_FILE: .nais/nais.yaml
MAIN_BRANCH: refs/heads/master
jobs:
build:
name: Build and deploy
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Check if correct branch
if: github.event.inputs.bump_patch == 'bump' && github.ref != env.MAIN_BRANCH
run: |
echo "Release skal kun bygges fra ${{ env.MAIN_BRANCH }} branch!"
exit 1
- name: Set default environment for push
if: github.event_name == 'push' && github.ref == env.MAIN_BRANCH
run: echo "ENVIRONMENT=q2" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '16'
- name: Read version from package.json
id: packageVersion
uses: martinbeentjes/npm-get-version-action@main
# Setter timestamp, eks. -20220104111118
- name: Set snapshot timestamp
if: github.event.inputs.bump_patch != 'bump'
run: |
echo "TIMESTAMP=-$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV
# Version blir eksisterende versjon fra package.json uten -SNAPSHOT
# For release: eks 1.0.1
# For snapshot: eks 1.0.1-20220104111118
- name: Calculate release version
run: |
echo "RELEASE_VERSION=$(echo "${{ steps.packageVersion.outputs.current-version }}${{ env.TIMESTAMP }}" | sed 's/-SNAPSHOT//g')" >> $GITHUB_ENV
- name: Set vars
run: |
echo "IMAGE=ghcr.io/${{ github.repository }}/${{ env.REPONAME }}:${{ env.RELEASE_VERSION }}" >> $GITHUB_ENV
- name: Set release version in package.json
run: |
jq '.version="${{ env.RELEASE_VERSION }}"' package.json > package.json.new
mv package.json.new package.json
- name: Authenticate with registry
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> ~/.npmrc
echo "@navikt:registry=https://npm.pkg.github.com" >> ~/.npmrc
- name: Install npm packages
run: npm ci --legacy-peer-deps
- name: Build application
run: npm run build
- name: Test application
run: npm test
- name: Build Docker image
run: |
npm run build
docker build -t ${{ env.IMAGE }} .
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push Docker image
run: |
docker push ${{ env.IMAGE }}
- name: Deploy to dev
if: env.ENVIRONMENT == 'q1' || env.ENVIRONMENT == 'q2' || (github.event_name == 'push' && github.ref == env.MAIN_BRANCH)
uses: nais/deploy/actions/deploy@v2
env:
APIKEY: ${{ secrets.NAIS_DEPLOY_APIKEY }}
PRINT_PAYLOAD: true
CLUSTER: ${{ env.CLUSTER }}
RESOURCE: ${{ env.NAIS_FILE }}
VAR: image=${{ env.IMAGE }},appName=${{ env.APPNAME }}
VARS: .nais/vars-${{ env.ENVIRONMENT }}.yaml
WAIT: false
- name: Archive files in .nais
if: github.event.inputs.bump_patch == 'bump' && github.ref == env.MAIN_BRANCH
uses: thedoctor0/zip-release@master
with:
path: .nais
filename: '${{ env.REPONAME }}-${{ env.RELEASE_VERSION }}.zip'
- name: Create release
if: github.event.inputs.bump_patch == 'bump' && github.ref == env.MAIN_BRANCH
uses: ncipollo/release-action@v1
with:
tag: 'v${{ env.RELEASE_VERSION }}'
artifacts: '${{ env.REPONAME }}-${{ env.RELEASE_VERSION }}.zip'
token: ${{ secrets.GITHUB_TOKEN }}
# Next version blir eksisterende versjon + 0.0.1, med postfix -SNAPSHOT
- name: Calculate next version
if: github.event.inputs.bump_patch == 'bump' && github.ref == env.MAIN_BRANCH
run: |
echo "NEXT_RELEASE_VERSION=$(echo "${{ steps.packageVersion.outputs.current-version }}" | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g' | sed 's/$/-SNAPSHOT/')" >> $GITHUB_ENV
# Hvis det er ny versjon, oppdater package.json og utfør git commit/push
- name: Set next version in package.json
if: github.event.inputs.bump_patch == 'bump' && github.ref == env.MAIN_BRANCH
run: |
jq '.version="${{ env.NEXT_RELEASE_VERSION }}"' package.json > package.json.new
mv package.json.new package.json
- name: Commit bumped version
if: github.event.inputs.bump_patch == 'bump' && github.ref == env.MAIN_BRANCH
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -m "Bump version fra Github Actions" -a
git tag -a ${{ env.REPONAME }}-${{ env.NEXT_RELEASE_VERSION }} -m ${{ env.REPONAME }}-${{ env.NEXT_RELEASE_VERSION }}
- name: Push changes
if: github.event.inputs.bump_patch == 'bump' && github.ref == env.MAIN_BRANCH
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}