Skip to content

Commit

Permalink
Merge pull request #37 from bcgov-nr/feat/addGHDeploy
Browse files Browse the repository at this point in the history
Feat/add gh deploy
  • Loading branch information
GraceRuan authored Mar 27, 2024
2 parents 66a9a59 + c8949cb commit 61c5f8b
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 28 deletions.
45 changes: 34 additions & 11 deletions generators/gh-maven-build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -98,17 +91,40 @@ export default class extends Generator {
default: false,
store: true,
},
{
type: 'input',
name: 'gitHubOwnerPack',
message: 'GitHub Owner with repo path (eg. 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',
message: 'Deploy on-prem?',
default: false,
store: true,
},
];
]);

const props = await this.prompt(prompts);
this.props = props;
this.props = answers;
}

// Generate GitHub workflows and NR Broker intention files
Expand All @@ -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(
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion generators/gh-maven-build/templates/build-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
131 changes: 115 additions & 16 deletions generators/gh-maven-build/templates/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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" \
Expand All @@ -34,12 +47,59 @@ 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
<% if (gitHubPackages) { -%>
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
<% } 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 }}
PACKAGE_REPO: ${{ env.PACKAGE_REPO }}
deploy-tag:
name: Deploy release version
if: ${{ startsWith(github.ref, 'refs/tags/') }}
Expand All @@ -48,8 +108,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 }}
Expand All @@ -62,36 +125,72 @@ 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
run: |
curl \
--data-urlencode "token=${{ secrets.JENKINS_TOKEN }}" \
--data-urlencode "githubToken=${{ secrets.GITHUB_TOKEN }}" \
--data-urlencode "artifactoryBuildNumber=${{ env.BUILD_NUMBER }}" \
--data-urlencode "cdVersion=${{ env.PROJECT_VERSION }}" \
--data-urlencode "artifactName=${ARTIFACT_NAME}" \
--data-urlencode "artifactSha256=${ARTIFACT_SHA256}" \
--data-urlencode "projectVersion=${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}" \
-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
Expand Down

0 comments on commit 61c5f8b

Please sign in to comment.