This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import fs from "fs"; | ||
import yaml from "js-yaml"; | ||
import path from "path"; | ||
import commander from "commander"; | ||
|
||
import { | ||
generateYamlScript, | ||
SAFE_SOURCE_BRANCH, | ||
IMAGE_TAG, | ||
BUILD_REPO_NAME, | ||
IMAGE_REPO, | ||
} from "../src/lib/fileutils"; | ||
import { assertIsStringWithContent } from "../src/lib/assertions"; | ||
import { AzurePipelinesYaml } from "../src/types"; | ||
import { VM_IMAGE } from "../src/lib/constants"; | ||
|
||
const updateStageYaml = (serviceName: string): string => { | ||
return generateYamlScript([ | ||
`export SERVICE_NAME_LOWER=$(echo ${serviceName} | tr '[:upper:]' '[:lower:]')`, | ||
`export BUILD_REPO_NAME=${BUILD_REPO_NAME(serviceName)}`, | ||
`export BRANCH_NAME=DEPLOY/$BUILD_REPO_NAME-${IMAGE_TAG}`, | ||
`export FAB_SAFE_SERVICE_NAME=$(echo $SERVICE_NAME_LOWER | tr . - | tr / -)`, | ||
`# --- From https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/release.sh`, | ||
`. build.sh --source-only`, | ||
``, | ||
`# Initialization`, | ||
`verify_access_token`, | ||
`init`, | ||
`helm_init`, | ||
``, | ||
`# Fabrikate`, | ||
`get_fab_version`, | ||
`download_fab`, | ||
``, | ||
`# Clone HLD repo`, | ||
`git_connect`, | ||
`# --- End Script`, | ||
``, | ||
`# Update HLD`, | ||
`git checkout -b "$BRANCH_NAME"`, | ||
`export BUILD_REPO_NAME=${BUILD_REPO_NAME(serviceName)}`, | ||
`export IMAGE_TAG=${IMAGE_TAG}`, | ||
`export IMAGE_NAME=$BUILD_REPO_NAME:$IMAGE_TAG`, | ||
`echo "Image Name: $IMAGE_NAME"`, | ||
`export IMAGE_REPO=${IMAGE_REPO}`, | ||
`echo "Image Repository: $IMAGE_REPO"`, | ||
`cd $(Build.Repository.Name)/$FAB_SAFE_SERVICE_NAME/${SAFE_SOURCE_BRANCH}`, | ||
`echo "FAB SET"`, | ||
`fab set --subcomponent chart image.tag=$IMAGE_TAG image.repository=$IMAGE_REPO/$BUILD_REPO_NAME`, | ||
]); | ||
}; | ||
|
||
const inject = (serviceName: string, pipelineFilePath: string): void => { | ||
const absPath = path.resolve(pipelineFilePath); | ||
const yamlStageContent = updateStageYaml(serviceName); | ||
|
||
const pipelineYaml: AzurePipelinesYaml = yaml.safeLoad( | ||
fs.readFileSync(absPath, "utf8") | ||
); | ||
|
||
pipelineYaml.stages?.push({ | ||
stage: "hld_update", | ||
jobs: [ | ||
{ | ||
job: "update_image_tag", | ||
pool: { | ||
vmImage: VM_IMAGE, | ||
}, | ||
steps: [ | ||
{ | ||
script: yamlStageContent, | ||
displayName: "HLD Update Stage", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
fs.writeFileSync( | ||
absPath, | ||
yaml.safeDump(pipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER }), | ||
"utf8" | ||
); | ||
}; | ||
|
||
commander | ||
.command("inject <serviceName> <pipelineFilePath>") | ||
.description( | ||
"This script injects the HLD update stage an existing azure devops build pipeline." | ||
) | ||
.usage("inject myServiceName /path/to/pipeline/file.yaml") | ||
.action((serviceName, pipelineFilePath, cmdObj) => { | ||
console.log( | ||
`Injecting HLD Update stage for service ${serviceName} with build pipeline path at ${pipelineFilePath}` | ||
); | ||
|
||
assertIsStringWithContent(serviceName); | ||
assertIsStringWithContent(pipelineFilePath); | ||
|
||
inject(serviceName, pipelineFilePath); | ||
}); | ||
|
||
commander.parse(process.argv); |