11name : Publish Docker Image to GHCR
22
33on :
4- workflow_dispatch :
54 push :
65 branches :
76 - main
8- - release
9- - dev
107 tags :
118 - ' v*.*.*' # Semantic versioning pattern
9+ pull_request :
10+ # This triggers on any PR, but we filter by source branch in the job
11+ types : [opened, synchronize, reopened]
12+ workflow_dispatch :
1213
1314permissions :
1415 contents : read
2122 steps :
2223 - uses : actions/checkout@v4
2324
25+ # Only continue for PRs if the source branch is dev or release
26+ - name : Check PR source branch
27+ if : github.event_name == 'pull_request'
28+ run : |
29+ echo "PR source branch: ${{ github.head_ref }}"
30+ if [[ "${{ github.head_ref }}" != "dev" && "${{ github.head_ref }}" != "release" ]]; then
31+ echo "Not a PR from dev or release branch. Skipping workflow."
32+ exit 1
33+ fi
34+
2435 - name : Log in to GHCR
2536 uses : docker/login-action@v3
2637 with :
3445 TAG=latest
3546 SHOULD_BUILD_PUSH=false
3647
48+ echo "GITHUB_EVENT_NAME: $GITHUB_EVENT_NAME"
49+ echo "GITHUB_REF: $GITHUB_REF"
50+ echo "GITHUB_HEAD_REF: $GITHUB_HEAD_REF"
51+
52+ # Determine context: PR or push/tag
53+ if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
54+ BRANCH_NAME="$GITHUB_HEAD_REF"
55+ REF_TYPE="pr"
56+ elif [[ "$GITHUB_REF" == refs/heads/* ]]; then
57+ BRANCH_NAME="${GITHUB_REF#refs/heads/}"
58+ REF_TYPE="branch"
59+ elif [[ "$GITHUB_REF" == refs/tags/* ]]; then
60+ BRANCH_NAME="${GITHUB_REF#refs/tags/}"
61+ REF_TYPE="tag"
62+ else
63+ BRANCH_NAME=""
64+ REF_TYPE=""
65+ fi
66+ echo "REF_TYPE: $REF_TYPE"
67+ echo "BRANCH_NAME: $BRANCH_NAME"
68+
3769 # Determine tag and build indicator based on context
38- if [[ "${{ github.ref_type }}" == "branch " ]]; then
39- case "${{ github.ref_name }} " in
70+ if [[ "$REF_TYPE" == "branch" || "$REF_TYPE" == "pr " ]]; then
71+ case "$BRANCH_NAME " in
4072 "main")
4173 TAG=main
4274 SHOULD_BUILD_PUSH=true
@@ -50,16 +82,19 @@ jobs:
5082 SHOULD_BUILD_PUSH=true
5183 ;;
5284 *)
53- TAG=${{ github.ref_name }}
85+ TAG="$BRANCH_NAME"
5486 ;;
5587 esac
56- elif [[ "${{ github.ref_type }} " == "tag" ]]; then
57- TAG=${{ github.ref_name }}
58- if [[ "${{ github.ref_name }} " =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
88+ elif [[ "$REF_TYPE " == "tag" ]]; then
89+ TAG="$BRANCH_NAME"
90+ if [[ "$BRANCH_NAME " =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
5991 SHOULD_BUILD_PUSH=true
6092 fi
6193 fi
6294
95+ echo "Determined TAG: $TAG"
96+ echo "SHOULD_BUILD_PUSH: $SHOULD_BUILD_PUSH"
97+
6398 # Build and push the image if indicated
6499 if [[ "$SHOULD_BUILD_PUSH" == "true" ]]; then
65100 docker build -t $IMAGE_NAME:$TAG -f .devcontainer/Dockerfile .
0 commit comments