Skip to content

Commit b533285

Browse files
committed
setpipeline supports detach mode
Signed-off-by: Evan <[email protected]>
1 parent 52cb197 commit b533285

File tree

12 files changed

+219
-38
lines changed

12 files changed

+219
-38
lines changed

atc/builds/planner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ func (visitor *planVisitor) VisitSetPipeline(step *atc.SetPipelineStep) error {
264264
Vars: step.Vars,
265265
VarFiles: step.VarFiles,
266266
InstanceVars: step.InstanceVars,
267+
Detach: step.Detach,
267268
})
268269

269270
return nil

atc/db/build.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ type Build interface {
212212
config atc.Config,
213213
from ConfigVersion,
214214
initiallyPaused bool,
215+
detach bool,
215216
) (Pipeline, bool, error)
216217

217218
ResourceCacheUser() ResourceCacheUser
@@ -1799,6 +1800,7 @@ func (b *build) SavePipeline(
17991800
config atc.Config,
18001801
from ConfigVersion,
18011802
initiallyPaused bool,
1803+
detach bool,
18021804
) (Pipeline, bool, error) {
18031805
tx, err := b.conn.Begin()
18041806
if err != nil {
@@ -1807,8 +1809,14 @@ func (b *build) SavePipeline(
18071809

18081810
defer Rollback(tx)
18091811

1810-
jobID := newNullInt64(b.jobID)
1811-
buildID := newNullInt64(b.id)
1812+
var jobID, buildID sql.NullInt64
1813+
if detach {
1814+
jobID = sql.NullInt64{Valid: false}
1815+
buildID = sql.NullInt64{Valid: false}
1816+
} else {
1817+
jobID = newNullInt64(b.jobID)
1818+
buildID = newNullInt64(b.id)
1819+
}
18121820
pipelineID, isNewPipeline, err := savePipeline(tx, pipelineRef, config, from, initiallyPaused, teamID, jobID, buildID)
18131821
if err != nil {
18141822
return nil, false, err

atc/db/build_in_memory_check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ func (b *inMemoryCheckBuild) Start(atc.Plan) (bool, error) {
662662
func (b *inMemoryCheckBuild) ResourcesChecked() (bool, error) {
663663
return false, errors.New("not implemented for in memory build")
664664
}
665-
func (b *inMemoryCheckBuild) SavePipeline(atc.PipelineRef, int, atc.Config, ConfigVersion, bool) (Pipeline, bool, error) {
665+
func (b *inMemoryCheckBuild) SavePipeline(atc.PipelineRef, int, atc.Config, ConfigVersion, bool, bool) (Pipeline, bool, error) {
666666
return nil, false, errors.New("not implemented for in memory build")
667667
}
668668
func (b *inMemoryCheckBuild) AdoptInputsAndPipes() ([]BuildInput, bool, error) {

atc/db/build_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ var _ = Describe("Build", func() {
806806
BeforeEach(func() {
807807
By("creating a child pipeline")
808808
build, _ := defaultJob.CreateBuild(defaultBuildCreatedBy)
809-
childPipeline, _, _ = build.SavePipeline(atc.PipelineRef{Name: "child1-pipeline"}, defaultTeam.ID(), defaultPipelineConfig, db.ConfigVersion(0), false)
809+
childPipeline, _, _ = build.SavePipeline(atc.PipelineRef{Name: "child1-pipeline"}, defaultTeam.ID(), defaultPipelineConfig, db.ConfigVersion(0), false, false)
810810
build.Finish(db.BuildStatusSucceeded)
811811

812812
childPipeline.Reload()
@@ -831,7 +831,7 @@ var _ = Describe("Build", func() {
831831
for i := 0; i < 5; i++ {
832832
job, _, _ := childPipeline.Job("some-job")
833833
build, _ := job.CreateBuild(defaultBuildCreatedBy)
834-
childPipeline, _, _ = build.SavePipeline(atc.PipelineRef{Name: "child-pipeline-" + strconv.Itoa(i)}, defaultTeam.ID(), defaultPipelineConfig, db.ConfigVersion(0), false)
834+
childPipeline, _, _ = build.SavePipeline(atc.PipelineRef{Name: "child-pipeline-" + strconv.Itoa(i)}, defaultTeam.ID(), defaultPipelineConfig, db.ConfigVersion(0), false, false)
835835
build.Finish(db.BuildStatusSucceeded)
836836
childPipelines = append(childPipelines, childPipeline)
837837
}
@@ -2393,7 +2393,7 @@ var _ = Describe("Build", func() {
23932393
},
23942394
},
23952395
},
2396-
}, db.ConfigVersion(0), false)
2396+
}, db.ConfigVersion(0), false, false)
23972397
Expect(err).ToNot(HaveOccurred())
23982398
Expect(pipeline.ParentJobID()).To(Equal(build.JobID()))
23992399
Expect(pipeline.ParentBuildID()).To(Equal(build.ID()))
@@ -2431,7 +2431,7 @@ var _ = Describe("Build", func() {
24312431
},
24322432
},
24332433
},
2434-
}, db.ConfigVersion(0), false)
2434+
}, db.ConfigVersion(0), false, false)
24352435
Expect(err).ToNot(HaveOccurred())
24362436
Expect(pipeline.ParentJobID()).To(Equal(buildTwo.JobID()))
24372437
Expect(pipeline.ParentBuildID()).To(Equal(buildTwo.ID()))
@@ -2461,7 +2461,7 @@ var _ = Describe("Build", func() {
24612461
},
24622462
},
24632463
},
2464-
}, pipeline.ConfigVersion(), false)
2464+
}, pipeline.ConfigVersion(), false, false)
24652465
Expect(err).To(Equal(db.ErrSetByNewerBuild))
24662466
})
24672467

