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

Commit 0d8a536

Browse files
committed
Adding service lifecycle pipeline
1 parent 605b8a2 commit 0d8a536

File tree

5 files changed

+151
-10
lines changed

5 files changed

+151
-10
lines changed

src/commands/project/init.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import os from "os";
33
import path from "path";
44
import shell from "shelljs";
55
import uuid from "uuid/v4";
6-
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
6+
import {
7+
disableVerboseLogging,
8+
enableVerboseLogging,
9+
logger
10+
} from "../../logger";
711
import { initialize } from "./init";
812

913
beforeAll(() => {
@@ -15,7 +19,7 @@ afterAll(() => {
1519
});
1620

1721
describe("Initializing a blank standard repo", () => {
18-
test("bedrock.yaml, maintainers.yaml, and azure-pipelines.yaml gets generated in the project root on init for standard repository", async () => {
22+
test("all standard files get generated in the project root on init for standard repository", async () => {
1923
// Create random directory to initialize
2024
const randomTmpDir = path.join(os.tmpdir(), uuid());
2125
fs.mkdirSync(randomTmpDir);
@@ -28,17 +32,19 @@ describe("Initializing a blank standard repo", () => {
2832
"bedrock.yaml",
2933
"maintainers.yaml",
3034
"azure-pipelines.yaml",
35+
"hld-lifecycle.yaml",
3136
"Dockerfile"
3237
].map(filename => path.join(randomTmpDir, filename));
3338

3439
for (const filepath of filepaths) {
40+
logger.info(filepath);
3541
expect(fs.existsSync(filepath)).toBe(true);
3642
}
3743
});
3844
});
3945

4046
describe("Initializing a blank mono-repo", () => {
41-
test("bedrock.yaml and maintainers.yaml get generated in the project root and azure-pipelines.yaml gets generated in all package directories in a mono-repo", async () => {
47+
test("all standard files get generated in the project root and azure-pipelines.yaml gets generated in all package directories in a mono-repo", async () => {
4248
const randomTmpDir = path.join(os.tmpdir(), uuid());
4349
fs.mkdirSync(randomTmpDir);
4450

@@ -61,7 +67,11 @@ describe("Initializing a blank mono-repo", () => {
6167
});
6268

6369
// root should have bedrock.yaml and maintainers.yaml and should not be in the the package dirs
64-
for (const file of ["bedrock.yaml", "maintainers.yaml"]) {
70+
for (const file of [
71+
"bedrock.yaml",
72+
"maintainers.yaml",
73+
"hld-lifecycle.yaml"
74+
]) {
6575
const filepath = path.join(randomTmpDir, file);
6676
// Should be in package dir
6777
expect(fs.existsSync(filepath)).toBe(true);

src/commands/project/init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as config from "../../config";
66
import {
77
generateDockerfile,
88
generateGitIgnoreFile,
9+
generateHldLifecyclePipelineYaml,
910
generateStarterAzurePipelinesYaml
1011
} from "../../lib/fileutils";
1112
import { exec } from "../../lib/shell";
@@ -109,6 +110,7 @@ export const initialize = async (
109110
defaultRing ? [defaultRing] : []
110111
);
111112
await generateMaintainersFile(absProjectRoot, absPackagePaths);
113+
await generateHldLifecyclePipelineYaml(absProjectRoot);
112114

113115
const gitIgnoreFileContent = "spk.log";
114116

src/lib/fileutils.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { disableVerboseLogging, enableVerboseLogging, logger } from "../logger";
1919
import {
2020
createTestBedrockYaml,
2121
createTestHldAzurePipelinesYaml,
22+
createTestHldLifecyclePipelineYaml,
2223
createTestMaintainersYaml
2324
} from "../test/mockFactory";
2425
import {
@@ -33,6 +34,7 @@ import {
3334
generateDockerfile,
3435
generateGitIgnoreFile,
3536
generateHldAzurePipelinesYaml,
37+
generateHldLifecyclePipelineYaml,
3638
generateStarterAzurePipelinesYaml,
3739
starterAzurePipelines
3840
} from "./fileutils";
@@ -49,6 +51,43 @@ beforeEach(() => {
4951
jest.clearAllMocks();
5052
});
5153

54+
describe("generateHldLifecyclePipelineYaml", () => {
55+
const targetDirectory = "app-repository";
56+
const writeSpy = jest.spyOn(fs, "writeFileSync");
57+
58+
beforeEach(() => {
59+
mockFs({
60+
"app-repository": {}
61+
});
62+
});
63+
64+
afterEach(() => {
65+
mockFs.restore();
66+
});
67+
68+
it("should not do anything if hld-lifecycle.yaml exists", async () => {
69+
const mockFsOptions = {
70+
[`${targetDirectory}/hld-lifecycle.yaml`]: "existing pipeline"
71+
};
72+
mockFs(mockFsOptions);
73+
74+
generateHldLifecyclePipelineYaml(targetDirectory);
75+
expect(writeSpy).not.toBeCalled();
76+
});
77+
78+
it("should generate the hld-lifecycle.yaml if one does not exist", async () => {
79+
const expectedFilePath = `${targetDirectory}/hld-lifecycle.yaml`;
80+
81+
generateHldLifecyclePipelineYaml(targetDirectory);
82+
expect(writeSpy).toBeCalledWith(
83+
expectedFilePath,
84+
createTestHldLifecyclePipelineYaml(),
85+
"utf8"
86+
);
87+
expect(writeSpy).toBeCalled();
88+
});
89+
});
90+
5291
describe("generateHldAzurePipelinesYaml", () => {
5392
const targetDirectory = "hld-repository";
5493
const writeSpy = jest.spyOn(fs, "writeFileSync");

src/lib/fileutils.ts

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,56 @@ import {
1111
IUser
1212
} from "../types";
1313

14+
export const generateHldLifecyclePipelineYaml = async (projectRoot: string) => {
15+
logger.info(
16+
`Generating hld lifecycle pipeline hld-lifecycle.yaml in ${projectRoot}`
17+
);
18+
19+
const azurePipelinesYamlPath = path.join(projectRoot, "hld-lifecycle.yaml");
20+
21+
if (fs.existsSync(azurePipelinesYamlPath)) {
22+
logger.warn(
23+
`Existing hld-lifecycle.yaml found at ${azurePipelinesYamlPath}, skipping generation`
24+
);
25+
26+
return;
27+
}
28+
29+
const hldYaml = hldLifecyclePipelineYaml();
30+
logger.info(`Writing hld-lifecycle.yaml file to ${azurePipelinesYamlPath}`);
31+
fs.writeFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
32+
};
33+
1434
/**
15-
* Writes out the starter azure-pipelines.yaml file to `targetPath`
35+
* Writes out a starter azure-pipelines.yaml
36+
* One pipeline should exist for each service.
1637
*
17-
* @param targetPath Path to write the azure-pipelines.yaml file to
38+
* @param projectRoot Path to the root of the project (where the bedrock.yaml file exists)
39+
* @param packagePath Path to the packages directory
1840
*/
1941
export const generateStarterAzurePipelinesYaml = async (
2042
projectRoot: string,
2143
packagePath: string
2244
) => {
2345
const absProjectRoot = path.resolve(projectRoot);
2446
const absPackagePath = path.resolve(packagePath);
47+
const azurePipelineFileName = `azure-pipelines.yaml`;
2548

26-
logger.info(`Generating starter azure-pipelines.yaml in ${absPackagePath}`);
49+
logger.info(
50+
`Generating starter ${azurePipelineFileName} in ${absPackagePath}`
51+
);
2752

2853
// Check if azure-pipelines.yaml already exists; if it does, skip generation
2954
const azurePipelinesYamlPath = path.join(
3055
absPackagePath,
31-
"azure-pipelines.yaml"
56+
azurePipelineFileName
3257
);
3358
logger.debug(
34-
`Writing azure-pipelines.yaml file to ${azurePipelinesYamlPath}`
59+
`Writing ${azurePipelineFileName} file to ${azurePipelinesYamlPath}`
3560
);
3661
if (fs.existsSync(azurePipelinesYamlPath)) {
3762
logger.warn(
38-
`Existing azure-pipelines.yaml found at ${azurePipelinesYamlPath}, skipping generation`
63+
`Existing ${azurePipelineFileName} found at ${azurePipelinesYamlPath}, skipping generation`
3964
);
4065
} else {
4166
const starterYaml = await starterAzurePipelines({
@@ -220,6 +245,39 @@ const manifestGenerationPipelineYaml = () => {
220245
return yaml.safeDump(pipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER });
221246
};
222247

248+
const hldLifecyclePipelineYaml = () => {
249+
// based on https://github.com/microsoft/bedrock/blob/master/gitops/azure-devops/ManifestGeneration.md#add-azure-pipelines-build-yaml
250+
// tslint:disable: object-literal-sort-keys
251+
// tslint:disable: no-empty
252+
const pipelineyaml: IAzurePipelinesYaml = {
253+
trigger: {
254+
branches: {
255+
include: ["master"]
256+
}
257+
},
258+
pool: {
259+
vmImage: "Ubuntu-16.04"
260+
},
261+
steps: [
262+
{
263+
checkout: "self",
264+
persistCredentials: true,
265+
clean: true
266+
},
267+
{
268+
bash: generateYamlScript([
269+
`echo "hello world. this is where lifecycle management will be implemented."`
270+
]),
271+
displayName: "hello world"
272+
}
273+
]
274+
};
275+
// tslint:enable: object-literal-sort-keys
276+
// tslint:enable: no-empty
277+
278+
return yaml.safeDump(pipelineyaml, { lineWidth: Number.MAX_SAFE_INTEGER });
279+
};
280+
223281
/**
224282
* Update maintainers.yml with new service
225283
*

src/test/mockFactory.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ export const createTestBedrockYaml = (
7777
return asString ? yaml.dump(data) : data;
7878
};
7979

80+
export const createTestHldLifecyclePipelineYaml = (
81+
asString = true
82+
): IAzurePipelinesYaml | string => {
83+
// tslint:disable: object-literal-sort-keys
84+
const data: IAzurePipelinesYaml = {
85+
trigger: {
86+
branches: {
87+
include: ["master"]
88+
}
89+
},
90+
pool: {
91+
vmImage: "Ubuntu-16.04"
92+
},
93+
steps: [
94+
{
95+
checkout: "self",
96+
persistCredentials: true,
97+
clean: true
98+
},
99+
{
100+
bash: `echo "hello world. this is where lifecycle management will be implemented."`,
101+
displayName: "hello world"
102+
}
103+
]
104+
};
105+
// tslint:enable: object-literal-sort-keys
106+
107+
return asString
108+
? yaml.safeDump(data, { lineWidth: Number.MAX_SAFE_INTEGER })
109+
: data;
110+
};
111+
80112
export const createTestHldAzurePipelinesYaml = (
81113
asString = true
82114
): IAzurePipelinesYaml | string => {

0 commit comments

Comments
 (0)