Skip to content

Commit 50ba04b

Browse files
feat: Implement PR-specific Docker tagging
Modifies the Docker build workflow to use PR-specific tags when building with an upstream PR merged. - If `upstream_pr_number` is provided: - The Docker image will be tagged exclusively as `breedinginsight/brapi-java-server:pr-<PR_NUMBER>`. - Standard tagging (run number, develop, rc, latest) is skipped. - If no `upstream_pr_number` is provided (regular build): - The original tagging logic applies (run number + stream name). This is achieved by a new 'Determine Docker Tags' step that centralizes tag generation logic, and updating the build step to consume these tags.
1 parent 7498f44 commit 50ba04b

File tree

1 file changed

+99
-15
lines changed

1 file changed

+99
-15
lines changed

.github/workflows/docker-build.yml

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,77 @@ on:
88
- brapi-server-v2
99

1010
workflow_dispatch:
11+
inputs:
12+
upstream_repo:
13+
description: 'Optional: Override upstream repository (owner/repo). Defaults to the repository this was forked from if not set.'
14+
required: false
15+
type: string
16+
upstream_pr_number:
17+
description: 'Required if merging an upstream PR: PR number from the upstream repository to merge into develop for this build.'
18+
required: false # Still false overall, but logic will require it if this feature is used.
19+
type: string
1120

1221
jobs:
1322
docker:
1423
runs-on: ubuntu-latest
1524

1625
steps:
17-
- uses: actions/checkout@v2
26+
- name: Checkout develop branch
27+
uses: actions/checkout@v4
28+
with:
29+
ref: develop # Default to develop, will be overridden by push events to other branches
30+
# token: ${{ secrets.PAT_TOKEN }} # Uncomment if upstream repo is private and requires a PAT
1831

