-
Notifications
You must be signed in to change notification settings - Fork 12
/
action.yml
129 lines (105 loc) · 4.79 KB
/
action.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
name: "push-to-ghcr"
author: "@macbre"
description: "This action simplifies pushes of Docker images ot ghcr.io repository"
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#branding
branding:
icon: "arrow-up-circle"
color: "blue"
inputs:
github_token:
description: "Your secrets.GITHUB_TOKEN"
required: true
image_name:
description: "Image name, e.g. my-user-name/my-repo"
required: true
dockerfile:
description: "A path to the Dockerfile (if it's not in the repository's root directory)"
required: false
default: "./Dockerfile"
context:
description: "A path to the context in which the build will happen, see https://docs.docker.com/engine/reference/commandline/build/"
required: false
default: "."
repository:
description: "Docker repository to push an image to, defaults to ghcr.io"
required: true
default: "ghcr.io"
docker_io_token:
description: "Your docker.io token created via https://hub.docker.com/settings/security"
required: false
image_tag:
description: Image tag, e.g. latest. Will overwrite the tag latest tag on a push, and have no effect on a release.
required: false
runs:
using: "composite"
steps:
# https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry
- name: Log in to the Container registry
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
DOCKER_BUILDKIT: 1
DOCKER_IO_TOKEN: ${{ inputs.docker_io_token }}
IMAGE_TAG: ${{ inputs.image_tag }}
run: |
echo "Using $(docker -v)"
echo "::group::Logging into the GitHub Container registry (ghcr.io) ..."
echo "${GITHUB_TOKEN}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
echo "::endgroup::"
echo "::group::Set commit tag and image name"
if [ "${{ github.event_name }}" = "release" ]; then
export COMMIT_TAG=${GITHUB_REF:10}
export COMMIT_TAG=${COMMIT_TAG//v/}
else
if [ -z ${IMAGE_TAG} ]; then
export COMMIT_TAG=latest
else
export COMMIT_TAG=${IMAGE_TAG}
fi
fi
echo "Tagging with ${COMMIT_TAG}"
echo "::endgroup::"
export BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
export GITHUB_URL=https://github.com/${{ github.repository }}
echo "::group::Building the Docker image: ${{ inputs.repository }}/${{ inputs.image_name }}:${COMMIT_TAG} from ${{ inputs.dockerfile }} in ${{ inputs.context}} context ..."
# https://docs.docker.com/develop/develop-images/build_enhancements/
# https://docs.docker.com/engine/reference/commandline/build/#specifying-external-cache-sources
>&0 docker build \
--file ${{ inputs.dockerfile }} \
--progress tty \
--cache-from ${{ inputs.repository }}/${{ inputs.image_name }}:latest \
--build-arg BUILDKIT_INLINE_CACHE=1 \
\
--build-arg BUILD_DATE=${BUILD_DATE} \
--build-arg GITHUB_SHA=${GITHUB_SHA} \
\
--tag ${{ inputs.repository }}/${{ inputs.image_name }}:${COMMIT_TAG} \
--tag docker.io/${{ inputs.image_name }}:${COMMIT_TAG} \
\
--label org.label-schema.build-date=${BUILD_DATE} \
--label org.label-schema.vcs-url=${GITHUB_URL} \
--label org.label-schema.vcs-ref=${GITHUB_SHA} \
\
--label org.opencontainers.image.created=${BUILD_DATE} \
--label org.opencontainers.image.source=${GITHUB_URL} \
--label org.opencontainers.image.revision=${GITHUB_SHA} \
${{ inputs.context }}
echo "::endgroup::"
echo "::group::Inspecting the image ..."
docker images
echo "Labels:"
docker image inspect ${{ inputs.repository }}/${{ inputs.image_name }}:${COMMIT_TAG} | jq '.[].Config.Labels'
echo "Env variables:"
docker image inspect ${{ inputs.repository }}/${{ inputs.image_name }}:${COMMIT_TAG} | jq '.[].Config.Env'
echo "::endgroup::"
echo "::group::Pushing the Docker image to ${{ inputs.repository }} ..."
>&0 docker push ${{ inputs.repository }}/${{ inputs.image_name }}:${COMMIT_TAG} && echo "Pushed"
echo "::endgroup::"
if [ -z "${DOCKER_IO_TOKEN}" ]; then
echo "::warning::NOT pushing the Docker image to docker.io ... Provide 'docker_io_token' if needed."
else
echo "::group::Pushing the Docker image to docker.io ..."
echo "${DOCKER_IO_TOKEN}" | docker login docker.io -u "${{ github.actor }}" --password-stdin
>&0 docker push docker.io/${{ inputs.image_name }}:${COMMIT_TAG} && echo "Pushed"
echo "::endgroup::"
fi