Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit e6eba57

Browse files
authored
SPK generated pipelines should log the version of SPK (#411)
1 parent 078cbc6 commit e6eba57

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

src/commands/project/create-variable-group.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
isExists as isBedrockFileExists,
1111
read as readBedrockFile
1212
} from "../../lib/bedrockYaml";
13-
import { PROJECT_PIPELINE_FILENAME } from "../../lib/constants";
13+
import {
14+
PROJECT_PIPELINE_FILENAME,
15+
VERSION_MESSAGE
16+
} from "../../lib/constants";
1417
import { AzureDevOpsOpts } from "../../lib/git";
1518
import { createTempDir } from "../../lib/ioUtil";
1619
import * as pipelineVariableGroup from "../../lib/pipelines/variableGroup";
@@ -30,6 +33,7 @@ import {
3033
setVariableGroupInBedrockFile,
3134
updateLifeCyclePipeline
3235
} from "./create-variable-group";
36+
import * as fileutils from "../../lib/fileutils";
3337

3438
beforeAll(() => {
3539
enableVerboseLogging();
@@ -43,6 +47,10 @@ beforeEach(() => {
4347
jest.clearAllMocks();
4448
});
4549

50+
jest
51+
.spyOn(fileutils, "getVersionMessage")
52+
.mockReturnValue(VERSION_MESSAGE + "0.5");
53+
4654
const registryName = uuid();
4755
const variableGroupName = uuid();
4856
const hldRepoUrl = uuid();

src/config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "fs";
33
import yaml from "js-yaml";
44
import * as os from "os";
55
import path from "path";
6+
import { writeVersion } from "./lib/fileutils";
67
import { getSecret } from "./lib/azure/keyvault";
78
import { logger } from "./logger";
89
import {
@@ -287,7 +288,8 @@ export const write = (
287288
throw new Error(`Pipeline yaml file name is undefined`);
288289
}
289290

290-
return fs.writeFileSync(path.join(targetDirectory, fileName), asYaml);
291+
writeVersion(path.join(targetDirectory, fileName));
292+
return fs.appendFileSync(path.join(targetDirectory, fileName), asYaml);
291293
}
292294
};
293295

src/lib/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export const RENDER_HLD_PIPELINE_FILENAME = "manifest-generation.yaml";
2323
export const SERVICE_PIPELINE_FILENAME = "build-update-hld.yaml";
2424

2525
export const VM_IMAGE = "ubuntu-latest";
26+
27+
export const VERSION_MESSAGE = "# GENERATED WITH SPK VERSION ";

src/lib/fileutils.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
generateHldLifecyclePipelineYaml,
4444
generateServiceBuildAndUpdatePipelineYaml,
4545
generateYamlScript,
46+
getVersionMessage,
4647
sanitizeTriggerPath,
4748
serviceBuildAndUpdatePipeline,
4849
updateTriggerBranchesForServiceBuildAndUpdatePipeline
@@ -60,6 +61,10 @@ beforeEach(() => {
6061
jest.clearAllMocks();
6162
});
6263

64+
jest.mock("../../package.json", () => {
65+
return { version: "0.5" };
66+
});
67+
6368
describe("generateAccessYaml", () => {
6469
const targetDirectory = "hld-repository";
6570
const serviceDirectory = "my-service";
@@ -158,6 +163,7 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => {
158163
const targetDirectory = "app-repository";
159164
const serviceDirectory = "my-service";
160165
const writeSpy = jest.spyOn(fs, "writeFileSync");
166+
const appendSpy = jest.spyOn(fs, "appendFileSync");
161167

162168
beforeEach(() => {
163169
mockFs({
@@ -198,7 +204,13 @@ describe("generateServiceBuildAndUpdatePipelineYaml", () => {
198204
path.join(targetDirectory, serviceDirectory),
199205
[]
200206
);
207+
201208
expect(writeSpy).toBeCalledWith(
209+
expectedFilePath,
210+
`${getVersionMessage()}\n`,
211+
"utf8"
212+
);
213+
expect(appendSpy).toBeCalledWith(
202214
expectedFilePath,
203215
createTestServiceBuildAndUpdatePipelineYaml(
204216
true,
@@ -246,6 +258,7 @@ describe("updateTriggerBranchesForServiceBuildAndUpdatePipeline", () => {
246258
const targetDirectory = "app-repository";
247259
const serviceDirectory = "my-service";
248260
const writeSpy = jest.spyOn(fs, "writeFileSync");
261+
const appendSpy = jest.spyOn(fs, "appendFileSync");
249262

250263
beforeEach(() => {
251264
mockFs({
@@ -305,6 +318,11 @@ describe("updateTriggerBranchesForServiceBuildAndUpdatePipeline", () => {
305318
);
306319

307320
expect(writeSpy).toBeCalledWith(
321+
expectedFilePath,
322+
`${getVersionMessage()}\n`,
323+
"utf8"
324+
);
325+
expect(appendSpy).toBeCalledWith(
308326
expectedFilePath,
309327
createTestServiceBuildAndUpdatePipelineYaml(
310328
true,
@@ -322,6 +340,7 @@ describe("updateTriggerBranchesForServiceBuildAndUpdatePipeline", () => {
322340
describe("generateHldLifecyclePipelineYaml", () => {
323341
const targetDirectory = "app-repository";
324342
const writeSpy = jest.spyOn(fs, "writeFileSync");
343+
const appendSpy = jest.spyOn(fs, "appendFileSync");
325344

326345
beforeEach(() => {
327346
mockFs({
@@ -348,6 +367,11 @@ describe("generateHldLifecyclePipelineYaml", () => {
348367

349368
generateHldLifecyclePipelineYaml(targetDirectory);
350369
expect(writeSpy).toBeCalledWith(
370+
expectedFilePath,
371+
`${getVersionMessage()}\n`,
372+
"utf8"
373+
);
374+
expect(appendSpy).toBeCalledWith(
351375
expectedFilePath,
352376
createTestHldLifecyclePipelineYaml(),
353377
"utf8"
@@ -359,6 +383,8 @@ describe("generateHldLifecyclePipelineYaml", () => {
359383
describe("generateHldAzurePipelinesYaml", () => {
360384
const targetDirectory = "hld-repository";
361385
const writeSpy = jest.spyOn(fs, "writeFileSync");
386+
const appendSpy = jest.spyOn(fs, "appendFileSync");
387+
362388
beforeEach(() => {
363389
mockFs({
364390
"hld-repository": {}
@@ -384,6 +410,11 @@ describe("generateHldAzurePipelinesYaml", () => {
384410

385411
generateHldAzurePipelinesYaml(targetDirectory);
386412
expect(writeSpy).toBeCalledWith(
413+
expectedFilePath,
414+
`${getVersionMessage()}\n`,
415+
"utf8"
416+
);
417+
expect(appendSpy).toBeCalledWith(
387418
expectedFilePath,
388419
createTestHldAzurePipelinesYaml(),
389420
"utf8"

src/lib/fileutils.ts

+26-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PROJECT_PIPELINE_FILENAME,
88
RENDER_HLD_PIPELINE_FILENAME,
99
SERVICE_PIPELINE_FILENAME,
10+
VERSION_MESSAGE,
1011
VM_IMAGE
1112
} from "../lib/constants";
1213
import { logger } from "../logger";
@@ -347,6 +348,21 @@ export const serviceBuildAndUpdatePipeline = (
347348
return pipelineYaml;
348349
};
349350

351+
/**
352+
* Gets the spk version message
353+
*/
354+
export const getVersionMessage = (): string => {
355+
return VERSION_MESSAGE + require("../../package.json").version;
356+
};
357+
358+
/**
359+
* Writes the spk version to the given file
360+
* @param filePath The path to the file
361+
*/
362+
export const writeVersion = (filePath: string): void => {
363+
fs.writeFileSync(filePath, `${getVersionMessage()}\n`, "utf8");
364+
};
365+
350366
/**
351367
* Creates the service multistage build and update image tag pipeline.
352368
* One pipeline should exist for each service.
@@ -393,7 +409,9 @@ export const generateServiceBuildAndUpdatePipelineYaml = (
393409
ringBranches,
394410
variableGroups
395411
);
396-
fs.writeFileSync(
412+
413+
writeVersion(pipelineYamlFullPath);
414+
fs.appendFileSync(
397415
pipelineYamlFullPath,
398416
yaml.safeDump(buildYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
399417
"utf8"
@@ -437,7 +455,8 @@ export const updateTriggerBranchesForServiceBuildAndUpdatePipeline = (
437455
buildPipelineYaml.trigger.branches.include = ringBranches;
438456
}
439457

440-
fs.writeFileSync(
458+
writeVersion(pipelineYamlFullPath);
459+
fs.appendFileSync(
441460
pipelineYamlFullPath,
442461
yaml.safeDump(buildPipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
443462
"utf8"
@@ -586,7 +605,8 @@ export const generateHldAzurePipelinesYaml = (
586605
`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}`
587606
);
588607

589-
fs.writeFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
608+
writeVersion(azurePipelinesYamlPath);
609+
fs.appendFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
590610
};
591611

592612
/**
@@ -773,7 +793,9 @@ export const generateHldLifecyclePipelineYaml = async (
773793
logger.info(
774794
`Writing ${PROJECT_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}`
775795
);
776-
fs.writeFileSync(azurePipelinesYamlPath, lifecycleYaml, "utf8");
796+
797+
writeVersion(azurePipelinesYamlPath);
798+
fs.appendFileSync(azurePipelinesYamlPath, lifecycleYaml, "utf8");
777799

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

0 commit comments

Comments
 (0)