-
Notifications
You must be signed in to change notification settings - Fork 0
177 lines (148 loc) · 5.18 KB
/
ci-cd.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
169
170
171
172
173
174
175
176
177
name: CI/CD
on: [ pull_request, push ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ghcr.io/${{ github.repository }}
DOCKER_IMAGE_TEST: app_test:${{ github.sha }}
DOCKER_IMAGE_TEST_ARTIFACT: test_image_${{ github.sha }}
DOCKER_IMAGE_TEST_PATH: /tmp/test_image.tar
jobs:
preflight:
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Running hadolint
uses: hadolint/hadolint-action@v1.6.0
with:
dockerfile: devops/docker/Dockerfile
- name: Running yaml lint
uses: "docker://pipelinecomponents/yamllint:0.21.1"
with:
args: yamllint .
- name: Running shellcheck
uses: "docker://pipelinecomponents/shellcheck:0.11.4"
with:
args: bash -c "find . -name \*.sh -type f -print0 | xargs -0 -P $(nproc) -r -n1 shellcheck"
- name: Run Snyk to check for vulnerabilities
uses: snyk/actions/python@master
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --sarif-file-output=snyk.sarif
- name: Upload result to GitHub Code Scanning
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: snyk.sarif
build-test-image:
runs-on: ubuntu-20.04
needs: preflight
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build test Docker image
uses: docker/build-push-action@v2
with:
context: .
file: devops/docker/Dockerfile
push: false
tags: ${{ env.DOCKER_IMAGE_TEST }}
target: test
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:_buildcache
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:_buildcache,mode=max
outputs: type=docker,dest=${{ env.DOCKER_IMAGE_TEST_PATH }}
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: ${{ env.DOCKER_IMAGE_TEST_ARTIFACT }}
path: ${{ env.DOCKER_IMAGE_TEST_PATH }}
retention-days: 1
lint:
runs-on: ubuntu-20.04
needs: build-test-image
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: ${{ env.DOCKER_IMAGE_TEST_ARTIFACT }}
path: /tmp
- name: Load Docker image
run: |
docker load --input ${{ env.DOCKER_IMAGE_TEST_PATH }}
- name: Lint
run: |
docker run ${{ env.DOCKER_IMAGE_TEST }} make lint
test:
runs-on: ubuntu-20.04
needs: build-test-image
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: ${{ env.DOCKER_IMAGE_TEST_ARTIFACT }}
path: /tmp
- name: Load Docker image
run: |
docker load --input ${{ env.DOCKER_IMAGE_TEST_PATH }}
- name: Test
env:
APP_TEST_IMAGE: ${{ env.DOCKER_IMAGE_TEST }}
run: |
docker-compose -f devops/ci/docker-compose.yml up --exit-code-from app
build:
runs-on: ubuntu-20.04
needs: [ lint, test ]
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=sha,priority=300
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
- name: Build production and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: devops/docker/Dockerfile
push: ${{ github.ref == 'refs/heads/main' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
target: production
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:_buildcache
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:_buildcache,mode=max
outputs:
tag: ${{ steps.meta.outputs.version }}
deploy:
runs-on: ubuntu-20.04
needs: build
if: ${{ github.ref == 'refs/heads/main' }}
steps:
- name: Deploy
run: curl -k -X POST -d "component=${{ secrets.DEPLOYER_COMPONENT }}&key=${{ secrets.DEPLOYER_KEY }}&tag=${{ needs.build.outputs.tag }}" ${{ secrets.DEPLOYER_HOST }}