|
| 1 | +import fs from "fs"; |
| 2 | +import yaml from "js-yaml"; |
| 3 | +import path from "path"; |
| 4 | +import commander from "commander"; |
| 5 | + |
| 6 | +import { |
| 7 | + generateYamlScript, |
| 8 | + SAFE_SOURCE_BRANCH, |
| 9 | + IMAGE_TAG, |
| 10 | + BUILD_REPO_NAME, |
| 11 | + IMAGE_REPO, |
| 12 | +} from "../src/lib/fileutils"; |
| 13 | +import { assertIsStringWithContent } from "../src/lib/assertions"; |
| 14 | +import { AzurePipelinesYaml } from "../src/types"; |
| 15 | +import { VM_IMAGE } from "../src/lib/constants"; |
| 16 | + |
| 17 | +const updateStageYaml = (serviceName: string): string => { |
| 18 | + return generateYamlScript([ |
| 19 | + `export SERVICE_NAME_LOWER=$(echo ${serviceName} | tr '[:upper:]' '[:lower:]')`, |
| 20 | + `export BUILD_REPO_NAME=${BUILD_REPO_NAME(serviceName)}`, |
| 21 | + `export BRANCH_NAME=DEPLOY/$BUILD_REPO_NAME-${IMAGE_TAG}`, |
| 22 | + `export FAB_SAFE_SERVICE_NAME=$(echo $SERVICE_NAME_LOWER | tr . - | tr / -)`, |
| 23 | + `# --- From https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/release.sh`, |
| 24 | + `. build.sh --source-only`, |
| 25 | + ``, |
| 26 | + `# Initialization`, |
| 27 | + `verify_access_token`, |
| 28 | + `init`, |
| 29 | + `helm_init`, |
| 30 | + ``, |
| 31 | + `# Fabrikate`, |
| 32 | + `get_fab_version`, |
| 33 | + `download_fab`, |
| 34 | + ``, |
| 35 | + `# Clone HLD repo`, |
| 36 | + `git_connect`, |
| 37 | + `# --- End Script`, |
| 38 | + ``, |
| 39 | + `# Update HLD`, |
| 40 | + `git checkout -b "$BRANCH_NAME"`, |
| 41 | + `export BUILD_REPO_NAME=${BUILD_REPO_NAME(serviceName)}`, |
| 42 | + `export IMAGE_TAG=${IMAGE_TAG}`, |
| 43 | + `export IMAGE_NAME=$BUILD_REPO_NAME:$IMAGE_TAG`, |
| 44 | + `echo "Image Name: $IMAGE_NAME"`, |
| 45 | + `export IMAGE_REPO=${IMAGE_REPO}`, |
| 46 | + `echo "Image Repository: $IMAGE_REPO"`, |
| 47 | + `cd $(Build.Repository.Name)/$FAB_SAFE_SERVICE_NAME/${SAFE_SOURCE_BRANCH}`, |
| 48 | + `echo "FAB SET"`, |
| 49 | + `fab set --subcomponent chart image.tag=$IMAGE_TAG image.repository=$IMAGE_REPO/$BUILD_REPO_NAME`, |
| 50 | + ]); |
| 51 | +}; |
| 52 | + |
| 53 | +const inject = (serviceName: string, pipelineFilePath: string): void => { |
| 54 | + const absPath = path.resolve(pipelineFilePath); |
| 55 | + const yamlStageContent = updateStageYaml(serviceName); |
| 56 | + |
| 57 | + const pipelineYaml: AzurePipelinesYaml = yaml.safeLoad( |
| 58 | + fs.readFileSync(absPath, "utf8") |
| 59 | + ); |
| 60 | + |
| 61 | + pipelineYaml.stages?.push({ |
| 62 | + stage: "hld_update", |
| 63 | + jobs: [ |
| 64 | + { |
| 65 | + job: "update_image_tag", |
| 66 | + pool: { |
| 67 | + vmImage: VM_IMAGE, |
| 68 | + }, |
| 69 | + steps: [ |
| 70 | + { |
| 71 | + script: yamlStageContent, |
| 72 | + displayName: "HLD Update Stage", |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }); |
| 78 | + |
| 79 | + fs.writeFileSync( |
| 80 | + absPath, |
| 81 | + yaml.safeDump(pipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER }), |
| 82 | + "utf8" |
| 83 | + ); |
| 84 | +}; |
| 85 | + |
| 86 | +commander |
| 87 | + .command("inject <serviceName> <pipelineFilePath>") |
| 88 | + .description( |
| 89 | + "This script injects the HLD update stage an existing azure devops build pipeline." |
| 90 | + ) |
| 91 | + .usage("inject myServiceName /path/to/pipeline/file.yaml") |
| 92 | + .action((serviceName, pipelineFilePath, cmdObj) => { |
| 93 | + console.log( |
| 94 | + `Injecting HLD Update stage for service ${serviceName} with build pipeline path at ${pipelineFilePath}` |
| 95 | + ); |
| 96 | + |
| 97 | + assertIsStringWithContent(serviceName); |
| 98 | + assertIsStringWithContent(pipelineFilePath); |
| 99 | + |
| 100 | + inject(serviceName, pipelineFilePath); |
| 101 | + }); |
| 102 | + |
| 103 | +commander.parse(process.argv); |
0 commit comments