From 99b2eb0ff4de186899a27bfc644a5dd02facfd74 Mon Sep 17 00:00:00 2001 From: Matthew Bystedt Date: Wed, 15 Jan 2025 16:52:20 -0800 Subject: [PATCH] fix: backstage default name --- README.md | 6 +- .../gh-nodejs-build/templates/deploy.yaml | 187 ++++++++++++++++++ generators/util/yaml.js | 2 +- 3 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 generators/gh-nodejs-build/templates/deploy.yaml diff --git a/README.md b/README.md index e8163fe..bef39bc 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ npm link First, open a terminal and change the current working directory to the root of the checked out repository that you wish to scaffold. It is recommended that you run the generators only on a clean repository. -The generators will output a file to save your answers and will update any `app-config.yaml` catalogue file. This is useful if you want to rerun the generator in the future to take advantage of any updated workflows. +The generators will output a file to save your answers and will update any `catalog-info.yaml` catalogue file. This is useful if you want to rerun the generator in the future to take advantage of any updated workflows. The example command will run the 'gh-maven-build' generator. This creates or updates the files for building and deploying a Maven (Java) application. @@ -63,7 +63,7 @@ All generators are built to be rerun multiple times. ### Backstage: backstage -The generator, backstage, generates a `app-config.yaml` catalogue file. It will prompt you for various information about your application. +The generator, backstage, generates a `catalog-info.yaml` catalogue file. It will prompt you for various information about your application. Other generators will ask to read from this file to skip prompts that ask for information stored in this file. @@ -83,7 +83,7 @@ The generated files will appear in your .github/workflows and .jenkins directori ### Skip prompts (--promptless) -The option `--promptless` can be used with a number of generators to attempt to run it without prompting for responses. It will attempt to only use information stored in your `app-config.yaml`. +The option `--promptless` can be used with a number of generators to attempt to run it without prompting for responses. It will attempt to only use information stored in your `catalog-info.yaml`. ### Force changes (--force) diff --git a/generators/gh-nodejs-build/templates/deploy.yaml b/generators/gh-nodejs-build/templates/deploy.yaml new file mode 100644 index 0000000..7d1ebfc --- /dev/null +++ b/generators/gh-nodejs-build/templates/deploy.yaml @@ -0,0 +1,187 @@ +name: Deploy + +on: + workflow_dispatch: + inputs: + config_branch: + description: 'Use branch for testing configuration changes' + required: false + type: string + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + SERVICE_PROJECT: <%= projectName %> + SERVICE_NAME: <%= serviceName %> + JOB: buildByToken/buildWithParameters?job=<%= projectName %>/<%= serviceName %> + URL: https://cd.io.nrs.gov.bc.ca + PACKAGE_REPO: https://maven.pkg.github.com/<%= gitHubOwnerPack %> +jobs: + check_token_expiration: + uses: ./.github/workflows/check-token.yaml + secrets: + token: ${{ secrets.<%= brokerJwt %> }} + deploy-build: + name: Deploy development version + if: ${{ ! startsWith(github.ref, 'refs/tags/') }} + runs-on: ubuntu-latest + needs: check_token_expiration + outputs: + 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 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" \ + -H 'accept: application/json' \ + -H 'Authorization: Bearer '"${BROKER_JWT}"'' \ + -d '') + if echo "$RESPONSE" | jq -e '.statusCode == 401' > /dev/null; then + echo "Status code is 401. This indicates an authorization error." + echo "Please check if your Broker Token is valid. Teams can generate a new one in Broker if needed." + echo "Link: https://broker.io.nrs.gov.bc.ca/browse/brokerAccount" + echo "Exiting..." + exit 1 + fi + 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://broker.io.nrs.gov.bc.ca + BROKER_JWT: ${{ secrets.<%= brokerJwt %> }} + 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 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/') }} + runs-on: ubuntu-latest + needs: check_token_expiration + outputs: + 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 tag output + id: set-tag-output + run: | + TAG=${{ github.ref_name }} + PROJECT_VERSION=${TAG#v} + 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.version%22%3A%22${PROJECT_VERSION}%22%7D&offset=0&limit=1" \ + -H 'accept: application/json' \ + -H 'Authorization: Bearer '"${BROKER_JWT}"'' \ + -d '') + if echo "$RESPONSE" | jq -e '.statusCode == 401' > /dev/null; then + echo "Status code is 401. This indicates an authorization error." + echo "Please check if your Broker Token is valid. Teams can generate a new one in Broker if needed." + echo "Link: https://broker.io.nrs.gov.bc.ca/browse/brokerAccount" + echo "Exiting..." + exit 1 + fi + 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://broker.io.nrs.gov.bc.ca + BROKER_JWT: ${{ secrets.<%= brokerJwt %> }} + 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 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-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 "artifactSha256=${ARTIFACT_SHA256}" \ + --data-urlencode "projectVersion=${PROJECT_VERSION}" \ + --data-urlencode "gitBranch=${{ github.ref_name }}" \ + --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}" \ + --data-urlencode "downloadType=GITHUB" \ + -H "Connection: close" \ + ${{ env.URL }}/${{ env.JOB }} + env: + 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_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 + # and download the package + - name: Sleep + run: sleep 90s + shell: bash diff --git a/generators/util/yaml.js b/generators/util/yaml.js index ca5fe09..bf8ab0c 100644 --- a/generators/util/yaml.js +++ b/generators/util/yaml.js @@ -1,4 +1,4 @@ -export const BACKSTAGE_FILENAME = 'app-config.yaml'; +export const BACKSTAGE_FILENAME = 'catalog-info.yaml'; export const pathToProps = [ { path: ['spec', 'system'], prop: 'projectName' },