-
Notifications
You must be signed in to change notification settings - Fork 0
194 lines (190 loc) · 9.99 KB
/
deploy.yaml
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
name: Deploy
on:
workflow_dispatch:
inputs:
config_branch:
description: 'Use branch for testing configuration changes'
required: false
type: string
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
SERVICE_PROJECT: oneteam-example
SERVICE_NAME: java-maven-pipeline-example
JOB: buildByToken/buildWithParameters?job=oneteam-example/java-maven-pipeline-example
URL: https://cd.io.nrs.gov.bc.ca
PACKAGE_REPO: https://maven.pkg.github.com/bcgov/java-maven-pipeline-example
jobs:
deploy-build:
name: Deploy development version
if: ${{ ! startsWith(github.ref, 'refs/tags/') }}
runs-on: ubuntu-latest
outputs:
project_version: ${{ steps.set-build-output.outputs.project_version }}
build_guid: ${{ steps.set-build-output.outputs.build_guid }}
build_number: ${{ steps.set-build-output.outputs.build_number }}
artifact_name: ${{ steps.set-build-output.outputs.artifact_name }}
artifact_sha256: ${{ steps.set-build-output.outputs.artifact_sha256 }}
download_url: ${{ steps.set-download-url.outputs.download_url }}
steps:
- name: Set build output
id: set-build-output
run: |
RESPONSE=$(curl -s -X 'POST' \
"${BROKER_URL}/v1/intention/search?where=%7B%22actions.action%22%3A%22package-build%22%2C%22actions.service.project%22%3A%22${SERVICE_PROJECT}%22%2C%22actions.service.name%22%3A%22${SERVICE_NAME}%22%2C%22actions.package.buildVersion%22%3A%22${GITHUB_SHA}%22%7D&offset=0&limit=1" \
-H 'accept: application/json' \
-H 'Authorization: Bearer '"${BROKER_JWT}"'' \
-d '')
if echo "$RESPONSE" | jq -e '.statusCode == 401' > /dev/null; then
echo "Status code is 401. This indicates an authorization error."
echo "Please check if your Broker Token is valid. Teams can generate a new one in Broker if needed."
echo "Link: https://broker.io.nrs.gov.bc.ca/browse/brokerAccount"
echo "Exiting..."
exit 1
fi
echo "project_version=$(echo ${RESPONSE} | jq -r '.data[].actions[].package.version')" >> $GITHUB_OUTPUT
echo "build_guid=$(echo ${RESPONSE} | jq -r '.data[].id')" >> $GITHUB_OUTPUT
echo "build_number=$(echo ${RESPONSE} | jq -r '.data[].actions[].package.buildNumber')" >> $GITHUB_OUTPUT
echo "artifact_name=$(echo ${RESPONSE} | jq -r '.data[].actions[].artifacts[].name')" >> $GITHUB_OUTPUT
artifact_checksum=$(echo ${RESPONSE} | jq -r '.data[].actions[].artifacts[].checksum')
echo "artifact_sha256=${artifact_checksum#sha256:}" >> $GITHUB_OUTPUT
env:
BROKER_URL: https://broker.io.nrs.gov.bc.ca
BROKER_JWT: ${{ secrets.broker_jwt_0e727e83_f027_426a_a31c_d7c1cceaddf6 }}
SERVICE_PROJECT: ${{ env.SERVICE_PROJECT }}
SERVICE_NAME: ${{ env.SERVICE_NAME }}
GITHUB_SHA: ${{ github.sha }}
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
ref: ${{ github.ref }}
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'temurin'
cache: maven
- name: Set download URL
id: set-download-url
run: |
sudo apt-get -qq install libxml2-utils
GROUP_ID=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout --file ./pom.xml)
GROUP_ID_PATH="${GROUP_ID//.//}"
PACKAGE_TYPE=$(mvn help:evaluate -Dexpression=project.packaging -q -DforceStdout --file ./pom.xml)
curl -LO "${PACKAGE_REPO}/${GROUP_ID_PATH}/${SERVICE_NAME}/${PROJECT_VERSION}/maven-metadata.xml" -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}"
TIMESTAMP=$(xmllint --xpath '//metadata/versioning/snapshot/timestamp/text()' maven-metadata.xml)
BUILD_NUMBER=$(xmllint --xpath '//metadata/versioning/snapshot/buildNumber/text()' maven-metadata.xml)
SNAPSHOT_VERSION="${PROJECT_VERSION//SNAPSHOT/$TIMESTAMP-$BUILD_NUMBER}"
ARTIFACT_NAME="${SERVICE_NAME}-${SNAPSHOT_VERSION}.${PACKAGE_TYPE}"
DOWNLOAD_URL="${PACKAGE_REPO}/${GROUP_ID_PATH}/${SERVICE_NAME}/${PROJECT_VERSION}/${ARTIFACT_NAME}"
echo "${DOWNLOAD_URL}"
echo "download_url=$(echo ${DOWNLOAD_URL})" >> $GITHUB_OUTPUT
env:
SERVICE_NAME: ${{ env.SERVICE_NAME }}
PROJECT_VERSION: ${{ steps.set-build-output.outputs.project_version }}
PACKAGE_REPO: ${{ env.PACKAGE_REPO }}
deploy-tag:
name: Deploy release version
if: ${{ startsWith(github.ref, 'refs/tags/') }}
runs-on: ubuntu-latest
outputs:
project_version: ${{ steps.set-tag-output.outputs.project_version }}
build_guid: ${{ steps.set-tag-output.outputs.build_guid }}
build_number: ${{ steps.set-tag-output.outputs.build_number }}
artifact_name: ${{ steps.set-tag-output.outputs.artifact_name }}
artifact_sha256: ${{ steps.set-tag-output.outputs.artifact_sha256 }}
download_url: ${{ steps.set-download-url.outputs.download_url }}
steps:
- name: Set tag output
id: set-tag-output
run: |
TAG=${{ github.ref_name }}
PROJECT_VERSION=${TAG#v}
RESPONSE=$(curl -s -X 'POST' \
"${BROKER_URL}/v1/intention/search?where=%7B%22actions.action%22%3A%22package-build%22%2C%22actions.service.project%22%3A%22${SERVICE_PROJECT}%22%2C%22actions.service.name%22%3A%22${SERVICE_NAME}%22%2C%22actions.package.version%22%3A%22${PROJECT_VERSION}%22%7D&offset=0&limit=1" \
-H 'accept: application/json' \
-H 'Authorization: Bearer '"${BROKER_JWT}"'' \
-d '')
if echo "$RESPONSE" | jq -e '.statusCode == 401' > /dev/null; then
echo "Status code is 401. This indicates an authorization error."
echo "Please check if your Broker Token is valid. Teams can generate a new one in Broker if needed."
echo "Link: https://broker.io.nrs.gov.bc.ca/browse/brokerAccount"
echo "Exiting..."
exit 1
fi
echo "project_version=${PROJECT_VERSION}" >> $GITHUB_OUTPUT
echo "build_guid=$(echo ${RESPONSE} | jq -r '.data[].id')" >> $GITHUB_OUTPUT
echo "build_number=$(echo ${RESPONSE} | jq -r '.data[].actions[].package.buildNumber')" >> $GITHUB_OUTPUT
echo "artifact_name=$(echo ${RESPONSE} | jq -r '.data[].actions[].artifacts[].name')" >> $GITHUB_OUTPUT
artifact_checksum=$(echo ${RESPONSE} | jq -r '.data[].actions[].artifacts[].checksum')
echo "artifact_sha256=${artifact_checksum#sha256:}" >> $GITHUB_OUTPUT
env:
BROKER_URL: https://broker.io.nrs.gov.bc.ca
BROKER_JWT: ${{ secrets.broker_jwt_0e727e83_f027_426a_a31c_d7c1cceaddf6 }}
SERVICE_PROJECT: ${{ env.SERVICE_PROJECT }}
SERVICE_NAME: ${{ env.SERVICE_NAME }}
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
ref: ${{ github.ref }}
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'temurin'
cache: maven
- name: Set download URL
id: set-download-url
run: |
sudo apt-get -qq install libxml2-utils
GROUP_ID=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout --file ./pom.xml)
GROUP_ID_PATH="${GROUP_ID//.//}"
PACKAGE_TYPE=$(mvn help:evaluate -Dexpression=project.packaging -q -DforceStdout --file ./pom.xml)
ARTIFACT_NAME="${SERVICE_NAME}-${PROJECT_VERSION}.${PACKAGE_TYPE}"
DOWNLOAD_URL="${PACKAGE_REPO}/${GROUP_ID_PATH}/${SERVICE_NAME}/${PROJECT_VERSION}/${ARTIFACT_NAME}"
echo "${DOWNLOAD_URL}"
echo "download_url=$(echo ${DOWNLOAD_URL})" >> $GITHUB_OUTPUT
env:
SERVICE_NAME: ${{ env.SERVICE_NAME }}
PROJECT_VERSION: ${{ steps.set-tag-output.outputs.project_version }}
PACKAGE_REPO: ${{ env.PACKAGE_REPO }}
submit-job:
name: Submit job
if: |
always() &&
((needs.deploy-build.result == 'success' && needs.deploy-tag.result == 'skipped') ||
(needs.deploy-build.result == 'skipped' && needs.deploy-tag.result == 'success'))
needs: [deploy-build, deploy-tag]
runs-on: ubuntu-latest
steps:
- name: Submit a job to Jenkins
run: |
curl \
--data-urlencode "token=${{ secrets.JENKINS_TOKEN }}" \
--data-urlencode "githubToken=${{ secrets.GITHUB_TOKEN }}" \
--data-urlencode "artifactSha256=${ARTIFACT_SHA256}" \
--data-urlencode "projectVersion=${PROJECT_VERSION}" \
--data-urlencode "gitBranch=${{ github.ref_name }}" \
--data-urlencode "intentionId=${BUILD_GUID}" \
--data-urlencode "gitTag=${{ (startsWith(github.ref, 'refs/tags/') && github.ref_name) || '' }}" \
--data-urlencode "configBranch=${{ inputs.config_branch || '' }}" \
--data-urlencode "downloadUrl=${DOWNLOAD_URL}" \
--data-urlencode "downloadType=GITHUB" \
-H "Connection: close" \
${{ env.URL }}/${{ env.JOB }}
env:
PROJECT_VERSION: ${{ needs.deploy-build.outputs.project_version || needs.deploy-tag.outputs.project_version }}
BUILD_GUID: ${{ needs.deploy-build.outputs.build_guid || needs.deploy-tag.outputs.build_guid }}
BUILD_NUMBER: ${{ needs.deploy-build.outputs.build_number || needs.deploy-tag.outputs.build_number }}
ARTIFACT_SHA256: ${{ needs.deploy-build.outputs.artifact_sha256 || needs.deploy-tag.outputs.artifact_sha256 }}
DOWNLOAD_URL: ${{ needs.deploy-build.outputs.download_url || needs.deploy-tag.outputs.download_url }}
# The automatically generated GitHub token will expire when the workflow ends. We need to wait so the job has time to clone the repo
# and download the package
- name: Sleep
run: sleep 90s
shell: bash