Skip to content

Commit

Permalink
Use pipeline run name as Bitbucket build status key
Browse files Browse the repository at this point in the history
* Multiple runs for the same commit now create multiple build status
  records
* Re-runs of pipeline runs update the build status corrresponding to the
  original pipeline run

Closes #736.
  • Loading branch information
michaelsauter committed Nov 7, 2023
1 parent 1089a45 commit 22020e6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 31 deletions.
24 changes: 15 additions & 9 deletions cmd/finish/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ type options struct {
bitbucketAccessToken string
bitbucketURL string
consoleURL string
pipelineRunName string
aggregateTasksStatus string
nexusURL string
nexusUsername string
nexusPassword string
artifactTarget string
debug bool
// pipelineRunName is the name of the Tekton PipelineRun resource.
pipelineRunName string
// preferredPipelineRunName is either the name of the Tekton PipelineRun
// resource or the name of the resource for which this pipeline run
// is a re-run.
preferredPipelineRunName string
aggregateTasksStatus string
nexusURL string
nexusUsername string
nexusPassword string
artifactTarget string
debug bool
}

func main() {
Expand All @@ -40,6 +45,7 @@ func main() {
flag.StringVar(&opts.bitbucketURL, "bitbucket-url", os.Getenv("BITBUCKET_URL"), "bitbucket-url")
flag.StringVar(&opts.consoleURL, "console-url", os.Getenv("CONSOLE_URL"), "web console URL")
flag.StringVar(&opts.pipelineRunName, "pipeline-run-name", "", "name of pipeline run")
flag.StringVar(&opts.preferredPipelineRunName, "preferred-pipeline-run-name", os.Getenv("PREFERRED_PIPELINE_RUN_NAME"), "preferred name of pipeline run")
// See https://tekton.dev/docs/pipelines/pipelines/#using-aggregate-execution-status-of-all-tasks.
// Possible values are: Succeeded, Failed, Completed, None.
flag.StringVar(&opts.aggregateTasksStatus, "aggregate-tasks-status", "None", "aggregate status of all the tasks")
Expand Down Expand Up @@ -84,8 +90,8 @@ func main() {

err = bitbucketClient.BuildStatusCreate(ctxt.GitCommitSHA, bitbucket.BuildStatusCreatePayload{
State: getBitbucketBuildStatus(opts.aggregateTasksStatus),
Key: ctxt.GitCommitSHA,
Name: ctxt.GitCommitSHA,
Key: bitbucket.BuildStatusKey(opts.preferredPipelineRunName, opts.pipelineRunName),
Name: bitbucket.BuildStatusKey(opts.preferredPipelineRunName, opts.pipelineRunName),
URL: prURL,
Description: "ODS Pipeline Build",
})
Expand Down
50 changes: 28 additions & 22 deletions cmd/start/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,31 @@ import (
)

type options struct {
bitbucketAccessToken string
bitbucketURL string
consoleURL string
pipelineRunName string
nexusURL string
nexusUsername string
nexusPassword string
artifactSource string
project string
prKey string
prBase string
httpProxy string
httpsProxy string
noProxy string
url string
gitFullRef string
submodules string
cloneDepth string
cacheBuildTasksForDays int
debug bool
bitbucketAccessToken string
bitbucketURL string
consoleURL string
// pipelineRunName is the name of the Tekton PipelineRun resource.
pipelineRunName string
// preferredPipelineRunName is either the name of the Tekton PipelineRun
// resource or the name of the resource for which this pipeline run
// is a re-run.
preferredPipelineRunName string
nexusURL string
nexusUsername string
nexusPassword string
artifactSource string
project string
prKey string
prBase string
httpProxy string
httpsProxy string
noProxy string
url string
gitFullRef string
submodules string
cloneDepth string
cacheBuildTasksForDays int
debug bool
}

func main() {
Expand All @@ -56,6 +61,7 @@ func main() {
flag.IntVar(&opts.cacheBuildTasksForDays, "cache-build-tasks-for-days", 7, "the number of days build outputs are cached. A negative number can be used to clear the cache.")
flag.StringVar(&opts.consoleURL, "console-url", os.Getenv("CONSOLE_URL"), "web console URL")
flag.StringVar(&opts.pipelineRunName, "pipeline-run-name", "", "name of pipeline run")
flag.StringVar(&opts.preferredPipelineRunName, "preferred-pipeline-run-name", os.Getenv("PREFERRED_PIPELINE_RUN_NAME"), "preferred name of pipeline run")
flag.StringVar(&opts.nexusURL, "nexus-url", os.Getenv("NEXUS_URL"), "Nexus URL")
flag.StringVar(&opts.nexusUsername, "nexus-username", os.Getenv("NEXUS_USERNAME"), "Nexus username")
flag.StringVar(&opts.nexusPassword, "nexus-password", os.Getenv("NEXUS_PASSWORD"), "Nexus password")
Expand Down Expand Up @@ -139,8 +145,8 @@ func main() {

err = bitbucketClient.BuildStatusCreate(ctxt.GitCommitSHA, bitbucket.BuildStatusCreatePayload{
State: bitbucket.BuildStatusInProgress,
Key: ctxt.GitCommitSHA,
Name: ctxt.GitCommitSHA,
Key: bitbucket.BuildStatusKey(opts.preferredPipelineRunName, opts.pipelineRunName),
Name: bitbucket.BuildStatusKey(opts.preferredPipelineRunName, opts.pipelineRunName),
URL: prURL,
Description: "ODS Pipeline Build",
})
Expand Down
8 changes: 8 additions & 0 deletions deploy/chart/templates/task-finish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ spec:
configMapKeyRef:
key: debug
name: ods-pipeline
- name: PREFERRED_PIPELINE_RUN_NAME
valueFrom:
fieldRef:
# preferredName is either the name of the Tekton PipelineRun
# resource or the name of the resource for which this pipeline run
# is a re-run. ods-finish reads this env var to determine the
# Bitbucket build status key.
fieldPath: 'metadata.annotations[''pipeline.openshift.io/preferredName'']'
computeResources: {}
script: |
Expand Down
8 changes: 8 additions & 0 deletions deploy/chart/templates/task-start.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ spec:
configMapKeyRef:
key: debug
name: ods-pipeline
- name: PREFERRED_PIPELINE_RUN_NAME
valueFrom:
fieldRef:
# preferredName is either the name of the Tekton PipelineRun
# resource or the name of the resource for which this pipeline run
# is a re-run. ods-start reads this env var to determine the
# Bitbucket build status key.
fieldPath: 'metadata.annotations[''pipeline.openshift.io/preferredName'']'
computeResources: {}
script: |
if [ -f /etc/ssl/certs/private-cert.pem ]; then
Expand Down
9 changes: 9 additions & 0 deletions pkg/bitbucket/build_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,12 @@ func (c *Client) BuildStatusList(gitCommit string) (*BuildStatusPage, error) {
}

}

// BuildStatusKey returns the key to use for the Bitbucket build status.
func BuildStatusKey(preferredName, name string) string {
if preferredName != "" {
return preferredName
} else {
return name
}
}

0 comments on commit 22020e6

Please sign in to comment.