-
Notifications
You must be signed in to change notification settings - Fork 0
92 lines (84 loc) · 3.53 KB
/
docker-image.yml
File metadata and controls
92 lines (84 loc) · 3.53 KB
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
name: Docker Image CI
on:
push:
branches:
- 'develop'
- 'main'
- 'master'
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches:
- 'develop'
- 'main'
- 'master'
workflow_dispatch:
inputs:
push_image:
description: 'Push the built image to DockerHub'
required: false
default: false
type: boolean
custom_tag:
description: "Optional extra tag (e.g. 'test-xyz'). Branch name is always tagged."
required: false
type: string
jobs:
build:
runs-on: ubuntu-latest
# Skip draft PRs
if: github.event.pull_request.draft == false || github.event_name == 'push'
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Check if Dockerfile exists
id: check_dockerfile
run: |
if [ -f "src/sn-auth/Dockerfile" ]; then
echo "dockerfile_exists=true" >> $GITHUB_OUTPUT
else
echo "dockerfile_exists=false" >> $GITHUB_OUTPUT
echo "⚠️ No Dockerfile found, skipping Docker build"
fi
- name: Compute version tag (commit date + short SHA)
if: steps.check_dockerfile.outputs.dockerfile_exists == 'true'
id: version
run: |
SHORT_SHA=$(git rev-parse --short=8 "$GITHUB_SHA")
COMMIT_DATE=$(git show -s --format=%cd --date=format:%Y%m%d "$GITHUB_SHA")
VERSION_TAG="${COMMIT_DATE}-${SHORT_SHA}"
echo "version_tag=${VERSION_TAG}" >> $GITHUB_OUTPUT
echo "Computed version tag: ${VERSION_TAG}"
- name: Set up Docker metadata
if: steps.check_dockerfile.outputs.dockerfile_exists == 'true'
id: meta
uses: docker/metadata-action@v5
with:
images: sensenetcsp/sn-auth
tags: |
# Clean branch name (e.g., feature-conditional-recaptcha)
type=ref,event=branch
# Immutable version tag: <commit-date>-<short-sha>, e.g. 20260522-a3777f7a
type=raw,value=${{ steps.version.outputs.version_tag }}
# 'latest' tag only when pushing to main or master (regardless of default branch)
type=raw,value=latest,enable=${{ github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') }}
# 'preview' tag only when pushing to develop
type=raw,value=preview,enable=${{ github.event_name == 'push' && github.ref == 'refs/heads/develop' }}
# PR number for pull requests
type=ref,event=pr
# Optional custom tag from manual workflow_dispatch
type=raw,value=${{ inputs.custom_tag }},enable=${{ github.event_name == 'workflow_dispatch' && inputs.custom_tag != '' }}
- name: Login to DockerHub
if: steps.check_dockerfile.outputs.dockerfile_exists == 'true' && (github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.push_image))
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
if: steps.check_dockerfile.outputs.dockerfile_exists == 'true'
uses: docker/build-push-action@v6
with:
context: .
file: ./src/sn-auth/Dockerfile
push: ${{ github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.push_image) }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}