1+ name : Reusable Docker build and push workflow
2+
3+ on :
4+ workflow_call :
5+ inputs :
6+ registry :
7+ required : true
8+ type : string
9+ username :
10+ required : true
11+ type : string
12+ password_secret :
13+ required : true
14+ type : string
15+ app_name :
16+ required : true
17+ type : string
18+ dockerfile :
19+ required : true
20+ type : string
21+ push :
22+ required : true
23+ type : boolean
24+ secrets :
25+ DOCKER_PASSWORD :
26+ required : true
27+
28+ jobs :
29+ docker-build :
30+ runs-on : ubuntu-latest
31+ steps :
32+
33+ - name : Checkout
34+ uses : actions/checkout@v4
35+
36+ - name : Docker Login
37+ if : ${{ inputs.push }}
38+ uses : docker/login-action@v3
39+ with :
40+ registry : ${{ inputs.registry }}
41+ username : ${{ inputs.username }}
42+ password : ${{ secrets[inputs.password_secret] }}
43+
44+ - name : Set up Docker Buildx
45+ uses : docker/setup-buildx-action@v3
46+
47+ - name : Get current date
48+ id : date
49+ run : echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
50+
51+ - name : Determine Tag Name Based on Branch
52+ id : determine_tag
53+ run : |
54+ if [[ "${{ github.base_ref }}" == "main" ]]; then
55+ echo "tagname=latest" >> $GITHUB_OUTPUT
56+ elif [[ "${{ github.base_ref }}" == "dev" ]]; then
57+ echo "tagname=dev" >> $GITHUB_OUTPUT
58+ elif [[ "${{ github.base_ref }}" == "demo" ]]; then
59+ echo "tagname=demo" >> $GITHUB_OUTPUT
60+ elif [[ "${{ github.base_ref }}" == "hotfix" ]]; then
61+ echo "tagname=hotfix" >> $GITHUB_OUTPUT
62+ elif [[ "${{ github.base_ref }}" == "dependabotchanges" ]]; then
63+ echo "tagname=dependabotchanges" >> $GITHUB_OUTPUT
64+ else
65+ echo "tagname=default" >> $GITHUB_OUTPUT
66+ fi
67+
68+
69+ - name : Build Docker Image and optionally push
70+ uses : docker/build-push-action@v6
71+ with :
72+ context : .
73+ file : ${{ inputs.dockerfile }}
74+ push : ${{ inputs.push }}
75+ cache-from : type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }}
76+ tags : |
77+ ${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }}
78+ ${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }}_${{ steps.date.outputs.date }}_${{ github.run_number }}
0 commit comments