Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit e35ad3d

Browse files
committed
Better retry around version tag
1 parent 1bdfa59 commit e35ad3d

File tree

4 files changed

+83
-85
lines changed

4 files changed

+83
-85
lines changed

lib/configuration.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export interface Configuration {
2020
publish: boolean;
2121
access: string;
2222
tag: string[];
23-
gitTag: boolean;
2423
command: string;
2524
docker_cache: string[];
2625
}

lib/events/buildOnPush.ts

Lines changed: 81 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import * as os from "os";
3333
import * as path from "path";
3434
import { Configuration } from "../configuration";
3535
import { BuildOnPushSubscription } from "../typings/types";
36+
import * as pRetry from "p-retry";
3637

3738
const Matchers = [
3839
{
@@ -261,68 +262,12 @@ const NpmInstallStep: NpmStep = {
261262
},
262263
};
263264

264-
const NodeVersionStep: NpmStep = {
265-
name: "version",
266-
runWhen: async (ctx, params) => {
267-
const pj = await fs.readJson(params.project.path("package.json"));
268-
return !pj.scripts?.version;
269-
},
270-
run: async (ctx, params) => {
271-
const push = ctx.data.Push[0];
272-
const pj = await fs.readJson(params.project.path("package.json"));
273-
274-
let pjVersion = pj.version;
275-
if (!pjVersion || pjVersion.length === 0) {
276-
pjVersion = "0.0.0";
277-
}
278-
279-
const version = await github.nextTag(
280-
params.project.id,
281-
"prerelease",
282-
pjVersion,
283-
);
284-
const result = await params.project.spawn(
285-
"npm",
286-
["version", "--no-git-tag-version", version],
287-
{
288-
env: {
289-
...process.env,
290-
PATH: `${params.path}:${process.env.PATH}`,
291-
},
292-
},
293-
);
294-
if (result.status !== 0) {
295-
await params.check.update({
296-
conclusion: "failure",
297-
body: "`npm version` failed",
298-
});
299-
return status.failure(
300-
`\`npm version\` failed on [${push.repo.owner}/${
301-
push.repo.name
302-
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
303-
);
304-
}
305-
return status.success();
306-
},
307-
};
308-
309-
function gitBranchToNpmVersion(branchName: string): string {
310-
// prettier-ignore
311-
return branchName.replace(/\//g, "-").replace(/_/g, "-").replace(/@/g, "");
312-
}
313-
314265
const NpmScriptsStep: NpmStep = {
315266
name: "npm run",
316267
run: async (ctx, params) => {
317268
const push = ctx.data.Push[0];
318269
const cfg = ctx.configuration?.[0]?.parameters;
319-
let scripts = cfg.scripts;
320-
321-
// Test if the project overwrites the version step
322-
const pj = await fs.readJson(params.project.path("package.json"));
323-
if (pj.scripts.version) {
324-
scripts = ["version", ...scripts];
325-
}
270+
const scripts = cfg.scripts;
326271

327272
// Run scripts
328273
for (const script of scripts) {
@@ -442,6 +387,84 @@ function mapSeverity(severity: string): "notice" | "warning" | "failure" {
442387
}
443388
}
444389

390+
const NpmVersionStep: NpmStep = {
391+
name: "version",
392+
runWhen: async ctx => ctx.configuration?.[0]?.parameters.publish,
393+
run: async (ctx, params) => {
394+
const push = ctx.data.Push[0];
395+
let pj = await fs.readJson(params.project.path("package.json"));
396+
397+
let pjVersion = pj.version;
398+
if (!pjVersion || pjVersion.length === 0) {
399+
pjVersion = "0.0.0";
400+
}
401+
402+
let version;
403+
if (!pj.scripts?.version) {
404+
version = await pRetry(
405+
async () => {
406+
const tag = await github.nextTag(
407+
params.project.id,
408+
"prerelease",
409+
pjVersion,
410+
);
411+
await params.project.exec("git", [
412+
"tag",
413+
"-m",
414+
`Version ${tag}`,
415+
tag,
416+
]);
417+
await params.project.exec("git", ["push", "origin", tag]);
418+
return tag;
419+
},
420+
{
421+
retries: 5,
422+
maxTimeout: 2500,
423+
minTimeout: 1000,
424+
randomize: true,
425+
},
426+
);
427+
} else {
428+
await params.project.spawn("npm", ["run", "version"], {
429+
env: {
430+
...process.env,
431+
PATH: `${params.path}:${process.env.PATH}`,
432+
},
433+
});
434+
pj = await fs.readJson(params.project.path("package.json"));
435+
version = pj.version;
436+
}
437+
438+
const result = await params.project.spawn(
439+
"npm",
440+
["version", "--no-git-tag-version", version],
441+
{
442+
env: {
443+
...process.env,
444+
PATH: `${params.path}:${process.env.PATH}`,
445+
},
446+
},
447+
);
448+
if (result.status !== 0) {
449+
await params.check.update({
450+
conclusion: "failure",
451+
body: "`npm version` failed",
452+
});
453+
return status.failure(
454+
`\`npm version\` failed on [${push.repo.owner}/${
455+
push.repo.name
456+
}/${push.after.sha.slice(0, 7)}](${push.after.url})`,
457+
);
458+
}
459+
return status.success();
460+
},
461+
};
462+
463+
function gitBranchToNpmVersion(branchName: string): string {
464+
// prettier-ignore
465+
return branchName.replace(/\//g, "-").replace(/_/g, "-").replace(/@/g, "");
466+
}
467+
445468
const NpmPublishStep: NpmStep = {
446469
name: "npm publish",
447470
runWhen: async ctx => ctx.configuration?.[0]?.parameters.publish,
@@ -592,24 +615,6 @@ function gitBranchToNpmTag(branchName: string): string {
592615
return `branch-${gitBranchToNpmVersion(branchName)}`;
593616
}
594617

595-
const GitTagStep: NpmStep = {
596-
name: "git tag",
597-
runWhen: async ctx => {
598-
return ctx.configuration?.[0]?.parameters?.gitTag;
599-
},
600-
run: async (ctx, params) => {
601-
const pj = await fs.readJson(params.project.path("package.json"));
602-
await params.project.spawn("git", [
603-
"tag",
604-
"-m",
605-
`Version ${pj.version}`,
606-
pj.version,
607-
]);
608-
await params.project.spawn("git", ["push", "origin", pj.version]);
609-
return status.success();
610-
},
611-
};
612-
613618
export const handler: EventHandler<
614619
BuildOnPushSubscription,
615620
Configuration
@@ -624,9 +629,8 @@ export const handler: EventHandler<
624629
SetupNodeStep,
625630
NpmInstallStep,
626631
NpmScriptsStep,
627-
NodeVersionStep,
632+
NpmVersionStep,
628633
NpmPublishStep,
629-
GitTagStep,
630634
],
631635
parameters: { body: [] },
632636
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"main": "node_modules/@atomist/skill/lib/function.js",
33
"dependencies": {
4-
"@atomist/skill": "0.1.1-1"
4+
"@atomist/skill": "0.1.1-1",
5+
"p-retry": "^4.2.0"
56
},
67
"devDependencies": {
78
"@google-cloud/functions-framework": "^1.6.0",

skill.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ export const Skill = skill<Configuration & { repos: any }>({
9494
"Register the published package with the given tags. If no tag is set here, the package will get published with a branch specific tag, e.g. `branch-<name of branch>`.",
9595
required: false,
9696
},
97-
gitTag: {
98-
type: ParameterType.Boolean,
99-
displayName: "Git tag",
100-
description: "Create a Git tag using the `package.json` version",
101-
required: false,
102-
},
10397
command: {
10498
type: ParameterType.String,
10599
displayName: "Shell command",

0 commit comments

Comments
 (0)