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