Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.
10 changes: 9 additions & 1 deletion src/commands/project/create-variable-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
isExists as isBedrockFileExists,
read as readBedrockFile
} from "../../lib/bedrockYaml";
import { PROJECT_PIPELINE_FILENAME } from "../../lib/constants";
import {
PROJECT_PIPELINE_FILENAME,
VERSION_MESSAGE
} from "../../lib/constants";
import { AzureDevOpsOpts } from "../../lib/git";
import { createTempDir } from "../../lib/ioUtil";
import * as pipelineVariableGroup from "../../lib/pipelines/variableGroup";
Expand All @@ -30,6 +33,7 @@ import {
setVariableGroupInBedrockFile,
updateLifeCyclePipeline
} from "./create-variable-group";
import * as fileutils from "../../lib/fileutils";

beforeAll(() => {
enableVerboseLogging();
Expand All @@ -43,6 +47,10 @@ beforeEach(() => {
jest.clearAllMocks();
});

jest
.spyOn(fileutils, "getVersionMessage")
.mockReturnValue(VERSION_MESSAGE + "0.5");

const registryName = uuid();
const variableGroupName = uuid();
const hldRepoUrl = uuid();
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs";
import yaml from "js-yaml";
import * as os from "os";
import path from "path";
import { writeVersion } from "./lib/fileutils";
import { getSecret } from "./lib/azure/keyvault";
import { logger } from "./logger";
import {
Expand Down Expand Up @@ -287,7 +288,8 @@ export const write = (
throw new Error(`Pipeline yaml file name is undefined`);
}

return fs.writeFileSync(path.join(targetDirectory, fileName), asYaml);
writeVersion(path.join(targetDirectory, fileName));
return fs.appendFileSync(path.join(targetDirectory, fileName), asYaml);
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export const RENDER_HLD_PIPELINE_FILENAME = "manifest-generation.yaml";
export const SERVICE_PIPELINE_FILENAME = "build-update-hld.yaml";

export const VM_IMAGE = "ubuntu-latest";

export const VERSION_MESSAGE = "# GENERATED WITH SPK VERSION ";
31 changes: 31 additions & 0 deletions src/lib/fileutils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
generateHldLifecyclePipelineYaml,
generateServiceBuildAndUpdatePipelineYaml,
generateYamlScript,
getVersionMessage,
sanitizeTriggerPath,
serviceBuildAndUpdatePipeline,
updateTriggerBranchesForServiceBuildAndUpdatePipeline
Expand All @@ -60,6 +61,10 @@ beforeEach(() => {
jest.clearAllMocks();
});

jest.mock("../../package.json", () => {
return { version: "0.5" };
});

describe("generateAccessYaml", () => {
const targetDirectory = "hld-repository";
const serviceDirectory = "my-service";
Expand Down Expand Up @@ -158,6 +163,7 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => {
const targetDirectory = "app-repository";
const serviceDirectory = "my-service";
const writeSpy = jest.spyOn(fs, "writeFileSync");
const appendSpy = jest.spyOn(fs, "appendFileSync");

beforeEach(() => {
mockFs({
Expand Down Expand Up @@ -198,7 +204,13 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => {
path.join(targetDirectory, serviceDirectory),
[]
);

expect(writeSpy).toBeCalledWith(
expectedFilePath,
`${getVersionMessage()}\n`,
"utf8"
);
expect(appendSpy).toBeCalledWith(
expectedFilePath,
createTestServiceBuildAndUpdatePipelineYaml(
true,
Expand Down Expand Up @@ -246,6 +258,7 @@ describe("updateTriggerBranchesForServiceBuildAndUpdatePipeline", () => {
const targetDirectory = "app-repository";
const serviceDirectory = "my-service";
const writeSpy = jest.spyOn(fs, "writeFileSync");
const appendSpy = jest.spyOn(fs, "appendFileSync");

beforeEach(() => {
mockFs({
Expand Down Expand Up @@ -305,6 +318,11 @@ describe("updateTriggerBranchesForServiceBuildAndUpdatePipeline", () => {
);

expect(writeSpy).toBeCalledWith(
expectedFilePath,
`${getVersionMessage()}\n`,
"utf8"
);
expect(appendSpy).toBeCalledWith(
expectedFilePath,
createTestServiceBuildAndUpdatePipelineYaml(
true,
Expand All @@ -322,6 +340,7 @@ describe("updateTriggerBranchesForServiceBuildAndUpdatePipeline", () => {
describe("generateHldLifecyclePipelineYaml", () => {
const targetDirectory = "app-repository";
const writeSpy = jest.spyOn(fs, "writeFileSync");
const appendSpy = jest.spyOn(fs, "appendFileSync");

beforeEach(() => {
mockFs({
Expand All @@ -348,6 +367,11 @@ describe("generateHldLifecyclePipelineYaml", () => {

generateHldLifecyclePipelineYaml(targetDirectory);
expect(writeSpy).toBeCalledWith(
expectedFilePath,
`${getVersionMessage()}\n`,
"utf8"
);
expect(appendSpy).toBeCalledWith(
expectedFilePath,
createTestHldLifecyclePipelineYaml(),
"utf8"
Expand All @@ -359,6 +383,8 @@ describe("generateHldLifecyclePipelineYaml", () => {
describe("generateHldAzurePipelinesYaml", () => {
const targetDirectory = "hld-repository";
const writeSpy = jest.spyOn(fs, "writeFileSync");
const appendSpy = jest.spyOn(fs, "appendFileSync");

beforeEach(() => {
mockFs({
"hld-repository": {}
Expand All @@ -384,6 +410,11 @@ describe("generateHldAzurePipelinesYaml", () => {

generateHldAzurePipelinesYaml(targetDirectory);
expect(writeSpy).toBeCalledWith(
expectedFilePath,
`${getVersionMessage()}\n`,
"utf8"
);
expect(appendSpy).toBeCalledWith(
expectedFilePath,
createTestHldAzurePipelinesYaml(),
"utf8"
Expand Down
30 changes: 26 additions & 4 deletions src/lib/fileutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PROJECT_PIPELINE_FILENAME,
RENDER_HLD_PIPELINE_FILENAME,
SERVICE_PIPELINE_FILENAME,
VERSION_MESSAGE,
VM_IMAGE
} from "../lib/constants";
import { logger } from "../logger";
Expand Down Expand Up @@ -347,6 +348,21 @@ export const serviceBuildAndUpdatePipeline = (
return pipelineYaml;
};

/**
* Gets the spk version message
*/
export const getVersionMessage = (): string => {
return VERSION_MESSAGE + require("../../package.json").version;
};

/**
* Writes the spk version to the given file
* @param filePath The path to the file
*/
export const writeVersion = (filePath: string): void => {
fs.writeFileSync(filePath, `${getVersionMessage()}\n`, "utf8");
};

/**
* Creates the service multistage build and update image tag pipeline.
* One pipeline should exist for each service.
Expand Down Expand Up @@ -393,7 +409,9 @@ export const generateServiceBuildAndUpdatePipelineYaml = (
ringBranches,
variableGroups
);
fs.writeFileSync(

writeVersion(pipelineYamlFullPath);
fs.appendFileSync(
pipelineYamlFullPath,
yaml.safeDump(buildYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
"utf8"
Expand Down Expand Up @@ -437,7 +455,8 @@ export const updateTriggerBranchesForServiceBuildAndUpdatePipeline = (
buildPipelineYaml.trigger.branches.include = ringBranches;
}

fs.writeFileSync(
writeVersion(pipelineYamlFullPath);
fs.appendFileSync(
pipelineYamlFullPath,
yaml.safeDump(buildPipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
"utf8"
Expand Down Expand Up @@ -586,7 +605,8 @@ export const generateHldAzurePipelinesYaml = (
`Generated ${RENDER_HLD_PIPELINE_FILENAME}. Commit and push this file to master before attempting to deploy via the command 'spk hld install-manifest-pipeline'; before running the pipeline ensure the following environment variables are available to your pipeline: ${requiredPipelineVariables}`
);

fs.writeFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
writeVersion(azurePipelinesYamlPath);
fs.appendFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
};

/**
Expand Down Expand Up @@ -773,7 +793,9 @@ export const generateHldLifecyclePipelineYaml = async (
logger.info(
`Writing ${PROJECT_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}`
);
fs.writeFileSync(azurePipelinesYamlPath, lifecycleYaml, "utf8");

writeVersion(azurePipelinesYamlPath);
fs.appendFileSync(azurePipelinesYamlPath, lifecycleYaml, "utf8");

const requiredPipelineVariables = [
`'HLD_REPO' (Repository for your HLD in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/hld')`,
Expand Down