-
Notifications
You must be signed in to change notification settings - Fork 57
94 lines (84 loc) · 3.04 KB
/
docker-dockerhub.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
# Reusable workflow for building and pushing a Docker Image to DockerHub.
#
# Auth username is taken from the repository secret, "DOCKERHUB_USERNAME"
# Publish username is taken from the repository secret, "DOCKERHUB_SCOPE"
# Password is taken from the repository secret, "DOCKERHUB_TOKEN"
#
# NOTE: DockerHub currently does not provide PATs for organizations.
# Users belonging to the org must use their own, which is why we need a
# username to publish through: DOCKERHUB_USERNAME, and a username to publish
# under: DOCKERHUB_SCOPE.
# You may use the same username for both.
name: Build and Push Docker Image (DockerHub)
on:
workflow_call:
inputs:
image-name:
required: true
type: string
version:
required: true
type: string
default: "latest"
dockerfile:
required: true
type: string
readme-file:
required: true
type: string
secrets:
# Username used for DockerHub authorization
DOCKERHUB_USERNAME:
required: true
# Username under which the image will be published
DOCKERHUB_SCOPE:
required: true
# Personal Access Token created by the user `DOCKERHUB_USERNAME`
# Must have read, write and delete permissions
DOCKERHUB_TOKEN:
required: true
env:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
dockerhub-scope: ${{ secrets.DOCKERHUB_SCOPE }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.registry }}
username: ${{ env.username }}
password: ${{ env.password }}
- name: Check if version exists
id: check_version
run: |
if docker manifest inspect ${{ env.registry }}/${{ env.dockerhub-scope }}/${{ inputs.image-name }}:${{ inputs.version }} > /dev/null 2>&1; then
echo "Version ${{ inputs.version }} already exists."
echo "exists=true" >> $GITHUB_ENV
else
echo "Version ${{ inputs.version }} does not exist."
echo "exists=false" >> $GITHUB_ENV
fi
- name: Build and push Docker image
if: env.exists == 'false'
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/${{ inputs.dockerfile }}
push: true
tags: ${{ env.registry }}/${{ env.dockerhub-scope }}/${{ inputs.image-name }}:${{ inputs.version }}, ${{ env.registry }}/${{ env.dockerhub-scope }}/${{ inputs.image-name }}:latest
- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ env.username }}
password: ${{ env.password }}
repository: ${{ env.dockerhub-scope }}/${{ inputs.image-name }}
readme-filepath: ${{ inputs.readme-file }}