|
6 | 6 | - develop |
7 | 7 | - release/** |
8 | 8 | - brapi-server-v2 |
9 | | - |
| 9 | + |
10 | 10 | 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 | + default: 'plantbreeding/brapi-Java-ProdServer' |
| 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. ex: 11' |
| 18 | + required: false # Still false overall, but logic will require it if this feature is used. |
| 19 | + type: string |
11 | 20 |
|
12 | 21 | jobs: |
13 | 22 | docker: |
14 | 23 | runs-on: ubuntu-latest |
15 | 24 |
|
16 | 25 | steps: |
17 | | - - uses: actions/checkout@v2 |
18 | | - |
| 26 | + - name: Checkout branch |
| 27 | + uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + fetch-depth: 0 # Fetch all history to ensure common ancestor is found |
| 30 | + |
| 31 | + - name: Conditionally Fetch and Merge Upstream PR |
| 32 | + # This step runs if a PR number is provided AND (an upstream_repo input is given OR a parent repo full_name is available) |
| 33 | + if: | |
| 34 | + github.event.inputs.upstream_pr_number && |
| 35 | + github.event.inputs.upstream_repo |
| 36 | + run: | |
| 37 | + UPSTREAM_REPO_SPECIFIED="${{ github.event.inputs.upstream_repo }}" |
| 38 | + UPSTREAM_PR_NUMBER="${{ github.event.inputs.upstream_pr_number }}" |
| 39 | +
|
| 40 | + if [[ -z "$UPSTREAM_PR_NUMBER" ]]; then |
| 41 | + echo "No upstream PR number provided. Skipping PR merge." |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +
|
| 45 | + TARGET_UPSTREAM_REPO="" |
| 46 | + if [[ -n "$UPSTREAM_REPO_SPECIFIED" ]]; then |
| 47 | + TARGET_UPSTREAM_REPO="$UPSTREAM_REPO_SPECIFIED" |
| 48 | + echo "Using specified upstream repository: $TARGET_UPSTREAM_REPO" |
| 49 | + else |
| 50 | + echo "Error: Upstream PR number '$UPSTREAM_PR_NUMBER' was provided, but no upstream_repo was specified and parent repository could not be determined." |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +
|
| 54 | + echo "Upstream PR to merge: $UPSTREAM_PR_NUMBER from $TARGET_UPSTREAM_REPO" |
| 55 | + UPSTREAM_REPO_URL="https://github.com/$TARGET_UPSTREAM_REPO.git" |
| 56 | +
|
| 57 | + # Fetch the PR from the upstream repository |
| 58 | + git fetch $UPSTREAM_REPO_URL +refs/pull/$UPSTREAM_PR_NUMBER/head:upstream_pr_branch |
| 59 | +
|
| 60 | + echo "Fetched PR branch 'upstream_pr_branch'. Merging into current HEAD (develop)..." |
| 61 | + # Merge the fetched PR branch. If conflicts occur, the script will exit with an error. |
| 62 | + git config user.name "GitHub Actions" |
| 63 | + git config user.email "actions@github.com" |
| 64 | + git merge upstream_pr_branch --no-ff -m "Merge upstream PR #$UPSTREAM_PR_NUMBER from $TARGET_UPSTREAM_REPO for testing" |
| 65 | +
|
| 66 | + echo "Merge complete." |
| 67 | + shell: bash |
| 68 | + |
19 | 69 | - name: Extract branch name |
20 | 70 | shell: bash |
21 | | - run: echo ::set-output name=branch::$(echo ${GITHUB_REF#refs/heads/}) |
| 71 | + run: echo "branch=$(echo ${GITHUB_REF#refs/heads/})" >> "$GITHUB_OUTPUT" |
22 | 72 | id: extract_branch |
23 | | - |
24 | | - - run: git pull origin ${{steps.extract_branch.outputs.branch}} |
25 | | - |
| 73 | + |
| 74 | + # This pull is no longer needed as checkout handles fetching the correct ref, |
| 75 | + # and the PR merge step brings in specific upstream changes. |
| 76 | + # - run: git pull origin ${{steps.extract_branch.outputs.branch}} |
| 77 | + |
26 | 78 | - name: Set up JDK 21 |
27 | 79 | uses: actions/setup-java@v4 |
28 | 80 | with: |
29 | 81 | distribution: temurin |
30 | 82 | java-version: '21' |
31 | 83 | cache: maven |
32 | | - |
| 84 | + |
33 | 85 | - name: Build with Maven |
34 | 86 | run: mvn clean install |
35 | 87 |
|
36 | 88 | - name: Set up Docker Buildx |
37 | | - uses: docker/setup-buildx-action@v2 |
| 89 | + uses: docker/setup-buildx-action@v3 |
38 | 90 | - name: Set up QEMU |
39 | | - uses: docker/setup-qemu-action@v2 |
| 91 | + uses: docker/setup-qemu-action@v3 |
40 | 92 | with: |
41 | | - platforms: 'arm64,arm,amd64,amd' |
42 | | - |
| 93 | + platforms: 'arm64,arm,amd64' |
| 94 | + |
43 | 95 | - name: Login to Docker Hub |
44 | | - uses: docker/login-action@v1 |
| 96 | + uses: docker/login-action@v3 |
45 | 97 | with: |
46 | 98 | username: ${{ secrets.DOCKERHUB_USERNAME }} |
47 | 99 | 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 }}) |
51 | | - |
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 |
| 100 | + |
| 101 | + - name: Determine Docker Tags |
| 102 | + id: docker_tags |
| 103 | + shell: bash |
| 104 | + run: | |
| 105 | + PR_NUMBER="${{ github.event.inputs.upstream_pr_number }}" |
| 106 | + IMAGE_BASE_NAME="breedinginsight/brapi-java-server" |
| 107 | + TAGS="" |
| 108 | + IS_PR_BUILD="false" |
| 109 | +
|
| 110 | + if [[ -n "$PR_NUMBER" ]]; then |
| 111 | + echo "This is a PR build. Setting PR-specific tag." |
| 112 | + PR_TAG="$IMAGE_BASE_NAME:pr-$PR_NUMBER" |
| 113 | + TAGS="$PR_TAG" |
| 114 | + IS_PR_BUILD="true" |
| 115 | + else |
| 116 | + echo "This is a regular build. Setting standard tags." |
| 117 | + RUN_NUMBER_TAG="$IMAGE_BASE_NAME:${{ github.run_number }}" |
| 118 | + TAGS="$RUN_NUMBER_TAG" |
| 119 | +
|
| 120 | + BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}" |
| 121 | + STREAM_NAME="" |
| 122 | + if [[ "$BRANCH_NAME" == "develop" ]]; then |
| 123 | + STREAM_NAME="$IMAGE_BASE_NAME:develop" |
| 124 | + elif [[ "$BRANCH_NAME" == "brapi-server-v2" ]]; then # Assuming brapi-server-v2 is 'latest' |
| 125 | + STREAM_NAME="$IMAGE_BASE_NAME:latest" |
| 126 | + elif [[ "${{ github.ref }}" == refs/heads/release/* ]]; then |
| 127 | + STREAM_NAME="$IMAGE_BASE_NAME:rc" |
| 128 | + fi |
| 129 | +
|
| 130 | + if [[ -n "$STREAM_NAME" ]]; then |
| 131 | + TAGS="$TAGS,$STREAM_NAME" # Comma-separated for docker/build-push-action |
| 132 | + fi |
| 133 | + fi |
| 134 | + echo "Final tags: $TAGS" |
| 135 | + echo "tags=$TAGS" >> "$GITHUB_OUTPUT" |
| 136 | + echo "is_pr_build=$IS_PR_BUILD" >> "$GITHUB_OUTPUT" |
| 137 | +
|
| 138 | + # The original 'Set tag' and 'Tag develop/rc/latest' steps are now incorporated into 'Determine Docker Tags' |
| 139 | + # and are conditioned by is_pr_build logic within that step. |
61 | 140 |
|
62 | 141 | - name: Build Docker and push image |
63 | 142 | run: | |
64 | | - docker buildx build . --file Dockerfile --tag ${{steps.vars.outputs.imageName}} --tag ${{env.streamName}} --push --platform=linux/arm64,linux/amd64 |
| 143 | + # Note: The `docker/build-push-action` is generally recommended over manual scripting |
| 144 | + # as it handles tags more robustly. However, to keep the change minimal, |
| 145 | + # this script adapts the original logic. |
| 146 | + FORMATTED_TAGS="" |
| 147 | + IFS=',' read -ra TAG_ARRAY <<< "${{ steps.docker_tags.outputs.tags }}" |
| 148 | + for tag in "${TAG_ARRAY[@]}"; do |
| 149 | + FORMATTED_TAGS="$FORMATTED_TAGS --tag $tag" |
| 150 | + done |
| 151 | + docker buildx build . --file Dockerfile $FORMATTED_TAGS --push --platform=linux/arm64,linux/amd64 |
0 commit comments