-
-
Notifications
You must be signed in to change notification settings - Fork 2
162 lines (152 loc) · 4.56 KB
/
docker.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
---
name: CI
on:
push:
branches:
- main
paths:
- "Dockerfile"
- "groovy/**"
- "files/**"
- "test/**"
- ".github/workflows/docker.yml"
pull_request:
branches:
- main
paths:
- "Dockerfile"
- "groovy/**"
- "files/**"
- "test/**"
- ".github/workflows/docker.yml"
schedule:
- cron: '0 7 * */1 1'
workflow_dispatch:
env:
REGISTRY: docker.io
OWNER: ${{ github.repository_owner }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
security-events: write
actions: read
contents: read
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Set Tag Names
run: |
echo "TAG=$(echo ${GITHUB_REF##*/})" >> $GITHUB_ENV
echo "DATE=v$(echo `date +'%Y.%m'`)" >> $GITHUB_ENV
echo "REPO_NAME=$(echo ${PWD##*/})" >> $GITHUB_ENV
- name: Login to DockerHub
uses: docker/login-action@v3
if: success()
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image - (MAIN)
uses: docker/build-push-action@v6
if: success()
env:
DOCKER_BUILDKIT: 1
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
build-args: |
BUILD_ID=${{ env.DATE }}
pull: true
push: true
tags: ${{ env.REGISTRY }}/${{ env.OWNER }}/${{ env.REPO_NAME }}:${{ env.TAG == 'main' && 'latest' || env.TAG }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.OWNER }}/${{ env.REPO_NAME }}:${{ env.TAG == 'main' && 'latest' || env.TAG }}
cache-to: type=inline
- name: Build and push Docker image - (DATE)
uses: docker/build-push-action@v6
if: ${{ github.event_name == 'schedule' || contains(github.ref, 'main') }}
env:
DOCKER_BUILDKIT: 1
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
build-args: |
BUILD_ID=${{ env.DATE }}
pull: true
push: true
tags: ${{ env.REGISTRY }}/${{ env.OWNER }}/${{ env.REPO_NAME }}:${{ env.DATE }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.OWNER }}/${{ env.REPO_NAME }}:${{ env.DATE }}
cache-to: type=inline
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
if: success()
with:
scan-type: 'config'
scan-ref: '.'
format: 'table'
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
continue-on-error: true
if: success()
with:
image-ref: ${{ env.REGISTRY }}/${{ env.OWNER }}/${{ env.REPO_NAME }}:${{ env.TAG == 'main' && 'latest' || env.TAG }}
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
continue-on-error: true
if: success()
with:
sarif_file: 'trivy-results.sarif'
category: 'Trivy'
k8s-test:
runs-on: ubuntu-latest
needs: [build]
if: ${{ ! contains(github.ref, 'main') && github.event_name != 'schedule' }}
steps:
- name: Create k8s Kind Cluster
uses: helm/[email protected]
- name: Checkout
uses: actions/checkout@v4
- name: Run K8s test
run: |
kubectl cluster-info
export NS=`cat deployment/kustomization.yml | grep namespace | awk '{ print $2 }'`
if [ -z "$NS" ]; then
export NS='cicd'
fi
kubectl create namespace $NS
kubectl apply -n $NS -k deployment/
kubectl get all -A
auto-approve:
runs-on: ubuntu-latest
needs: [build, k8s-test]
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Auto Approve PR
uses: actions/github-script@v7
with:
script: |
github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
event: "APPROVE"
})
auto-preview:
runs-on: ubuntu-latest
needs: [auto-approve]
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Add Preview Label
uses: actions/github-script@v7
with:
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['preview']
})