Skip to content

Commit d8c01f4

Browse files
update build
1 parent c76e406 commit d8c01f4

File tree

6 files changed

+208
-320
lines changed

6 files changed

+208
-320
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
name: CI Build and Publish
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
AZURE_DEVOPS_ARTIFACT_USERNAME:
7+
required: true
8+
AZURE_DEVOPS_ARTIFACT_TOKEN:
9+
required: true
10+
HMCTS_ADO_PAT:
11+
required: true
12+
inputs:
13+
is_release:
14+
required: false
15+
type: boolean
16+
default: false
17+
is_publish:
18+
required: true
19+
type: boolean
20+
trigger_docker:
21+
required: true
22+
type: boolean
23+
trigger_deploy:
24+
required: true
25+
type: boolean
26+
27+
jobs:
28+
Artefact-Version:
29+
runs-on: ubuntu-latest
30+
outputs:
31+
artefact_version: ${{ inputs.is_release && steps.artefact.outputs.release_version || steps.artefact.outputs.draft_version }}
32+
steps:
33+
- name: Checkout code
34+
uses: actions/checkout@v4
35+
with:
36+
fetch-depth: 0
37+
38+
- name: Generate Artefact Version
39+
id: artefact
40+
uses: hmcts/artefact-version-action@v1
41+
with:
42+
release: ${{ inputs.is_release }}
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
46+
Build:
47+
needs: [Artefact-Version]
48+
runs-on: ubuntu-latest
49+
outputs:
50+
repo_name: ${{ steps.repo_vars.outputs.repo_name }}
51+
artefact_name: ${{ steps.repo_vars.outputs.artefact_name }}
52+
steps:
53+
- name: Checkout source code
54+
uses: actions/checkout@v4
55+
56+
- name: Set up JDK
57+
uses: actions/setup-java@v4
58+
with:
59+
distribution: 'temurin'
60+
java-version: '21'
61+
62+
- name: Set up Gradle
63+
uses: gradle/actions/setup-gradle@v4
64+
with:
65+
gradle-version: current
66+
67+
- name: Gradle Build and Publish
68+
env:
69+
ARTEFACT_VERSION: ${{ needs.Artefact-Version.outputs.artefact_version }}
70+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71+
AZURE_DEVOPS_ARTIFACT_USERNAME: ${{ secrets.AZURE_DEVOPS_ARTIFACT_USERNAME }}
72+
AZURE_DEVOPS_ARTIFACT_TOKEN: ${{ secrets.AZURE_DEVOPS_ARTIFACT_TOKEN }}
73+
run: |
74+
echo "Building with ARTEFACT_VERSION=$ARTEFACT_VERSION"
75+
76+
gradle build -DARTEFACT_VERSION=$ARTEFACT_VERSION
77+
78+
if [ -z "AZURE_DEVOPS_ARTIFACT_USERNAME" ]; then
79+
echo "::warning::AZURE_DEVOPS_ARTIFACT_USERNAME is null or not set"
80+
fi
81+
82+
if [ -z "$AZURE_DEVOPS_ARTIFACT_TOKEN" ]; then
83+
echo "::warning::AZURE_DEVOPS_ARTIFACT_TOKEN is null or not set"
84+
fi
85+
86+
if [ "${{ inputs.is_publish }}" == "true" ]; then
87+
echo "Publishing artefact for version: $ARTEFACT_VERSION"
88+
89+
gradle publish \
90+
-DARTEFACT_VERSION=$ARTEFACT_VERSION \
91+
-DGITHUB_REPOSITORY=${{ github.repository }} \
92+
-DGITHUB_ACTOR=${{ github.actor }} \
93+
-DGITHUB_TOKEN=$GITHUB_TOKEN \
94+
-DAZURE_DEVOPS_ARTIFACT_USERNAME=$AZURE_DEVOPS_ARTIFACT_USERNAME \
95+
-DAZURE_DEVOPS_ARTIFACT_TOKEN=$AZURE_DEVOPS_ARTIFACT_TOKEN
96+
fi
97+
98+
- name: Extract repo name
99+
id: repo_vars
100+
run: |
101+
repo_name=${GITHUB_REPOSITORY##*/}
102+
echo "repo_name=${repo_name}" >> $GITHUB_OUTPUT
103+
echo "artefact_name=${repo_name}-${{ needs.Artefact-Version.outputs.artefact_version }}" >> $GITHUB_OUTPUT
104+
105+
- name: Upload JAR Artefact
106+
uses: actions/upload-artifact@v4
107+
with:
108+
name: app-jar
109+
path: build/libs/${{ steps.repo_vars.outputs.artefact_name }}.jar
110+
111+
Build-Docker:
112+
needs: [ Build, Artefact-Version ]
113+
if: ${{ inputs.trigger_docker }}
114+
runs-on: ubuntu-latest
115+
steps:
116+
- name: Checkout Code
117+
uses: actions/checkout@v4
118+
119+
- name: Download JAR Artefact
120+
uses: actions/download-artifact@v4
121+
with:
122+
name: app-jar
123+
path: build/libs
124+
125+
- name: Set up Docker Buildx
126+
uses: docker/setup-buildx-action@v3
127+
128+
- name: Log in to GitHub Packages
129+
uses: docker/login-action@v3
130+
with:
131+
registry: ghcr.io
132+
username: ${{ github.actor }}
133+
password: ${{ secrets.GITHUB_TOKEN }}
134+
135+
- name: Build and Push Docker Image to GitHub
136+
uses: docker/build-push-action@v6
137+
with:
138+
context: .
139+
file: Dockerfile
140+
push: true
141+
tags: |
142+
ghcr.io/${{ github.repository }}:${{ needs.Artefact-Version.outputs.artefact_version }}
143+
build-args: |
144+
BASE_IMAGE=openjdk:21-jdk-slim
145+
JAR_FILENAME=${{ needs.Build.outputs.artefact_name }}.jar
146+
147+
Deploy:
148+
needs: [ Build, Artefact-Version ]
149+
if: ${{ inputs.trigger_deploy }}
150+
runs-on: ubuntu-latest
151+
steps:
152+
- name: Extract repo name
153+
run: echo "REPO_NAME=${GITHUB_REPOSITORY##*/}" >> $GITHUB_ENV
154+
155+
- name: Trigger ADO pipeline
156+
uses: hmcts/trigger-ado-pipeline@v1
157+
with:
158+
pipeline_id: 460
159+
ado_pat: ${{ secrets.HMCTS_ADO_PAT }}
160+
template_parameters: >
161+
{
162+
"GROUP_ID": "uk.gov.hmcts.cp",
163+
"ARTIFACT_ID": "${{ env.REPO_NAME }}",
164+
"ARTIFACT_VERSION": "${{ needs.Artefact-Version.outputs.artefact_version }}",
165+
"TARGET_REPOSITORY": "${{ github.repository }}"
166+
}

.github/workflows/ci-draft.yml

Lines changed: 11 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI Build and Publish Increments Draft
1+
name: Build and Publish (Non-Release)
22

33
on:
44
pull_request:
@@ -11,166 +11,14 @@ on:
1111
- main
1212

1313
jobs:
14-
Artefact-Version:
15-
runs-on: ubuntu-latest
16-
outputs:
17-
draft_version: ${{ steps.vars.outputs.draft_version }}
18-
latest_tag: ${{ steps.vars.outputs.latest_tag }}
19-
steps:
20-
- name: Checkout code
21-
uses: actions/checkout@v4
22-
with:
23-
fetch-depth: 0
14+
ci-draft:
15+
uses: ./.github/workflows/ci-build-publish.yml
16+
secrets:
17+
AZURE_DEVOPS_ARTIFACT_USERNAME: ${{ secrets.AZURE_DEVOPS_ARTIFACT_USERNAME }}
18+
AZURE_DEVOPS_ARTIFACT_TOKEN: ${{ secrets.AZURE_DEVOPS_ARTIFACT_TOKEN }}
19+
HMCTS_ADO_PAT: ${{ secrets.HMCTS_ADO_PAT }}
20+
with:
21+
is_publish: ${{ github.event_name == 'push' }}
22+
trigger_docker: ${{ github.event_name == 'push' }}
23+
trigger_deploy: ${{ github.event_name == 'push' }}
2424

25-
- name: Get short SHA for versioning
26-
id: vars
27-
run: |
28-
if LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null); then
29-
:
30-
else
31-
LATEST_TAG="v0.0.0"
32-
fi
33-
echo "🏷️ Latest Git tag resolved to: $LATEST_TAG"
34-
LATEST_TAG="${LATEST_TAG#v}"
35-
36-
echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
37-
38-
SHORT_SHA=$(git rev-parse --short HEAD)
39-
DRAFT_VERSION="${LATEST_TAG}-${SHORT_SHA}"
40-
41-
echo "draft_version=$DRAFT_VERSION"
42-
echo "draft_version=$DRAFT_VERSION" >> $GITHUB_OUTPUT
43-
44-
Build:
45-
needs: [Artefact-Version]
46-
runs-on: ubuntu-latest
47-
outputs:
48-
repo_name: ${{ steps.repo_vars.outputs.repo_name }}
49-
artefact_name: ${{ steps.repo_vars.outputs.artefact_name }}
50-
51-
steps:
52-
- name: Checkout source code
53-
uses: actions/checkout@v4
54-
55-
- name: Set up JDK
56-
uses: actions/setup-java@v4
57-
with:
58-
distribution: 'temurin'
59-
java-version: '21'
60-
61-
- name: Set up Gradle
62-
uses: gradle/actions/setup-gradle@v4
63-
with:
64-
gradle-version: current
65-
66-
- name: Gradle Build and Publish on Push [Merge]
67-
env:
68-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69-
AZURE_DEVOPS_ARTIFACT_USERNAME: ${{ secrets.AZURE_DEVOPS_ARTIFACT_USERNAME }}
70-
AZURE_DEVOPS_ARTIFACT_TOKEN: ${{ secrets.AZURE_DEVOPS_ARTIFACT_TOKEN }}
71-
run: |
72-
VERSION=${{ needs.Artefact-Version.outputs.draft_version }}
73-
74-
gradle build -DAPI_SPEC_VERSION=$VERSION
75-
76-
if [ "${{ github.event_name }}" == "push" ]; then
77-
echo "Push event trigger - Publishing artefact"
78-
gradle publish \
79-
-DAPI_SPEC_VERSION=$VERSION \
80-
-DGITHUB_REPOSITORY=${{ github.repository }} \
81-
-DGITHUB_ACTOR=${{ github.actor }} \
82-
-DGITHUB_TOKEN=$GITHUB_TOKEN \
83-
-DAZURE_DEVOPS_ARTIFACT_USERNAME=$AZURE_DEVOPS_ARTIFACT_USERNAME \
84-
-DAZURE_DEVOPS_ARTIFACT_TOKEN=$AZURE_DEVOPS_ARTIFACT_TOKEN
85-
else
86-
echo "Skipping publish because this is a pull_request"
87-
fi
88-
89-
- name: Extract repo name
90-
if: github.event_name == 'push'
91-
id: repo_vars
92-
run: |
93-
repo_name=${GITHUB_REPOSITORY##*/}
94-
echo "repo_name=${repo_name}" >> $GITHUB_OUTPUT
95-
echo "artefact_name=${repo_name}-${{ needs.Artefact-Version.outputs.draft_version }}" >> $GITHUB_OUTPUT
96-
97-
- name: Upload JAR Artefact
98-
uses: actions/upload-artifact@v4
99-
if: github.event_name == 'push'
100-
with:
101-
name: app-jar
102-
path: build/libs/${{ steps.repo_vars.outputs.artefact_name }}.jar
103-
104-
Build-Docker:
105-
needs: [ Build, Artefact-Version ]
106-
runs-on: ubuntu-latest
107-
if: github.event_name == 'push'
108-
109-
steps:
110-
- name: Checkout Code
111-
uses: actions/checkout@v4
112-
113-
- name: Download JAR Artefact
114-
uses: actions/download-artifact@v4
115-
with:
116-
name: app-jar
117-
path: build/libs
118-
119-
- name: Set up Docker Buildx
120-
uses: docker/setup-buildx-action@v3
121-
122-
- name: Log in to GitHub Packages
123-
uses: docker/login-action@v3
124-
with:
125-
registry: ghcr.io
126-
username: ${{ github.actor }}
127-
password: ${{ secrets.GITHUB_TOKEN }}
128-
129-
- name: Build and Push Docker Image to GitHub
130-
uses: docker/build-push-action@v6
131-
with:
132-
context: .
133-
file: Dockerfile
134-
push: true
135-
tags: |
136-
ghcr.io/${{ github.repository }}:${{ needs.Artefact-Version.outputs.draft_version }}
137-
build-args: |
138-
BASE_IMAGE=openjdk:21-jdk-slim
139-
JAR_FILENAME=${{ needs.Build.outputs.artefact_name }}.jar
140-
141-
Deploy:
142-
needs: [ Build, Artefact-Version ]
143-
runs-on: ubuntu-latest
144-
if: github.event_name == 'push'
145-
146-
steps:
147-
- name: Trigger ADO pipeline
148-
env:
149-
ADO_ORG: 'hmcts-cpp'
150-
ADO_PROJECT: 'cpp-apps'
151-
PIPELINE_ID: 460 #cp-gh-artifact-to-acr
152-
ADO_PAT: ${{ secrets.HMCTS_ADO_PAT }}
153-
run: |
154-
ARTEFACT_VERSION="${{ needs.Artefact-Version.outputs.draft_version }}"
155-
REPO_NAME="${GITHUB_REPOSITORY##*/}"
156-
TARGET_REPOSITORY="${GITHUB_REPOSITORY}"
157-
158-
curl -X POST \
159-
-u ":${ADO_PAT}" \
160-
-H "Content-Type: application/json" \
161-
https://dev.azure.com/${ADO_ORG}/${ADO_PROJECT}/_apis/pipelines/${PIPELINE_ID}/runs?api-version=7.0 \
162-
-d "{
163-
\"resources\": {
164-
\"repositories\": {
165-
\"self\": {
166-
\"refName\": \"refs/heads/main\"
167-
}
168-
}
169-
},
170-
\"templateParameters\": {
171-
\"GROUP_ID\": \"uk.gov.hmcts.cp\",
172-
\"ARTIFACT_ID\": \"${REPO_NAME}\",
173-
\"ARTIFACT_VERSION\": \"${ARTEFACT_VERSION}\",
174-
\"TARGET_REPOSITORY\": \"${TARGET_REPOSITORY}\"
175-
}
176-
}"

0 commit comments

Comments
 (0)