-
Notifications
You must be signed in to change notification settings - Fork 0
114 lines (101 loc) · 3.5 KB
/
docker-publish.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
name: Build and publish container image
on:
push:
tags:
- 'cr*'
branches:
- 'master'
- 'dev'
pull_request:
branches:
- 'master'
- 'dev'
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
security-events: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1 # Shallow clone for faster checkout
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
buildkitd-flags: --debug
driver-opts: |
image=moby/buildkit:v0.12.0
network=host
- name: Get tag
id: tag
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
else
echo "tag=${GITHUB_REF_NAME}" >> $GITHUB_OUTPUT
fi
- name: Log in to the Container registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Cache Docker layers
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
# Build amd64 and arm64 in parallel
- name: Build and push amd64
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: ghcr.io/${{ github.repository }}:${{ steps.tag.outputs.tag }}-amd64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
build-args: |
BUILDKIT_INLINE_CACHE=1
- name: Build and push arm64
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ghcr.io/${{ github.repository }}:${{ steps.tag.outputs.tag }}-arm64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
build-args: |
BUILDKIT_INLINE_CACHE=1
# Create and push a multi-arch manifest
- name: Create and push manifest
if: github.event_name != 'pull_request'
run: |
docker buildx imagetools create -t ghcr.io/${{ github.repository }}:${{ steps.tag.outputs.tag }} \
ghcr.io/${{ github.repository }}:${{ steps.tag.outputs.tag }}-amd64 \
ghcr.io/${{ github.repository }}:${{ steps.tag.outputs.tag }}-arm64
# Move cache
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Run Trivy vulnerability scanner in parallel
uses: aquasecurity/trivy-action@master
if: github.event_name != 'pull_request'
with:
image-ref: ghcr.io/${{ github.repository }}:${{ steps.tag.outputs.tag }}-amd64
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results
uses: github/codeql-action/upload-sarif@v3
if: github.event_name != 'pull_request' && always()
with:
sarif_file: 'trivy-results.sarif'