From 5d2c1f60e5960ba8d9c2246844f25422838c25a8 Mon Sep 17 00:00:00 2001 From: GraceRuan Date: Fri, 15 Mar 2024 11:42:24 -0700 Subject: [PATCH] feat: clean commit for build-release changes --- generators/gh-maven-build/index.js | 13 +- .../{build.yaml => build-release.yaml} | 90 ++++++++------ .../gh-maven-build/templates/release.yaml | 111 ------------------ 3 files changed, 55 insertions(+), 159 deletions(-) rename generators/gh-maven-build/templates/{build.yaml => build-release.yaml} (69%) delete mode 100644 generators/gh-maven-build/templates/release.yaml diff --git a/generators/gh-maven-build/index.js b/generators/gh-maven-build/index.js index f307229..61fe7e1 100644 --- a/generators/gh-maven-build/index.js +++ b/generators/gh-maven-build/index.js @@ -114,8 +114,8 @@ export default class extends Generator { // Generate GitHub workflows and NR Broker intention files writing() { this.fs.copyTpl( - this.templatePath('build.yaml'), - this.destinationPath('.github/workflows/build.yaml'), + this.templatePath('build-release.yaml'), + this.destinationPath('.github/workflows/build-release.yaml'), { projectName: this.props.projectName, serviceName: this.props.serviceName, @@ -137,15 +137,6 @@ export default class extends Generator { this.templatePath('build-intention.sh'), this.destinationPath('.github/workflows/build-intention.sh'), ); - this.fs.copyTpl( - this.templatePath('release.yaml'), - this.destinationPath('.github/workflows/release.yaml'), - { - projectName: this.props.projectName, - serviceName: this.props.serviceName, - pomRoot: this.props.pomRoot, - }, - ); if (this.props.deployOnPrem) { this.fs.copyTpl( this.templatePath('deploy.yaml'), diff --git a/generators/gh-maven-build/templates/build.yaml b/generators/gh-maven-build/templates/build-release.yaml similarity index 69% rename from generators/gh-maven-build/templates/build.yaml rename to generators/gh-maven-build/templates/build-release.yaml index 268ffa7..ee59353 100644 --- a/generators/gh-maven-build/templates/build.yaml +++ b/generators/gh-maven-build/templates/build-release.yaml @@ -1,17 +1,12 @@ -name: Build +name: Build and release on: + push: + tags: + - 'v*' pull_request: branches: - main - workflow_call: - outputs: - branch: - value: ${{ jobs.build.outputs.branch }} - project_version: - value: ${{ jobs.build.outputs.project_version }} - intention_id: - value: ${{ jobs.build.outputs.intention_id }} workflow_dispatch: concurrency: @@ -25,26 +20,29 @@ env: SERVICE_NAME: <%= serviceName %> jobs: -<% if (unitTestsPath) { -%> +<% if (unitTestsPath) { -%> test: name: Test + if: ${{ ! startsWith(github.ref, 'refs/tags/v') }} uses: <%= unitTestsPath %> <% } -%> - build: - name: Build + build-release: + name: Build and release runs-on: ubuntu-latest <% if (unitTestsPath) { -%> + if: ${{ ! startsWith(github.ref, 'refs/tags/v') }} needs: test <% } -%> permissions: contents: read packages: write outputs: - branch: ${{ steps.set-branch.outputs.branch }} project_version: ${{ steps.set-build-output-parameters.outputs.project_version }} intention_id: ${{ steps.set-intention-id.outputs.intention_id }} + branch: $${{ steps.set-branch.outputs.branch }} steps: - - uses: actions/checkout@v4 + - name: Checkout repository + uses: actions/checkout@v4 with: fetch-depth: 0 fetch-tags: true @@ -56,7 +54,8 @@ jobs: distribution: 'temurin' cache: maven - name: Set branch - id: set-branch + id: set-branch + if: ${{ ! startsWith(github.ref, 'refs/tags/v') }} run: | if [[ ${{ startsWith(github.ref, 'refs/heads/') }} = true ]]; then echo "branch=${{ github.ref_name }}" >> "$GITHUB_OUTPUT" @@ -64,11 +63,21 @@ jobs: if [[ ${{ startsWith(github.ref, 'refs/pull/') }} = true ]]; then echo "branch=${{ github.head_ref }}" >> "$GITHUB_OUTPUT" fi + - name: Update pom.xml + id: update-repo + if: ${{ startsWith(github.ref, 'refs/tags/v') }} + shell: bash + run: | + TAG=${{ github.ref_name }} + VERSION=${TAG#v} + echo "VERSION=${VERSION}" >> $GITHUB_ENV + mvn versions:set -DnewVersion=${VERSION} --file <%= pomRoot %>pom.xml - name: Set build output parameters id: set-build-output-parameters run: | echo "project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout --file <%= pomRoot %>pom.xml)" >> $GITHUB_OUTPUT echo "artifact_id=$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout --file <%= pomRoot %>pom.xml)" >> $GITHUB_OUTPUT + echo "package_type=$(mvn help:evaluate -Dexpression=project.packaging -q -DforceStdout --file <%= pomRoot %>pom.xml)" >> $GITHUB_OUTPUT echo "git_commit=$(git rev-parse --verify HEAD)" >> $GITHUB_OUTPUT - name: Build intention run: sh ./.github/workflows/build-intention.sh @@ -93,7 +102,32 @@ jobs: echo "intention_id=${INTENTION_ID}" >> $GITHUB_OUTPUT env: INTENTION_ID: ${{ env.INTENTION_ID }} - - name: Publish to OpenShift Artifactory + - name: Publish artifact + id: publish-artifact +<% if (gitHubPackages) { -%> + run: | + ARTIFACT_NAME="${ARTIFACT_ID}-${PROJECT_VERSION}.${PACKAGE_TYPE}" + mvn --batch-mode -Dmaven.test.skip=true -Pgithub deploy --file <%= pomRoot %>pom.xml + echo "artifact_sha256=$(sha256sum target/${ARTIFACT_NAME} | awk '{ print $1 }')" >> $GITHUB_OUTPUT + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PROJECT_VERSION: ${{ steps.set-build-output-parameters.outputs.project_version }} + ARTIFACT_ID: ${{ steps.set-build-output-parameters.outputs.artifact_id }} + PACKAGE_TYPE: ${{ steps.set-build-output-parameters.outputs.package_type }} + - name: Send build info + run: | + ARTIFACT_NAME="${ARTIFACT_ID}-${PROJECT_VERSION}.${PACKAGE_TYPE}" + curl -s POST ${{ env.BROKER_URL }}/v1/intention/action/artifact \ + -H 'Content-Type: application/json' \ + -H 'X-Broker-Token: '"${BUILD_TOKEN}"'' \ + -d '{"checksum": "sha256:'${ARTIFACT_SHA256}'", "name": "'${ARTIFACT_NAME}'", "type": "'${PACKAGE_TYPE}'" }' + env: + ARTIFACT_SHA256: ${{ steps.publish-artifact.outputs.artifact_sha256 }} + ARTIFACT_ID: ${{ steps.set-build-output-parameters.outputs.artifact_id }} + PROJECT_VERSION: ${{ steps.set-build-output-parameters.outputs.project_version }} + PACKAGE_TYPE: ${{ steps.set-build-output-parameters.outputs.package_type }} + BUILD_TOKEN: ${{ env.ACTION_TOKEN_BUILD }} +<% } else { -%> run: mvn --batch-mode -Dmaven.test.skip=true -Partifactory deploy --file <%= pomRoot %>pom.xml env: ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }} @@ -104,7 +138,7 @@ jobs: id: set-build-info run: | BUILD_INFO=$(curl -s -u ${{ secrets.ARTIFACTORY_USERNAME }}:${{ secrets.ARTIFACTORY_PASSWORD }} \ - https://artifacts.developer.gov.bc.ca/artifactory/api/build/${{ env.ARTIFACT_ID }}/${{ github.run_number }}?project=cc20) + https://artifacts.developer.gov.bc.ca/artifactory/api/build/${{ env.ARTIFACT_ID }}/${{ github.run_number }}?project=<%= artifactoryProject %>) echo "vcs_revision=$(echo ${BUILD_INFO} | jq -r '.buildInfo.vcs[0].revision')" >> $GITHUB_OUTPUT echo "vcs_message=$(echo ${BUILD_INFO} | jq -r '.buildInfo.vcs[0].message')" >> $GITHUB_OUTPUT echo "vcs_branch=$(echo ${BUILD_INFO} | jq -r '.buildInfo.vcs[0].branch')" >> $GITHUB_OUTPUT @@ -127,27 +161,9 @@ jobs: ARTIFACT_NAME: ${{ steps.set-build-info.outputs.artifact_name }} ARTIFACT_TYPE: ${{ steps.set-build-info.outputs.artifact_type }} BUILD_TOKEN: ${{ env.ACTION_TOKEN_BUILD }} +<% } -%> - name: Close intention uses: bcgov-nr/action-broker-intention-close@v1 with: intention_token: ${{ env.INTENTION_TOKEN }} -<% if (gitHubPackages) { -%> - ghcr: - name: GHCR - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - uses: actions/checkout@v4 - - name: Set up Java for publishing to GitHub Packages - uses: actions/setup-java@v3 - with: - java-version: '8' - distribution: 'temurin' - cache: maven - - name: Publish to GitHub Packages - run: mvn --batch-mode -Dmaven.test.skip=true -Pgithub deploy --file <%= pomRoot %>pom.xml - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -<% } %> \ No newline at end of file + \ No newline at end of file diff --git a/generators/gh-maven-build/templates/release.yaml b/generators/gh-maven-build/templates/release.yaml deleted file mode 100644 index 51db2bd..0000000 --- a/generators/gh-maven-build/templates/release.yaml +++ /dev/null @@ -1,111 +0,0 @@ -name: Release - -on: - push: - tags: - - 'v*' - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -env: - BROKER_URL: https://nr-broker.apps.silver.devops.gov.bc.ca - BROKER_JWT: ${{ secrets.BROKER_JWT }} - SERVICE_PROJECT: <%= projectName %> - SERVICE_NAME: <%= serviceName %> - -jobs: - release-build: - name: Publish release - runs-on: ubuntu-latest - outputs: - project_version: ${{ steps.set-build-output-parameters.outputs.project_version }} - intention_id: ${{ steps.set-intention-id.outputs.intention_id }} - steps: - - 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: Update pom.xml - id: update-repo - shell: bash - run: | - TAG=${{ github.ref_name }} - VERSION=${TAG#v} - echo "VERSION=${VERSION}" >> $GITHUB_ENV - mvn versions:set -DnewVersion=${VERSION} --file <%= pomRoot %>pom.xml - - name: Set build output parameters - id: set-build-output-parameters - run: | - echo "project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout --file <%= pomRoot %>pom.xml)" >> $GITHUB_OUTPUT - echo "artifact_id=$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout --file <%= pomRoot %>pom.xml)" >> $GITHUB_OUTPUT - echo "git_commit=$(git rev-parse --verify HEAD)" >> $GITHUB_OUTPUT - - name: Build intention - run: sh ./.github/workflows/build-intention.sh - env: - GITHUB_REPOSITORY: ${{ github.repository}} - GITHUB_RUN_ID: ${{ github.run_id}} - SERVICE_PROJECT: ${{ env.SERVICE_PROJECT }} - SERVICE_NAME: ${{ env.SERVICE_NAME }} - PROJECT_VERSION: ${{ steps.set-build-output-parameters.outputs.project_version }} - GIT_COMMIT: ${{ steps.set-build-output-parameters.outputs.git_commit }} - BUILD_NUMBER: ${{ github.run_number }} - - name: Open intention - uses: bcgov-nr/action-broker-intention-open@v2 - with: - broker_jwt: ${{ secrets.BROKER_JWT }} - intention_path: intention.json - quickstart: true - - name: Set intention ID for deployment job - id: set-intention-id - run: | - echo "intention_id=${INTENTION_ID}" >> $GITHUB_OUTPUT - env: - INTENTION_ID: ${{ env.INTENTION_ID }} - - name: Publish to OpenShift Artifactory - run: mvn --batch-mode -Dmaven.test.skip=true -Partifactory deploy --file <%= pomRoot %>pom.xml - env: - ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }} - ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }} - BUILD_NUMBER: ${{ github.run_number }} - BUILD_URL: ${{ github.repositoryUrl }} - - name: Set build info - id: set-build-info - run: | - BUILD_INFO=$(curl -s -u ${{ secrets.ARTIFACTORY_USERNAME }}:${{ secrets.ARTIFACTORY_PASSWORD }} \ - https://artifacts.developer.gov.bc.ca/artifactory/api/build/${{ env.ARTIFACT_ID }}/${{ github.run_number }}?project=cc20) - echo "vcs_revision=$(echo ${BUILD_INFO} | jq -r '.buildInfo.vcs[0].revision')" >> $GITHUB_OUTPUT - echo "vcs_message=$(echo ${BUILD_INFO} | jq -r '.buildInfo.vcs[0].message')" >> $GITHUB_OUTPUT - echo "vcs_branch=$(echo ${BUILD_INFO} | jq -r '.buildInfo.vcs[0].branch')" >> $GITHUB_OUTPUT - echo "vcs_url=$(echo ${BUILD_INFO} | jq -r '.buildInfo.vcs[0].url')" >> $GITHUB_OUTPUT - echo "artifact_type=$(echo ${BUILD_INFO} | jq -r '.buildInfo.modules[0].artifacts[0].type')" >> $GITHUB_OUTPUT - echo "artifact_sha1=$(echo ${BUILD_INFO} | jq -r '.buildInfo.modules[0].artifacts[0].sha1')" >> $GITHUB_OUTPUT - echo "artifact_sha256=$(echo ${BUILD_INFO} | jq -r '.buildInfo.modules[0].artifacts[0].sha256')" >> $GITHUB_OUTPUT - echo "artifact_md5=$(echo ${BUILD_INFO} | jq -r '.buildInfo.modules[0].artifacts[0].md5')" >> $GITHUB_OUTPUT - echo "artifact_name=$(echo ${BUILD_INFO} | jq -r '.buildInfo.modules[0].artifacts[0].name')" >> $GITHUB_OUTPUT - env: - ARTIFACT_ID: ${{ steps.set-build-output-parameters.outputs.artifact_id }} - - name: Send build info - run: | - curl -s POST ${{ env.BROKER_URL }}/v1/intention/action/artifact \ - -H 'Content-Type: application/json' \ - -H 'X-Broker-Token: '"${BUILD_TOKEN}"'' \ - -d '{"checksum": "sha256:'${ARTIFACT_SHA256}'", "name": "'${ARTIFACT_NAME}'", "type": "'${ARTIFACT_TYPE}'" }' - env: - ARTIFACT_SHA256: ${{ steps.set-build-info.outputs.artifact_sha256 }} - ARTIFACT_NAME: ${{ steps.set-build-info.outputs.artifact_name }} - ARTIFACT_TYPE: ${{ steps.set-build-info.outputs.artifact_type }} - BUILD_TOKEN: ${{ env.ACTION_TOKEN_BUILD }} - - name: Close intention - uses: bcgov-nr/action-broker-intention-close@v1 - with: - intention_token: ${{ env.INTENTION_TOKEN }}