-
Notifications
You must be signed in to change notification settings - Fork 0
127 lines (114 loc) · 4.22 KB
/
reusable-docker-build-push.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
name: Build and push images to registry
env:
OWNER: ${{ github.repository }}
REGISTRY: ghcr.io
on:
workflow_call:
inputs:
image:
description: Image name
required: true
type: string
folder:
description: Location and build context of the Dockerfile folder
required: true
type: string
buildargs:
description: build-args for docker build action (new line delimitated yaml like list)
required: false
type: string
default: ""
outputs:
tags:
description: "tags"
value: ${{ jobs.build-push.outputs.tags }}
labels:
description: "labels"
value: ${{ jobs.build-push.outputs.labels }}
image:
description: "image"
value: ${{ jobs.build-push.outputs.image }}
metadata:
description: metadata of image
value: ${{ jobs.build-push.outputs.metadata }}
jobs:
build-push:
#runs-on: ubuntu-latest
#runs-on: self-hosted
runs-on: gha-runner-scale-set-dind
outputs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
metadata: ${{ steps.build.outputs.metadata }}
image: ${{ inputs.image }}
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
# disable attestations for now, see https://github.com/orgs/community/discussions/45969
uses: docker/setup-buildx-action@15c905b16b06416d2086efa066dd8e3a35cc7f98
with:
driver-opts: "image=moby/buildkit:v0.10.5"
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: create image name based on build args
id: imagename
run: |
image_name=`echo "${{inputs.buildargs}}" | awk -F'=' '{print $2}' | tr -d '[:blank:]' | tr -s '\n' '-' | sed 's/-$//'`
if [ "$image_name" != "" ]; then image_name="-$image_name" ; fi
echo "$image_name"
echo "imagename=$image_name" >> $GITHUB_OUTPUT
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.OWNER }}/${{ inputs.image }}${{ steps.imagename.outputs.imagename }}
#images: ${{ env.REGISTRY }}/${{ env.OWNER }}/${{ inputs.image }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
labels: |
org.opencontainers.image.title=${{ inputs.image }}
org.opencontainers.image.src=${{ github.event.repository.html_url }}
- name: Show metadata
run: |
echo "tags: ${{ steps.meta.outputs.tags }}"
echo "labels: ${{ steps.meta.outputs.labels }}"
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v4
with:
context: ./${{ inputs.folder }}/.
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: ${{ inputs.buildargs }}
- name: pull to harbor
if: ${{ github.event_name != 'pull_request' }}
run: |
tag=$(echo "${{ steps.meta.outputs.tags }}" | grep sha | head -n 1 | awk -F ':' '{print $2}')
tags=$(echo "${{ steps.meta.outputs.tags }}")
for tag in $tags
do
docker image pull harbor.ki.fh-swf.de/$tag
done
#- name: setoutput
# id: setoutput
# run: |
# echo "tags=${{ steps.meta.outputs.tags }}" >> "$GITHUB_OUTPUT"
# echo "lables=${{ steps.meta.outputs.lables }}" >> "$GITHUB_OUTPUT"
# echo "tags=${{ inputs.image }}" >> "$GITHUB_OUTPUT"