forked from plantbreeding/brapi-Java-ProdServer
-
Notifications
You must be signed in to change notification settings - Fork 0
148 lines (125 loc) · 6 KB
/
docker-build.yml
File metadata and controls
148 lines (125 loc) · 6 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
name: Docker Build
on:
push:
branches:
- develop
- release/**
- brapi-server-v2
workflow_dispatch:
inputs:
upstream_repo:
description: 'Optional: Override upstream repository (owner/repo). Defaults to the repository this was forked from if not set.'
required: false
type: string
upstream_pr_number:
description: 'Required if merging an upstream PR: PR number from the upstream repository to merge into develop for this build.'
required: false # Still false overall, but logic will require it if this feature is used.
type: string
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout develop branch
uses: actions/checkout@v4
with:
ref: develop # Default to develop, will be overridden by push events to other branches
# token: ${{ secrets.PAT_TOKEN }} # Uncomment if upstream repo is private and requires a PAT
- name: Conditionally Fetch and Merge Upstream PR
# This step runs if a PR number is provided AND (an upstream_repo input is given OR a parent repo full_name is available)
if: |
github.event.inputs.upstream_pr_number &&
(github.event.inputs.upstream_repo || github.event.repository.parent.full_name)
run: |
UPSTREAM_REPO_SPECIFIED="${{ github.event.inputs.upstream_repo }}"
UPSTREAM_REPO_PARENT="${{ github.event.repository.parent.full_name }}"
UPSTREAM_PR_NUMBER="${{ github.event.inputs.upstream_pr_number }}"
if [[ -z "$UPSTREAM_PR_NUMBER" ]]; then
echo "No upstream PR number provided. Skipping PR merge."
exit 0
fi
TARGET_UPSTREAM_REPO=""
if [[ -n "$UPSTREAM_REPO_SPECIFIED" ]]; then
TARGET_UPSTREAM_REPO="$UPSTREAM_REPO_SPECIFIED"
echo "Using specified upstream repository: $TARGET_UPSTREAM_REPO"
elif [[ -n "$UPSTREAM_REPO_PARENT" ]]; then
TARGET_UPSTREAM_REPO="$UPSTREAM_REPO_PARENT"
echo "Using parent repository as upstream: $TARGET_UPSTREAM_REPO"
else
echo "Error: Upstream PR number '$UPSTREAM_PR_NUMBER' was provided, but no upstream_repo was specified and parent repository could not be determined."
exit 1
fi
echo "Upstream PR to merge: $UPSTREAM_PR_NUMBER from $TARGET_UPSTREAM_REPO"
UPSTREAM_REPO_URL="https://github.com/$TARGET_UPSTREAM_REPO.git"
# Fetch the PR from the upstream repository
git fetch $UPSTREAM_REPO_URL +refs/pull/$UPSTREAM_PR_NUMBER/head:upstream_pr_branch
echo "Fetched PR branch 'upstream_pr_branch'. Merging into current HEAD (develop)..."
# Merge the fetched PR branch. If conflicts occur, the script will exit with an error.
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git merge upstream_pr_branch --no-ff -m "Merge upstream PR #$UPSTREAM_PR_NUMBER from $TARGET_UPSTREAM_REPO for testing"
echo "Merge complete."
shell: bash
- name: Extract branch name
shell: bash
run: echo ::set-output name=branch::$(echo ${GITHUB_REF#refs/heads/})
id: extract_branch
# This pull is no longer needed as checkout handles fetching the correct ref,
# and the PR merge step brings in specific upstream changes.
# - run: git pull origin ${{steps.extract_branch.outputs.branch}}
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'
cache: maven
- name: Build with Maven
run: mvn clean install
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: 'arm64,arm,amd64,amd'
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Determine Docker Tags
id: docker_tags
shell: bash
run: |
PR_NUMBER="${{ github.event.inputs.upstream_pr_number }}"
IMAGE_BASE_NAME="breedinginsight/brapi-java-server"
TAGS=""
if [[ -n "$PR_NUMBER" ]]; then
echo "This is a PR build. Setting PR-specific tag."
PR_TAG="$IMAGE_BASE_NAME:pr-$PR_NUMBER"
TAGS="$PR_TAG"
echo "::set-output name=is_pr_build::true"
else
echo "This is a regular build. Setting standard tags."
RUN_NUMBER_TAG="$IMAGE_BASE_NAME:${{ github.run_number }}"
TAGS="$RUN_NUMBER_TAG"
BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}"
STREAM_NAME=""
if [[ "$BRANCH_NAME" == "develop" ]]; then
STREAM_NAME="$IMAGE_BASE_NAME:develop"
elif [[ "$BRANCH_NAME" == "brapi-server-v2" ]]; then # Assuming brapi-server-v2 is 'latest'
STREAM_NAME="$IMAGE_BASE_NAME:latest"
elif [[ "${{ github.ref }}" == refs/heads/release/* ]]; then
STREAM_NAME="$IMAGE_BASE_NAME:rc"
fi
if [[ -n "$STREAM_NAME" ]]; then
TAGS="$TAGS,$STREAM_NAME" # Comma-separated for docker/build-push-action or loop for docker buildx build
fi
echo "::set-output name=is_pr_build::false"
fi
echo "Final tags: $TAGS"
echo "::set-output name=tags::$TAGS"
# The original 'Set tag' and 'Tag develop/rc/latest' steps are now incorporated into 'Determine Docker Tags'
# and are conditioned by is_pr_build logic within that step.
- name: Build Docker and push image
run: |
FORMATTED_TAGS=$(echo "${{ steps.docker_tags.outputs.tags }}" | sed 's/,/ --tag /g')
docker buildx build . --file Dockerfile --tag $FORMATTED_TAGS --push --platform=linux/arm64,linux/amd64