32+
- name: Conditionally Fetch and Merge Upstream PR
33+
# This step runs if a PR number is provided AND (an upstream_repo input is given OR a parent repo full_name is available)
34+
if: |
35+
github.event.inputs.upstream_pr_number && \
36+
(github.event.inputs.upstream_repo || github.event.repository.parent.full_name)
37+
run: |
38+
UPSTREAM_REPO_SPECIFIED="${{ github.event.inputs.upstream_repo }}"
39+
UPSTREAM_REPO_PARENT="${{ github.event.repository.parent.full_name }}"
40+
UPSTREAM_PR_NUMBER="${{ github.event.inputs.upstream_pr_number }}"
41+
42+
if [[ -z "$UPSTREAM_PR_NUMBER" ]]; then
43+
echo "No upstream PR number provided. Skipping PR merge."
44+
exit 0
45+
fi
46+
47+
TARGET_UPSTREAM_REPO=""
48+
if [[ -n "$UPSTREAM_REPO_SPECIFIED" ]]; then
49+
TARGET_UPSTREAM_REPO="$UPSTREAM_REPO_SPECIFIED"
50+
echo "Using specified upstream repository: $TARGET_UPSTREAM_REPO"
51+
elif [[ -n "$UPSTREAM_REPO_PARENT" ]]; then
52+
TARGET_UPSTREAM_REPO="$UPSTREAM_REPO_PARENT"
53+
echo "Using parent repository as upstream: $TARGET_UPSTREAM_REPO"
54+
else
55+
echo "Error: Upstream PR number '$UPSTREAM_PR_NUMBER' was provided, but no upstream_repo was specified and parent repository could not be determined."
56+
exit 1
57+
fi
58+
59+
echo "Upstream PR to merge: $UPSTREAM_PR_NUMBER from $TARGET_UPSTREAM_REPO"
60+
UPSTREAM_REPO_URL="https://github.com/$TARGET_UPSTREAM_REPO.git"
61+
62+
# Fetch the PR from the upstream repository
63+
git fetch $UPSTREAM_REPO_URL +refs/pull/$UPSTREAM_PR_NUMBER/head:upstream_pr_branch
64+
65+
echo "Fetched PR branch 'upstream_pr_branch'. Merging into current HEAD (develop)..."
66+
# Merge the fetched PR branch. If conflicts occur, the script will exit with an error.
67+
git config user.name "GitHub Actions"
68+
git config user.email "actions@github.com"
69+
git merge upstream_pr_branch --no-ff -m "Merge upstream PR #$UPSTREAM_PR_NUMBER from $TARGET_UPSTREAM_REPO for testing"
70+
71+
echo "Merge complete."
72+
shell: bash
73+
1974
- name: Extract branch name
2075
shell: bash
2176
run: echo ::set-output name=branch::$(echo ${GITHUB_REF#refs/heads/})
2277
id: extract_branch
2378

24-
- run: git pull origin ${{steps.extract_branch.outputs.branch}}
79+
# This pull is no longer needed as checkout handles fetching the correct ref,
80+
# and the PR merge step brings in specific upstream changes.
81+
# - run: git pull origin ${{steps.extract_branch.outputs.branch}}
2582

2683
- name: Set up JDK 21
2784
uses: actions/setup-java@v4
@@ -45,20 +102,47 @@ jobs:
45102
with:
46103
username: ${{ secrets.DOCKERHUB_USERNAME }}
47104
password: ${{ secrets.DOCKERHUB_PASSWORD }}
48-
- name: Set tag
49-
id: vars
50-
run: echo ::set-output name=imageName::$(echo breedinginsight/brapi-java-server:${{ github.run_number }})
105+
106+
- name: Determine Docker Tags
107+
id: docker_tags
108+
shell: bash
109+
run: |
110+
PR_NUMBER="${{ github.event.inputs.upstream_pr_number }}"
111+
IMAGE_BASE_NAME="breedinginsight/brapi-java-server"
112+
TAGS=""
113+
114+
if [[ -n "$PR_NUMBER" ]]; then
115+
echo "This is a PR build. Setting PR-specific tag."
116+
PR_TAG="$IMAGE_BASE_NAME:pr-$PR_NUMBER"
117+
TAGS="$PR_TAG"
118+
echo "::set-output name=is_pr_build::true"
119+
else
120+
echo "This is a regular build. Setting standard tags."
121+
RUN_NUMBER_TAG="$IMAGE_BASE_NAME:${{ github.run_number }}"
122+
TAGS="$RUN_NUMBER_TAG"
123+
124+
BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}"
125+
STREAM_NAME=""
126+
if [[ "$BRANCH_NAME" == "develop" ]]; then
127+
STREAM_NAME="$IMAGE_BASE_NAME:develop"
128+
elif [[ "$BRANCH_NAME" == "brapi-server-v2" ]]; then # Assuming brapi-server-v2 is 'latest'
129+
STREAM_NAME="$IMAGE_BASE_NAME:latest"
130+
elif [[ "${{ github.ref }}" == refs/heads/release/* ]]; then
131+
STREAM_NAME="$IMAGE_BASE_NAME:rc"
132+
fi
133+
134+
if [[ -n "$STREAM_NAME" ]]; then
135+
TAGS="$TAGS,$STREAM_NAME" # Comma-separated for docker/build-push-action or loop for docker buildx build
136+
fi
137+
echo "::set-output name=is_pr_build::false"
138+
fi
139+
echo "Final tags: $TAGS"
140+
echo "::set-output name=tags::$TAGS"
51141
52-
- name: Tag develop
53-
if: steps.extract_branch.outputs.branch == 'develop'
54-
run: echo "streamName=breedinginsight/brapi-java-server:develop" >> $GITHUB_ENV
55-
- name: Tag release candidate
56-
if: contains(github.ref, '/release/')
57-
run: echo "streamName=breedinginsight/brapi-java-server:rc" >> $GITHUB_ENV
58-
- name: Tag latest
59-
if: steps.extract_branch.outputs.branch == 'brapi-server-v2'
60-
run: echo "streamName=breedinginsight/brapi-java-server:latest" >> $GITHUB_ENV
142+
# The original 'Set tag' and 'Tag develop/rc/latest' steps are now incorporated into 'Determine Docker Tags'
143+
# and are conditioned by is_pr_build logic within that step.
61144

62145
- name: Build Docker and push image
63146
run: |
64-
docker buildx build . --file Dockerfile --tag ${{steps.vars.outputs.imageName}} --tag ${{env.streamName}} --push --platform=linux/arm64,linux/amd64
147+
FORMATTED_TAGS=$(echo "${{ steps.docker_tags.outputs.tags }}" | sed 's/,/ --tag /g')
148+
docker buildx build . --file Dockerfile --tag $FORMATTED_TAGS --push --platform=linux/arm64,linux/amd64

0 commit comments

Comments
 (0)