-
Notifications
You must be signed in to change notification settings - Fork 0
162 lines (147 loc) · 5.55 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
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
on:
workflow_call:
inputs:
image-name:
required: true
type: string
image-tag:
required: true
type: string
outputs:
full-image-name:
description: "Full pushed image name including host/registry, name, and tag"
value: ${{ jobs.docker-build.outputs.full-image-name }}
jobs:
docker-build:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
timeout-minutes: 25
outputs:
full-image-name: ${{ steps.export-output.outputs.full-image-name }}
steps:
- name: Checkout workflow.transition.monitor
uses: actions/checkout@v4
with:
path: workflow.transition.monitor
- name: Make fake (empty) pacta-data folder
run: mkdir pacta-data
- name: Make fake (empty) templates.transition.monitor folder
run: mkdir templates.transition.monitor
- name: Prepare Environment
id: prepare-environment
env:
registry: "ghcr.io"
image_name: ${{ github.repository_owner }}/${{ inputs.image-name }}
run: |
NOW="$(date -u +'%Y%m%dT%H%M%SZ')"
echo "now=$NOW" >> $GITHUB_OUTPUT
echo "$NOW"
registry_image=$(
echo "$registry/$image_name" | \
tr '[:upper:]' '[:lower:]' \
)
REGISTRY_IMAGE=${registry_image}
echo "registry-image=$REGISTRY_IMAGE"
echo "registry-image=$REGISTRY_IMAGE" >> $GITHUB_OUTPUT
- name: Identify LABELs in dockerfile
id: custom-labels
run: |
DOCKERFILE_LABELS="$(grep "^LABEL" workflow.transition.monitor/Dockerfile | sed 's/^LABEL[[:space:]]*//')"
echo "$DOCKERFILE_LABELS"
echo "DOCKERFILE_LABELS<<EOF" >> $GITHUB_ENV
echo "$DOCKERFILE_LABELS" >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
echo 'dockerfile-labels<<EOF' >> $GITHUB_OUTPUT
echo "$DOCKERFILE_LABELS" >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
# Setup docker metadata, including tags and labels (and annotations)
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
env:
additional_image_tags: ${{ inputs.additional-image-tags }}
now: ${{ steps.prepare-environment.outputs.now }}
with:
images: ${{ steps.prepare-environment.outputs.registry-image }}
annotations:
${{ env.DOCKERFILE_LABELS }}
labels:
${{ env.DOCKERFILE_LABELS }}
tags: |
type=schedule,enable=true,pattern={{date 'YYYYMMDD[T]HHmmss[Z]' tz='UTC'}}
type=schedule,enable=true,pattern=nightly,priority=950
type=raw,value=latest,enable={{is_default_branch}}
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
${{ inputs.image-tag }}
${{ needs.read-config.outputs.now }},priority=1100
# Set up Dockerignore file
- name: Copy dockerignore
run: |
cp ./workflow.transition.monitor/.dockerignore.CIbuild .dockerignore
# set up our build environment
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
# Actually build the image (for a single architecture)!
- name: Build
id: build
uses: docker/build-push-action@v6
with:
context: .
file: ./workflow.transition.monitor/Dockerfile
# include the labels from the meta step
labels: ${{ steps.meta.outputs.labels }}
# the same for annotations
annotations: ${{ steps.meta.outputs.annotations }}
push: true
tags: ${{ steps.meta.outputs.tags }}
# Use the GitHub actions cache to speed up repeated builds
cache-from: type=gha
cache-to: type=gha,mode=min
# but don't cache the install pacta step.
no-cache-filters: install-pacta
- name: Export Outputs
id: export-outputs
env:
commit_time: ${{ github.event.pull_request.updated_at }},
git_sha: ${{ github.event.pull_request.head.sha }},
run: |
echo "DOCKER_METADATA_OUTPUT_JSON=$DOCKER_METADATA_OUTPUT_JSON"
jq --version
full_image_name=$(echo $DOCKER_METADATA_OUTPUT_JSON | jq '.tags[0]')
echo "full-image-name=$full_image_name"
echo "full-image-name=$full_image_name" >> $GITHUB_OUTPUT
mkdir -p /tmp/comment-json
json_filename=$( \
echo "comment-json-merge-${{ inputs.image-name }}-build-push.json" | \
tr '/' '-' \
)
echo "json-filename=$json_filename"
echo "json-filename=$json_filename" >> "$GITHUB_ENV"
json_file="/tmp/comment-json/$json_filename"
echo "json-file=$json_file"
jq \
-n \
--arg commit_time "$commit_time" \
--arg git_sha "$git_sha" \
--arg full_image_name "$full_image_name" \
'{
"commit_time": $commit_time,
"git_sha": $git_sha,
"image": ("`" + $full_image_name + "`")
}' \
>> $json_file
- name: Upload comment JSON
uses: actions/upload-artifact@v4
with:
name: ${{ env.json-filename }}
path: /tmp/comment-json/*
if-no-files-found: error
retention-days: 1