-
Notifications
You must be signed in to change notification settings - Fork 0
261 lines (225 loc) · 9.27 KB
/
deploy.yml
File metadata and controls
261 lines (225 loc) · 9.27 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
name: Spring Cloud Deploy (Reusable)
on:
workflow_call:
inputs:
branch:
description: 'Which branch should be built (for workflow_dispatch)'
required: false
type: string
custom_build_command:
description: 'Custom run command for the Build and Deploy step (overrides default Maven command)'
required: false
type: string
runs_on:
description: 'Runner to use for the build job (defaults to ubuntu-latest)'
required: false
type: string
secrets:
ARTIFACTORY_USERNAME:
required: true
ARTIFACTORY_PASSWORD:
required: true
COMMERCIAL_ARTIFACTORY_USERNAME:
required: false
COMMERCIAL_ARTIFACTORY_PASSWORD:
required: false
DOCKERHUB_USERNAME:
required: true
DOCKERHUB_TOKEN:
required: true
jobs:
setup:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout spring-cloud-github-actions for config
uses: actions/checkout@v4
with:
repository: spring-cloud/spring-cloud-github-actions
ref: main
path: github-actions-config
- name: Determine branches and JDK versions
id: set-matrix
run: |
echo "=== Workflow Debug Information ==="
echo "Event name: ${{ github.event_name }}"
echo "Ref name: ${{ github.ref_name }}"
echo "Repository: ${{ github.repository }}"
echo "Branch input: ${{ inputs.branch }}"
echo ""
# Extract repository name (without org)
FULL_REPO="${{ github.repository }}"
REPO_NAME="${FULL_REPO#*/}"
echo "Repository name: $REPO_NAME"
# Check if this is a commercial repository
IS_COMMERCIAL=false
BASE_REPO_NAME="$REPO_NAME"
if [[ "$REPO_NAME" == *"-commercial" ]]; then
IS_COMMERCIAL=true
BASE_REPO_NAME="${REPO_NAME%-commercial}"
echo "Commercial repository detected, base name: $BASE_REPO_NAME"
fi
# Read the configuration file
CONFIG_FILE="github-actions-config/config/projects.json"
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "ERROR: Configuration file not found: $CONFIG_FILE"
exit 1
fi
# Determine which config section to use (oss or commercial)
if [[ "$IS_COMMERCIAL" == "true" ]]; then
CONFIG_SECTION="commercial"
else
CONFIG_SECTION="oss"
fi
echo "Using config section: $CONFIG_SECTION"
# Check if project exists in config, otherwise use defaults
PROJECT_EXISTS=$(jq -r --arg repo "$BASE_REPO_NAME" 'has($repo)' "$CONFIG_FILE")
if [[ "$PROJECT_EXISTS" == "true" ]]; then
echo "Found configuration for project: $BASE_REPO_NAME"
CONFIG_PATH=".[\"$BASE_REPO_NAME\"].$CONFIG_SECTION"
else
echo "No specific configuration found for $BASE_REPO_NAME, using defaults"
CONFIG_PATH=".defaults.$CONFIG_SECTION"
fi
# Determine the branch to use
BRANCH="${{ inputs.branch }}"
if [[ -z "$BRANCH" ]]; then
BRANCH="${{ github.ref_name }}"
fi
echo "Target branch: $BRANCH"
# Determine which branches to build based on event type
if [[ "${{ github.event_name }}" == "schedule" ]]; then
echo "Trigger: Scheduled run - building multiple branches"
BRANCHES=$(jq -r "$CONFIG_PATH.branches.scheduled // .defaults.$CONFIG_SECTION.branches.scheduled" "$CONFIG_FILE" | jq -r '.[]')
else
echo "Trigger: ${{ github.event_name }} - building single branch"
BRANCHES="$BRANCH"
fi
echo ""
echo "=== Branches to Build ==="
echo "$BRANCHES"
echo ""
echo "=== Building Matrix ==="
# Build matrix entries
MATRIX_ENTRIES=()
for BRANCH in $BRANCHES; do
echo "Processing branch: $BRANCH"
# Get JDK versions for this branch
JDK_VERSIONS=$(jq -r --arg branch "$BRANCH" \
"$CONFIG_PATH.jdkVersions[\$branch] // $CONFIG_PATH.jdkVersions.default // .defaults.$CONFIG_SECTION.jdkVersions.default" \
"$CONFIG_FILE" | jq -r '.[]')
if [[ -z "$JDK_VERSIONS" ]]; then
# Fallback to global defaults
JDK_VERSIONS=$(jq -r ".defaults.$CONFIG_SECTION.jdkVersions.default[]" "$CONFIG_FILE")
fi
echo " JDK versions: $JDK_VERSIONS"
# Check if JDK 8 is in the configuration for this branch
HAS_JDK8=false
if echo "$JDK_VERSIONS" | grep -q "^8$"; then
HAS_JDK8=true
fi
echo " Has JDK 8: $HAS_JDK8"
for VERSION in $JDK_VERSIONS; do
MATRIX_ENTRIES+=("{\"branch\":\"$BRANCH\",\"java-version\":\"$VERSION\",\"has-jdk8\":$HAS_JDK8}")
done
done
# Join entries with comma and wrap in array
MATRIX_JSON="[$(IFS=,; echo "${MATRIX_ENTRIES[*]}")]"
echo ""
echo "=== Generated Matrix ==="
echo "$MATRIX_JSON" | jq .
echo ""
echo "matrix=$MATRIX_JSON" >> $GITHUB_OUTPUT
build:
name: Build ${{ matrix.branch }} (JDK ${{ matrix.java-version }})
needs: setup
runs-on: ${{ inputs.runs_on || 'ubuntu-latest' }}
timeout-minutes: 60
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.setup.outputs.matrix) }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ matrix.branch }}
clean: true
- name: Set up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java-version }}
distribution: 'temurin'
cache: 'maven'
- name: Configure Maven settings
uses: s4u/maven-settings-action@v3.0.0
with:
servers: |
[{
"id": "repo.spring.io",
"username": "${{ secrets.ARTIFACTORY_USERNAME }}",
"password": "${{ secrets.ARTIFACTORY_PASSWORD }}"
},
{
"id": "spring-commercial-snapshot",
"username": "${{ secrets.COMMERCIAL_ARTIFACTORY_USERNAME }}",
"password": "${{ secrets.COMMERCIAL_ARTIFACTORY_PASSWORD }}"
},
{
"id": "spring-commercial-release",
"username": "${{ secrets.COMMERCIAL_ARTIFACTORY_USERNAME }}",
"password": "${{ secrets.COMMERCIAL_ARTIFACTORY_PASSWORD }}"
}]
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Stop running Docker containers
continue-on-error: true
run: |
#!/bin/bash
if command -v timeout &> /dev/null; then
timeout 10s docker ps -a -q | xargs -n 1 -P 8 -I {} docker stop {} || echo "Failed to stop docker... Hopefully you know what you're doing"
fi
- name: Verify Maven installation
run: ./mvnw --version
- name: Set build configuration
run: |
# Determine if docs profile should be included based on JDK configuration
# Logic: Include docs if (has-jdk8=true AND java-version=8) OR (has-jdk8=false AND java-version=17)
BUILD_WITH_DOCS=false
if [[ ("${{ matrix.has-jdk8 }}" == "true" && "${{ matrix.java-version }}" == "8") ]] || [[ ("${{ matrix.has-jdk8 }}" == "false" && "${{ matrix.java-version }}" == "17") ]]; then
BUILD_WITH_DOCS=true
echo "Docs profile will be included in this build"
else
echo "Docs profile will NOT be included in this build"
fi
echo "BUILD_WITH_DOCS=$BUILD_WITH_DOCS" >> $GITHUB_ENV
echo "Build configuration: BUILD_WITH_DOCS=$BUILD_WITH_DOCS"
- name: Build and deploy
run: |
# Write build command to temporary script file
if [[ -n "${{ inputs.custom_build_command }}" ]]; then
# Custom build command provided by caller
cat > /tmp/build-deploy.sh << CUSTOM_BUILD_EOF
${{ inputs.custom_build_command }}
CUSTOM_BUILD_EOF
else
# Default Maven build command with conditional docs profile
cat > /tmp/build-deploy.sh << 'DEFAULT_BUILD_EOF'
#!/bin/bash
set -e
# BUILD_WITH_DOCS is set by the "Set build configuration" step
if [[ "$BUILD_WITH_DOCS" == "true" ]]; then
echo "Building with docs profile, and deploying docs"
./mvnw clean deploy -Pdocs,deploy,spring -B -U
else
echo "Will not add docs profile, docs will not be deployed"
./mvnw clean deploy -Pdeploy,spring -B -U
fi
DEFAULT_BUILD_EOF
fi
chmod +x /tmp/build-deploy.sh
bash /tmp/build-deploy.sh