From 0f05aca6cf751cf664567d04dba860f9f21ef05e Mon Sep 17 00:00:00 2001 From: GraceRuan Date: Thu, 21 Mar 2024 16:33:37 -0700 Subject: [PATCH 1/5] feat: add artifact and GH for deploy --- generators/gh-maven-build/index.js | 45 +++++-- .../gh-maven-build/templates/deploy.yaml | 115 ++++++++++++++++-- 2 files changed, 136 insertions(+), 24 deletions(-) diff --git a/generators/gh-maven-build/index.js b/generators/gh-maven-build/index.js index 61fe7e1..705d22a 100644 --- a/generators/gh-maven-build/index.js +++ b/generators/gh-maven-build/index.js @@ -57,7 +57,7 @@ export default class extends Generator { this.log(chalk.bold('Prompts')); this.log(''); - const prompts = [ + const answers = await this.prompt([ { type: 'input', name: 'projectName', @@ -70,13 +70,6 @@ export default class extends Generator { message: 'Service:', store: true, }, - { - type: 'input', - name: 'artifactoryProject', - message: 'Artifactory:', - default: 'cc20', - store: true, - }, { type: 'input', name: 'pomRoot', @@ -98,6 +91,30 @@ export default class extends Generator { default: false, store: true, }, + { + type: 'input', + name: 'gitHubOwnerPack', + message: 'GitHub Owner with repo path (bcgov-nr-labs/edqa-war):', + default: 'bcgov-nr-labs/edqa-war', + store: true, + when: (answers) => answers.gitHubPackages, + }, + { + type: 'input', + name: 'artifactoryProject', + message: 'Artifactory:', + default: 'cc20', + store: true, + when: (answers) => !answers.gitHubPackages, + }, + { + type: 'input', + name: 'artifactoryPackageType', + message: 'Artifactory Package Type (maven, ivy, npm):', + default: 'maven', + store: true, + when: (answers) => !answers.gitHubPackages, + }, { type: 'confirm', name: 'deployOnPrem', @@ -105,10 +122,9 @@ export default class extends Generator { default: false, store: true, }, - ]; + ]); - const props = await this.prompt(prompts); - this.props = props; + this.props = answers; } // Generate GitHub workflows and NR Broker intention files @@ -123,6 +139,8 @@ export default class extends Generator { pomRoot: this.props.pomRoot, unitTestsPath: this.props.unitTestsPath, gitHubPackages: this.props.gitHubPackages, + artifactoryPackageType: this.props.artifactoryPackageType, + gitHubOwnerPack: this.props.gitHubOwnerPack, }, ); this.fs.copyTpl( @@ -144,6 +162,11 @@ export default class extends Generator { { projectName: this.props.projectName, serviceName: this.props.serviceName, + artifactoryProject: this.props.artifactoryProject, + pomRoot: this.props.pomRoot, + gitHubPackages: this.props.gitHubPackages, + artifactoryPackageType: this.props.artifactoryPackageType, + gitHubOwnerPack: this.props.gitHubOwnerPack, }, ); this.fs.copyTpl( diff --git a/generators/gh-maven-build/templates/deploy.yaml b/generators/gh-maven-build/templates/deploy.yaml index 40cadb8..dd00534 100644 --- a/generators/gh-maven-build/templates/deploy.yaml +++ b/generators/gh-maven-build/templates/deploy.yaml @@ -2,6 +2,11 @@ name: Deploy on: workflow_dispatch: + inputs: + config_branch: + description: 'A branch used for testing configuration changes' + required: false + type: string concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -12,19 +17,27 @@ env: SERVICE_NAME: <%= serviceName %> JOB: buildByToken/buildWithParameters?job=<%= projectName %>/<%= serviceName %> URL: https://cd.io.nrs.gov.bc.ca +<% if (gitHubPackages) { -%> + PACKAGE_REPO: https://maven.pkg.github.com/<%= gitHubOwnerPack %> +<% } else { -%> + PACKAGE_REPO: https://artifacts.developer.gov.bc.ca/artifactory/<%= artifactoryProject %>-<%= projectName %>-<%= artifactoryPackageType %>-local +<% } -%> jobs: - deploy-snapshot: + deploy-build: name: Deploy development version if: ${{ ! startsWith(github.ref, 'refs/tags/') }} runs-on: ubuntu-latest outputs: - project_version: ${{ steps.set-snapshot-output.outputs.project_version }} - build_guid: ${{ steps.set-snapshot-output.outputs.build_guid }} - build_number: ${{ steps.set-snapshot-output.outputs.build_number }} + 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 number and build guid (intention) - id: set-snapshot-output + - 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" \ @@ -34,12 +47,46 @@ jobs: 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://nr-broker.apps.silver.devops.gov.bc.ca BROKER_JWT: ${{ secrets.BROKER_JWT }} 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 <%= pomRoot %>pom.xml) + GROUP_ID_PATH="${GROUP_ID//.//}" + PACKAGE_TYPE=$(mvn help:evaluate -Dexpression=project.packaging -q -DforceStdout --file <%= pomRoot %>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/') }} @@ -48,8 +95,11 @@ jobs: 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 build number and build guid (intention) + - name: Set tag output id: set-tag-output run: | TAG=${{ github.ref_name }} @@ -62,18 +112,48 @@ jobs: 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://nr-broker.apps.silver.devops.gov.bc.ca BROKER_JWT: ${{ secrets.BROKER_JWT }} 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 <%= pomRoot %>pom.xml) + GROUP_ID_PATH="${GROUP_ID//.//}" + PACKAGE_TYPE=$(mvn help:evaluate -Dexpression=project.packaging -q -DforceStdout --file <%= pomRoot %>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-snapshot.result == 'success' && needs.deploy-tag.result == 'skipped') || - (needs.deploy-snapshot.result == 'skipped' && needs.deploy-tag.result == 'success')) - needs: [deploy-snapshot, deploy-tag] + ((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 @@ -82,16 +162,25 @@ jobs: --data-urlencode "token=${{ secrets.JENKINS_TOKEN }}" \ --data-urlencode "githubToken=${{ secrets.GITHUB_TOKEN }}" \ --data-urlencode "artifactoryBuildNumber=${{ env.BUILD_NUMBER }}" \ + --data-urlencode "artifactName=${ARTIFACT_NAME}" \ + --data-urlencode "artifactSha256=${ARTIFACT_SHA256}" \ + --data-urlencode "projectVersion=${PROJECT_VERSION}" \ + --data-urlencode "gitBranch=${{ github.ref_name }}" \ --data-urlencode "cdVersion=${{ env.PROJECT_VERSION }}" \ --data-urlencode "gitBranch=${{ github.ref_name }}" \ --data-urlencode "intentionId=${{ env.BUILD_GUID }}" \ --data-urlencode "gitTag=${{ (startsWith(github.ref, 'refs/tags/') && github.ref_name) || '' }}" \ + --data-urlencode "configBranch=${{ inputs.config_branch || '' }}" \ + --data-urlencode "downloadUrl=${DOWNLOAD_URL}" \ -H "Connection: close" \ ${{ env.URL }}/${{ env.JOB }} env: - PROJECT_VERSION: ${{ needs.deploy-snapshot.outputs.project_version || needs.deploy-tag.outputs.project_version }} - BUILD_GUID: ${{ needs.deploy-snapshot.outputs.build_guid || needs.deploy-tag.outputs.build_guid }} - BUILD_NUMBER: ${{ needs.deploy-snapshot.outputs.build_number || needs.deploy-tag.outputs.build_number }} + 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_NAME: ${{ needs.deploy-build.outputs.artifact_name || needs.deploy-tag.outputs.artifact_name }} + 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. - name: Sleep for 30 seconds run: sleep 30s From a83812c57d9d66dee328cdda2e2227e241014f6b Mon Sep 17 00:00:00 2001 From: GraceRuan Date: Thu, 21 Mar 2024 16:52:08 -0700 Subject: [PATCH 2/5] fix: remove unnecessary params in Jenkins --- generators/gh-maven-build/templates/deploy.yaml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/generators/gh-maven-build/templates/deploy.yaml b/generators/gh-maven-build/templates/deploy.yaml index dd00534..48f6524 100644 --- a/generators/gh-maven-build/templates/deploy.yaml +++ b/generators/gh-maven-build/templates/deploy.yaml @@ -161,14 +161,11 @@ jobs: curl \ --data-urlencode "token=${{ secrets.JENKINS_TOKEN }}" \ --data-urlencode "githubToken=${{ secrets.GITHUB_TOKEN }}" \ - --data-urlencode "artifactoryBuildNumber=${{ env.BUILD_NUMBER }}" \ --data-urlencode "artifactName=${ARTIFACT_NAME}" \ --data-urlencode "artifactSha256=${ARTIFACT_SHA256}" \ --data-urlencode "projectVersion=${PROJECT_VERSION}" \ --data-urlencode "gitBranch=${{ github.ref_name }}" \ - --data-urlencode "cdVersion=${{ env.PROJECT_VERSION }}" \ - --data-urlencode "gitBranch=${{ github.ref_name }}" \ - --data-urlencode "intentionId=${{ env.BUILD_GUID }}" \ + --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}" \ From 644275812d36fb275104c41bc9159e715ad5e05d Mon Sep 17 00:00:00 2001 From: GraceRuan Date: Tue, 26 Mar 2024 14:06:57 -0700 Subject: [PATCH 3/5] feat: add GH and artifactory deploy --- generators/gh-maven-build/templates/deploy.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/generators/gh-maven-build/templates/deploy.yaml b/generators/gh-maven-build/templates/deploy.yaml index 48f6524..af5f554 100644 --- a/generators/gh-maven-build/templates/deploy.yaml +++ b/generators/gh-maven-build/templates/deploy.yaml @@ -70,6 +70,7 @@ jobs: cache: maven - name: Set download URL id: set-download-url +<% if (gitHubPackages) { -%> run: | sudo apt-get -qq install libxml2-utils GROUP_ID=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout --file <%= pomRoot %>pom.xml) @@ -83,6 +84,18 @@ jobs: DOWNLOAD_URL="${PACKAGE_REPO}/${GROUP_ID_PATH}/${SERVICE_NAME}/${PROJECT_VERSION}/${ARTIFACT_NAME}" echo "${DOWNLOAD_URL}" echo "download_url=$(echo ${DOWNLOAD_URL})" >> $GITHUB_OUTPUT +<% } else { -%> + run: | + sudo apt-get -qq install libxml2-utils + GROUP_ID=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout --file <%= pomRoot %>pom.xml) + GROUP_ID_PATH="${GROUP_ID//.//}" + PACKAGE_TYPE=$(mvn help:evaluate -Dexpression=project.packaging -q -DforceStdout --file <%= pomRoot %>pom.xml) + SNAPSHOT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout --file <%= pomRoot %>pom.xml) + 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 }} From f34a7473ede5f212d5ca7027f27102e9185abb6f Mon Sep 17 00:00:00 2001 From: GraceRuan Date: Tue, 26 Mar 2024 14:22:12 -0700 Subject: [PATCH 4/5] feat: modify format for GitHub repo path --- generators/gh-maven-build/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/gh-maven-build/index.js b/generators/gh-maven-build/index.js index 705d22a..7c7ba7d 100644 --- a/generators/gh-maven-build/index.js +++ b/generators/gh-maven-build/index.js @@ -94,7 +94,7 @@ export default class extends Generator { { type: 'input', name: 'gitHubOwnerPack', - message: 'GitHub Owner with repo path (bcgov-nr-labs/edqa-war):', + message: 'GitHub Owner with repo path (eg. bcgov-nr-labs/edqa-war):', default: 'bcgov-nr-labs/edqa-war', store: true, when: (answers) => answers.gitHubPackages, From c8949cbfe767857663bbfe672d69e227dcc09ca3 Mon Sep 17 00:00:00 2001 From: Andreas Wilson <41663792+andrwils@users.noreply.github.com> Date: Tue, 26 Mar 2024 17:25:05 -0700 Subject: [PATCH 5/5] feat: set target path --- generators/gh-maven-build/templates/build-release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/gh-maven-build/templates/build-release.yaml b/generators/gh-maven-build/templates/build-release.yaml index ee59353..f37aa8e 100644 --- a/generators/gh-maven-build/templates/build-release.yaml +++ b/generators/gh-maven-build/templates/build-release.yaml @@ -108,7 +108,7 @@ jobs: 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 + echo "artifact_sha256=$(sha256sum <%= pomRoot %>target/${ARTIFACT_NAME} | awk '{ print $1 }')" >> $GITHUB_OUTPUT env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PROJECT_VERSION: ${{ steps.set-build-output-parameters.outputs.project_version }}