-
Notifications
You must be signed in to change notification settings - Fork 2
99 lines (90 loc) · 3.13 KB
/
build-and-push-docker-image.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
name: "Build and Push Docker Images"
# Usage - copy into project .github/workflows/build-and-push-docker-image.yaml
#on:
# release:
# types: [published]
# pull_request:
# branches: ["main"] <-- customize to your own repo's default branch
#
#jobs:
# push_to_registry:
# name: Build
# uses: waggle-sensor/.github/.github/workflows/build-and-push-docker-image.yml@main
# secrets: inherit
# with:
# platforms: "linux/amd64,linux/arm64" <-- optional, to override default below
# context: . <-- optional, to override default below
# image: ${{ github.event.repository.name }} <-- optional, to override default below
on:
workflow_call:
inputs:
platforms:
required: false
type: string
default: "linux/amd64,linux/arm64"
context:
required: false
type: string
default: .
image:
required: false
type: string
default: ${{ github.event.repository.name }}
jobs:
build_and_push_docker_images:
name: Build and Push Docker Images
runs-on: ubuntu-20.04
steps:
- name: Set DOCKER_PUSH env var
run: |
if [[ -n "${{ secrets.DOCKER_USERNAME }}" && -n "${{ secrets.DOCKER_PASSWORD }}" ]]; then
echo "Workflow will push images."
echo "DOCKER_PUSH=true" >> $GITHUB_ENV
else
echo "Workflow will not push images."
echo "DOCKER_PUSH=false" >> $GITHUB_ENV
fi
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/metadata-action@v5
id: generate_meta
with:
images: |
waggle/${{ inputs.image }}
tags: |
type=ref,event=pr
type=semver,pattern={{major}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{version}}
- uses: docker/build-push-action@v5
id: build_image
if: env.DOCKER_PUSH == 'false'
with:
context: ${{ inputs.context }}
platforms: ${{ inputs.platforms }}
push: false
tags: ${{ steps.generate_meta.outputs.tags }}
labels: ${{ steps.generate_meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- uses: docker/login-action@v3
if: env.DOCKER_PUSH == 'true'
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- uses: docker/build-push-action@v5
id: build_and_push_image
if: env.DOCKER_PUSH == 'true'
with:
context: ${{ inputs.context }}
platforms: ${{ inputs.platforms }}
push: true
tags: ${{ steps.generate_meta.outputs.tags }}
labels: ${{ steps.generate_meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Add summary
if: env.DOCKER_PUSH
run: |
echo 'Tag and Digest: ${{ steps.generate_meta.outputs.version }}@${{ steps.build_and_push_image.outputs.digest }}' >> $GITHUB_STEP_SUMMARY