|
| 1 | +name: CI Build and Publish Increments Draft |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - main |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - master |
| 11 | + - main |
| 12 | + |
| 13 | +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 |
| 24 | + |
| 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 \ |
| 75 | + -DAPI_SPEC_VERSION=$VERSION \ |
| 76 | + -DGITHUB_REPOSITORY=${{ github.repository }} \ |
| 77 | + -DGITHUB_ACTOR=${{ github.actor }} \ |
| 78 | + -DGITHUB_TOKEN=$GITHUB_TOKEN |
| 79 | +
|
| 80 | + if [ "${{ github.event_name }}" == "push" ]; then |
| 81 | + echo "Push event trigger - Publishing artefact" |
| 82 | + gradle publish \ |
| 83 | + -DAPI_SPEC_VERSION=$VERSION \ |
| 84 | + -DGITHUB_REPOSITORY=${{ github.repository }} \ |
| 85 | + -DGITHUB_ACTOR=${{ github.actor }} \ |
| 86 | + -DGITHUB_TOKEN=$GITHUB_TOKEN \ |
| 87 | + -DAZURE_DEVOPS_ARTIFACT_USERNAME=$HMCTS_ARTEFACT_ACTOR \ |
| 88 | + -DAZURE_DEVOPS_ARTIFACT_TOKEN=$AZURE_DEVOPS_ARTIFACT_TOKEN |
| 89 | + else |
| 90 | + echo "Skipping publish because this is a pull_request" |
| 91 | + fi |
| 92 | +
|
| 93 | +
|
| 94 | + - name: Extract repo name |
| 95 | + if: github.event_name == 'push' |
| 96 | + id: repo_vars |
| 97 | + run: | |
| 98 | + repo_name=${GITHUB_REPOSITORY##*/} |
| 99 | + echo "repo_name=${repo_name}" >> $GITHUB_OUTPUT |
| 100 | + echo "artefact_name=${repo_name}-${{ needs.Artefact-Version.outputs.draft_version }}" >> $GITHUB_OUTPUT |
| 101 | +
|
| 102 | + - name: Upload JAR Artefact |
| 103 | + uses: actions/upload-artifact@v4 |
| 104 | + if: github.event_name == 'push' |
| 105 | + with: |
| 106 | + name: app-jar |
| 107 | + path: build/libs/${{ steps.repo_vars.outputs.artefact_name }}.jar |
| 108 | + |
| 109 | + Build-Docker: |
| 110 | + needs: [ Build, Artefact-Version ] |
| 111 | + runs-on: ubuntu-latest |
| 112 | + if: github.event_name == 'push' |
| 113 | + |
| 114 | + steps: |
| 115 | + - name: Checkout Code |
| 116 | + uses: actions/checkout@v4 |
| 117 | + |
| 118 | + - name: Download JAR Artefact |
| 119 | + uses: actions/download-artifact@v4 |
| 120 | + with: |
| 121 | + name: app-jar |
| 122 | + path: build/libs |
| 123 | + |
| 124 | + - name: Set up Docker Buildx |
| 125 | + uses: docker/setup-buildx-action@v3 |
| 126 | + |
| 127 | + - name: Log in to GitHub Packages |
| 128 | + uses: docker/login-action@v3 |
| 129 | + with: |
| 130 | + registry: ghcr.io |
| 131 | + username: ${{ github.actor }} |
| 132 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 133 | + |
| 134 | + - name: Build and Push Docker Image to GitHub |
| 135 | + uses: docker/build-push-action@v6 |
| 136 | + with: |
| 137 | + context: . |
| 138 | + file: Dockerfile |
| 139 | + push: true |
| 140 | + tags: | |
| 141 | + ghcr.io/${{ github.repository }}:${{ needs.Artefact-Version.outputs.draft_version }} |
| 142 | + build-args: | |
| 143 | + BASE_IMAGE=openjdk:21-jdk-slim |
| 144 | + JAR_FILENAME=${{ needs.Build.outputs.artefact_name }}.jar |
| 145 | +
|
| 146 | + # https://github.com/marketplace/actions/azure-pipelines-action |
| 147 | + Deploy: |
| 148 | + needs: [ Build, Artefact-Version ] |
| 149 | + runs-on: ubuntu-latest |
| 150 | + if: github.event_name == 'push' |
| 151 | + |
| 152 | + steps: |
| 153 | + - name: Trigger ADO pipeline |
| 154 | + uses: Azure/pipelines@v1.2 |
| 155 | + with: |
| 156 | + azure-devops-project-url: 'https://dev.azure.com/hmcts-cpp/cpp-apps' |
| 157 | + azure-pipeline-name: 'cp-gh-artifact-to-acr' |
| 158 | + azure-devops-token: ${{ secrets.HMCTS_ADO_PAT }} |
| 159 | + azure-pipeline-variables: >- |
| 160 | + { |
| 161 | + "GROUP_ID" : "uk.gov.hmcts.cp", |
| 162 | + "ARTIFACT_ID" : "${{ github.repository }}", |
| 163 | + "ARTIFACT_VERSION" : "${{ needs.Artefact-Version.outputs.draft_version}}" |
| 164 | + } |
0 commit comments