@@ -2472,7 +2472,7 @@ var _ = Describe("Build", func() {
24722472
Expect(err).ToNot(HaveOccurred())
24732473

24742474
By("re-saving the default pipeline with the build")
2475-
pipeline, _, err := build.SavePipeline(defaultPipelineRef, build.TeamID(), defaultPipelineConfig, db.ConfigVersion(1), false)
2475+
pipeline, _, err := build.SavePipeline(defaultPipelineRef, build.TeamID(), defaultPipelineConfig, db.ConfigVersion(1), false, false)
24762476
Expect(err).ToNot(HaveOccurred())
24772477
Expect(pipeline.ParentJobID()).To(Equal(build.JobID()))
24782478
Expect(pipeline.ParentBuildID()).To(Equal(build.ID()))
@@ -2493,7 +2493,7 @@ var _ = Describe("Build", func() {
24932493
By("setting the pipeline again via a build")
24942494
build, err := defaultJob.CreateBuild(defaultBuildCreatedBy)
24952495
Expect(err).ToNot(HaveOccurred())
2496-
pipeline, _, err = build.SavePipeline(defaultPipelineRef, build.TeamID(), defaultPipelineConfig, pipeline.ConfigVersion(), false)
2496+
pipeline, _, err = build.SavePipeline(defaultPipelineRef, build.TeamID(), defaultPipelineConfig, pipeline.ConfigVersion(), false, false)
24972497
Expect(err).ToNot(HaveOccurred())
24982498

24992499
Expect(pipeline.Paused()).To(BeFalse())
@@ -2513,7 +2513,7 @@ var _ = Describe("Build", func() {
25132513
By("setting the pipeline again via a build")
25142514
build, err := defaultJob.CreateBuild(defaultBuildCreatedBy)
25152515
Expect(err).ToNot(HaveOccurred())
2516-
pipeline, _, err = build.SavePipeline(defaultPipelineRef, build.TeamID(), defaultPipelineConfig, pipeline.ConfigVersion(), false)
2516+
pipeline, _, err = build.SavePipeline(defaultPipelineRef, build.TeamID(), defaultPipelineConfig, pipeline.ConfigVersion(), false, false)
25172517
Expect(err).ToNot(HaveOccurred())
25182518

25192519
Expect(pipeline.Paused()).To(BeTrue())

atc/db/dbfakes/fake_build.go

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atc/db/dbfakes/fake_pipeline.go

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atc/db/pipeline.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type Pipeline interface {
127127
Variables(lager.Logger, creds.Secrets, creds.VarSourcePool) (vars.Variables, error)
128128

129129
SetParentIDs(jobID, buildID int) error
130+
DetachParent() error
130131
}
131132

132133
type pipeline struct {
@@ -1168,6 +1169,39 @@ func (p *pipeline) Variables(logger lager.Logger, globalSecrets creds.Secrets, v
11681169
return allVars, nil
11691170
}
11701171

1172+
func (p *pipeline) DetachParent() error {
1173+
tx, err := p.conn.Begin()
1174+
if err != nil {
1175+
return err
1176+
}
1177+
1178+
defer Rollback(tx)
1179+
1180+
nullID := sql.NullInt64{Valid: false}
1181+
result, err := psql.Update("pipelines").
1182+
Set("parent_job_id", nullID).
1183+
Set("parent_build_id", nullID).
1184+
Where(sq.Eq{
1185+
"id": p.id,
1186+
}).
1187+
RunWith(tx).
1188+
Exec()
1189+
1190+
if err != nil {
1191+
return err
1192+
}
1193+
1194+
rows, err := result.RowsAffected()
1195+
if err != nil {
1196+
return err
1197+
}
1198+
if rows == 0 {
1199+
return ErrSetByNewerBuild
1200+
}
1201+
1202+
return tx.Commit()
1203+
}
1204+
11711205
func (p *pipeline) SetParentIDs(jobID, buildID int) error {
11721206
if jobID <= 0 || buildID <= 0 {
11731207
return errors.New("job and build id cannot be negative or zero-value")

atc/exec/set_pipeline_step.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (step *SetPipelineStep) run(ctx context.Context, state RunState, delegate S
133133
}
134134

135135
var team db.Team
136+
var detach bool
136137
if step.plan.Team == "" {
137138
team = step.teamFactory.GetByID(step.metadata.TeamID)
138139
} else {
@@ -163,6 +164,7 @@ func (step *SetPipelineStep) run(ctx context.Context, state RunState, delegate S
163164
}
164165
if currentTeam.Admin() {
165166
permitted = true
167+
detach = step.plan.Detach
166168
}
167169
if !permitted {
168170
return false, fmt.Errorf(
@@ -202,7 +204,12 @@ func (step *SetPipelineStep) run(ctx context.Context, state RunState, delegate S
202204
fmt.Fprintf(stdout, "no changes to apply.\n")
203205

204206
if found {
205-
err := pipeline.SetParentIDs(step.metadata.JobID, step.metadata.BuildID)
207+
var err error
208+
if detach {
209+
err = pipeline.DetachParent()
210+
} else {
211+
err = pipeline.SetParentIDs(step.metadata.JobID, step.metadata.BuildID)
212+
}
206213
if err != nil {
207214
return false, err
208215
}
@@ -230,7 +237,7 @@ func (step *SetPipelineStep) run(ctx context.Context, state RunState, delegate S
230237
return false, fmt.Errorf("set_pipeline step not attached to a buildID")
231238
}
232239

233-
pipeline, _, err = parentBuild.SavePipeline(pipelineRef, team.ID(), atcConfig, fromVersion, false)
240+
pipeline, _, err = parentBuild.SavePipeline(pipelineRef, team.ID(), atcConfig, fromVersion, false, detach)
234241
if err != nil {
235242
if err == db.ErrSetByNewerBuild {
236243
fmt.Fprintln(stderr, "\x1b[1;33mWARNING: the pipeline was not saved because it was already saved by a newer build\x1b[0m")

0 commit comments

Comments
 (0)