-
Notifications
You must be signed in to change notification settings - Fork 3
167 lines (148 loc) Β· 5.04 KB
/
build-and-deploy.yaml
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
name: Build and Deploy Skiperator
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * *' # at 12:00 AM every day
push:
branches: [main]
paths-ignore:
- doc/**
- samples/**
- README.md
- CONTRIBUTING.md
pull_request:
branches: [main]
paths-ignore:
- doc/**
- samples/**
- README.md
- CONTRIBUTING.md
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
RBAC_FILE_PATH: config/rbac/role.yaml
CRD_APP_FILE_PATH: config/crd/skiperator.kartverket.no_applications.yaml
CRD_JOB_FILE_PATH: config/crd/skiperator.kartverket.no_skipjobs.yaml
CRD_ROUTING_FILE_PATH: config/crd/skiperator.kartverket.no_routings.yaml
ARTIFACT_NAME: skiperator-artifact-${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
jobs:
build:
name: Build container image
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# Use sha for tags tags
type=sha,format=long
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
id: build-docker
uses: docker/build-push-action@v6
with:
context: .
push: ${{ !github.event.pull_request.draft }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Notice of image digest
run: echo "${{ steps.build-docker.outputs.digest }}" >> $GITHUB_STEP_SUMMARY
outputs:
image_digest: ${{ steps.build-docker.outputs.digest }}
pharos-scan:
if: (!github.event.pull_request.draft)
name: Run Pharos Security Scan
needs: build
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
actions: read
security-events: write
steps:
- name: Run Pharos
uses: kartverket/pharos@v0.2.1
with:
image_url: "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{needs.build.outputs.image_digest}}"
tfsec: false
generate:
if: (!github.event.pull_request.draft)
name: CRD and ClusterRole
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Golang environment
uses: actions/setup-go@v5
with:
go-version: '1.22.2'
- name: Generate CRD and ClusterRole
run: make generate
- name: Upload CRD and ClusterRole
uses: actions/upload-artifact@v4
with:
name: ${{ env.ARTIFACT_NAME }}
path: |
${{ env.RBAC_FILE_PATH }}
${{ env.CRD_APP_FILE_PATH }}
${{ env.CRD_JOB_FILE_PATH }}
${{ env.CRD_ROUTING_FILE_PATH }}
deploy-argo:
if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || (github.event_name == 'workflow_dispatch')
needs: [build, generate]
runs-on: ubuntu-latest
env:
BASE_DIR: ./bases/skiperator
TMP_FILE: tmp_kustomization.yaml
steps:
- name: Checkout apps repo
uses: actions/checkout@v4
with:
repository: kartverket/skip-apps
token: ${{ secrets.SKIPERATOR_DEPLOY_SECRET }}
- name: Download CRD and RBAC
uses: actions/download-artifact@v4
with:
name: ${{ env.ARTIFACT_NAME }}
path: config/
- name: Patch Image Digest
run: |
kubectl patch --type=merge --local \
-f $BASE_DIR/kustomization.yaml \
-p '{"images":[{"name":"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}","digest":"${{needs.build.outputs.image_digest}}"}]}' \
-o yaml > $BASE_DIR/$TMP_FILE
rm $BASE_DIR/kustomization.yaml
mv $BASE_DIR/$TMP_FILE $BASE_DIR/kustomization.yaml
- name: Update CRD and Role
run: |
cp -f -v $CRD_APP_FILE_PATH $BASE_DIR/crd.yaml
cp -f -v $CRD_JOB_FILE_PATH $BASE_DIR/skipjob-crd.yaml
cp -f -v $CRD_ROUTING_FILE_PATH $BASE_DIR/routing-crd.yaml
cp -f -v $RBAC_FILE_PATH $BASE_DIR/clusterrole.yaml
rm -rf config/
- name: Commit Changes to Repo
run: |
git config --global user.email "noreply@kartverket.no"
git config --global user.name "GitHub Actions"
git commit -aF- <<EOF
skiperator ${{ github.ref_name }}[${{ github.event.after }}]: ${{ github.event.head_commit.message }}
EOF
git push