Skip to content

Commit

Permalink
fix(3012): trigger stage teardown job when some stage builds failed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
y-oksaku authored Jun 4, 2024
1 parent c3ba648 commit 23ee643
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion plugins/builds/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ const buildsPlugin = {

// if nextBuild is stage teardown, just return nextBuild
if (current.stage) {
const buildDeletePromises = handleStageFailure({
const buildDeletePromises = await handleStageFailure({
nextJobName,
current,
buildConfig,
Expand Down
48 changes: 35 additions & 13 deletions test/plugins/trigger.test.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fs = require('fs');
const { Server } = require('@hapi/hapi');
const sinon = require('sinon');
const { assert } = require('chai');
const { getStageFromSetupJobName } = require('screwdriver-models/lib/helper');
const { getStageFromSetupJobName, getFullStageJobName } = require('screwdriver-models/lib/helper');

/**
* Mock models for code completion
Expand Down Expand Up @@ -195,20 +195,29 @@ class PipelineFactoryMock {
});
});

Object.keys(stages || {}).forEach(name => {
Object.entries(stages || {}).forEach(([name, stage]) => {
const setupJobName = getFullStageJobName({ stageName: name, jobName: 'setup' });
const teardownJobName = getFullStageJobName({ stageName: name, jobName: 'teardown' });

jobs[setupJobName] = this.server.app.jobFactory.create({
name: setupJobName,
pipeline,
pipelineId: pipeline.id
});
jobs[teardownJobName] = this.server.app.jobFactory.create({
name: teardownJobName,
pipeline,
pipelineId: pipeline.id
});

stages[name] = this.server.app.stageFactory.create({
setup: {
image: 'node:20',
steps: []
},
teardown: {
image: 'node:20',
steps: []
},
...stages[name],
...stage,
setup: jobs[setupJobName].id,
teardown: jobs[teardownJobName].id,
pipelineId: pipeline.id,
name,
archived: false
archived: false,
jobIds: stage.jobs.map(jobName => jobs[jobName].id)
});
});

Expand Down Expand Up @@ -345,7 +354,18 @@ class EventFactoryMock {
const pipeline = this.server.app.pipelineFactory.get(event.pipelineId);

event.workflowGraph = structuredClone(pipeline.workflowGraph);
event.getBuilds = () => {
event.getBuilds = query => {
if (query && query.params && query.params.jobId) {
const { jobId } = query.params;
const jobIds = Array.isArray(jobId) ? jobId : [jobId];

return this.server.app.buildFactory.records.filter(build => {
return (
build && parseInt(build.eventId, 10) === parseInt(event.id, 10) && jobIds.includes(build.job.id)
);
});
}

return this.server.app.buildFactory.records.filter(build => {
return build && parseInt(build.eventId, 10) === parseInt(event.id, 10);
});
Expand Down Expand Up @@ -695,6 +715,8 @@ class StageFactoryMock {
}

create(config) {
delete config.jobs;

const stage = {
...config,
id: this.records.length
Expand Down
18 changes: 18 additions & 0 deletions test/plugins/trigger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,24 @@ describe('trigger tests', () => {
assert.equal(event.getBuildOf('stage@red:teardown').status, 'RUNNING');
});

it('stage teardown is triggered when some stage builds fail', async () => {
const pipeline = await pipelineFactoryMock.createFromFile('stage.yaml');

const event = await eventFactoryMock.create({
pipelineId: pipeline.id,
startFrom: 'hub'
});

await event.getBuildOf('hub').complete('SUCCESS');
await event.getBuildOf('a').complete('SUCCESS');
await event.getBuildOf('stage@red:setup').complete('SUCCESS');
await event.getBuildOf('target1').complete('SUCCESS');
await event.getBuildOf('target2').complete('SUCCESS');
await event.getBuildOf('target3').complete('FAILURE');

assert.equal(event.getBuildOf('stage@red:teardown').status, 'RUNNING');
});

it('[ ~stage@red:setup ] is triggered', async () => {
const pipeline = await pipelineFactoryMock.createFromFile('~stage@red:setup.yaml');

Expand Down

0 comments on commit 23ee643

Please sign in to comment.