-
Notifications
You must be signed in to change notification settings - Fork 37
105 lines (92 loc) · 4.33 KB
/
reusable-docker.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
name: Reusable workflow for docker images
on:
workflow_call:
inputs:
docker_org:
required: true
type: string
docker_image:
required: true
type: string
docker_file:
required: true
type: string
secrets:
dockerhub_username:
required: true
dockerhub_token:
required: true
env:
VERSION: 1.1.0-next
jobs:
build:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Create docker tags
id: get_tags
run: |
echo "version_tag=${{ inputs.docker_org }}/${{ inputs.docker_image }}:${{ env.VERSION }}" >> $GITHUB_OUTPUT
- name: Build the Docker image
run: |
docker build -t ${{ steps.get_tags.outputs.version_tag }} -f ${{ inputs.docker_file }} .
publish:
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'release'
steps:
- name: Checkout Repository
uses: actions/checkout@v4
# Check if we have a valid *-next version for merges on main
- name: Check version (only for push events)
if: github.event_name == 'push'
id: version_check
run: |
if [[ $VERSION == *"-next" ]]; then
echo "is_next_version=true" >> $GITHUB_OUTPUT
else
echo "is_next_version=false" >> $GITHUB_OUTPUT
fi
# Create tags according to the state to be published:
# - next version on main merge: create `*-next` and `*-next.<git_sha>` tags
# - release version: create version, `*-next` and `latest` tags
- name: Create docker tags
id: get_tags
if: github.event_name == 'push' && steps.version_check.outputs.is_next_version == 'true' || github.event_name == 'release'
run: |
if [[ $GITHUB_EVENT_NAME == 'push' ]]; then
echo "sha_tag=${{ inputs.docker_org }}/${{ inputs.docker_image }}:${{ env.VERSION }}.$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_OUTPUT
echo "version_tag=${{ inputs.docker_org }}/${{ inputs.docker_image }}:${{ env.VERSION }}" >> $GITHUB_OUTPUT
else
echo "version_tag=${{ inputs.docker_org }}/${{ inputs.docker_image }}:${{ env.VERSION }}" >> $GITHUB_OUTPUT
echo "next_tag=${{ inputs.docker_org }}/${{ inputs.docker_image }}:${{ env.VERSION }}-next" >> $GITHUB_OUTPUT
echo "latest_tag=${{ inputs.docker_org }}/${{ inputs.docker_image }}:latest" >> $GITHUB_OUTPUT
fi
# Only build when we will publish, so either a main merge with next-version or a release
- name: Build Docker image
if: github.event_name == 'push' && steps.version_check.outputs.is_next_version == 'true' || github.event_name == 'release'
run: docker build -t ${{ steps.get_tags.outputs.version_tag }} -f ${{ inputs.docker_file }} .
# Only log in to dockerhub when we will publish, so either a main merge with next-version or a release
- name: Login to DockerHub
if: github.event_name == 'push' && steps.version_check.outputs.is_next_version == 'true' || github.event_name == 'release'
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
with:
username: ${{ secrets.dockerhub_username }}
password: ${{ secrets.dockerhub_token }}
# Push for main merges with next version
- name: Push Docker tags (for push events with next version)
if: github.event_name == 'push' && steps.version_check.outputs.is_next_version == 'true'
run: |
docker push ${{ steps.get_tags.outputs.version_tag }}
docker tag ${{ steps.get_tags.outputs.version_tag }} ${{ steps.get_tags.outputs.sha_tag }}
docker push ${{ steps.get_tags.outputs.sha_tag }}
# Push for releases
- name: Push Docker tags (for release events)
if: github.event_name == 'release'
run: |
docker push ${{ steps.get_tags.outputs.version_tag }}
docker tag ${{ steps.get_tags.outputs.version_tag }} ${{ steps.get_tags.outputs.latest_tag }}
docker push ${{ steps.get_tags.outputs.latest_tag }}
docker tag ${{ steps.get_tags.outputs.version_tag }} ${{ steps.get_tags.outputs.next_tag }}
docker push ${{ steps.get_tags.outputs.next_tag }}