Added a buildkite detector for detecting the correct env vars in CI (#7933)

* Added a buildkite detector for detecting the correct env vars in CI

* adding pending changelog entry

* fixed PR logic to actually match the Buildkite Docs and simplified if statement, Fixed a few typos in comments and added PR to CHANGELOG_PENDING.md

* made PR number fetch easier to read

* fixing typo in comment
This commit is contained in:
Adam Wilczek 2021-11-15 11:10:07 -06:00 committed by GitHub
parent 272c4643b2
commit a5f72ddbeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 3 deletions

View file

@ -1,4 +1,5 @@
### Improvements
* Adds CI detector for Buildkite [#7933](https://github.com/pulumi/pulumi/pull/7933)
- [CLI] Adding the ability to use `pulumi org set [name]` to set a default org
to use when creating a stacks in the Pulumi Service backend or Self -hosted Service

View file

@ -0,0 +1,56 @@
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ciutil
import (
"os"
)
// buildkiteCI represents a Buildkite CI/CD system.
type buildkiteCI struct {
baseCI
}
// DetectVars detects the env vars for a Buildkite Build.
func (bci buildkiteCI) DetectVars() Vars {
v := Vars{Name: Buildkite}
// https://buildkite.com/docs/pipelines/environment-variables#bk-env-vars-buildkite-branch
v.BranchName = os.Getenv("BUILDKITE_BRANCH")
// https://buildkite.com/docs/pipelines/environment-variables#bk-env-vars-buildkite-build-id
v.BuildID = os.Getenv("BUILDKITE_BUILD_ID")
// https://buildkite.com/docs/pipelines/environment-variables#bk-env-vars-buildkite-build-number
v.BuildNumber = os.Getenv("BUILDKITE_BUILD_NUMBER")
// https://buildkite.com/docs/pipelines/environment-variables#bk-env-vars-buildkite-build-url
v.BuildURL = os.Getenv("BUILDKITE_BUILD_URL")
// https://buildkite.com/docs/pipelines/environment-variables#bk-env-vars-buildkite-message
// This is usually the commit message but can be other messages.
v.CommitMessage = os.Getenv("BUILDKITE_MESSAGE")
// https://buildkite.com/docs/pipelines/environment-variables#bk-env-vars-buildkite-pull-request
// If Buildkite's PR env var it is a pull request of the supplied number, else the build type is
// whatever Buildkite says it is. Pull requests are webhooks just like a standard push so this allows
// us to differentiate the two.
prNumber := os.Getenv("BUILDKITE_PULL_REQUEST")
if prNumber != "false" {
v.PRNumber = prNumber
v.BuildType = "PullRequest"
} else {
// https://buildkite.com/docs/pipelines/environment-variables#bk-env-vars-buildkite-source
v.BuildType = os.Getenv("BUILDKITE_SOURCE")
}
// https://buildkite.com/docs/pipelines/environment-variables#bk-env-vars-buildkite-commit
v.SHA = os.Getenv("BUILDKITE_COMMIT")
return v
}

View file

@ -48,9 +48,11 @@ var detectors = map[SystemName]system{
EnvVarsToDetect: []string{"TF_BUILD"},
},
},
Buildkite: baseCI{
Name: Buildkite,
EnvVarsToDetect: []string{"BUILDKITE"},
Buildkite: buildkiteCI{
baseCI: baseCI{
Name: Buildkite,
EnvVarsToDetect: []string{"BUILDKITE"},
},
},
CircleCI: circleCICI{
baseCI: baseCI{

View file

@ -35,6 +35,13 @@ func TestDetectVars(t *testing.T) {
"BUILD_BUILDID": buildNumber,
"GITHUB_ACTIONS": "",
},
Buildkite: {
"GITHUB_ACTIONS": "",
"TRAVIS": "",
"BUILDKITE": "true",
"BUILDKITE_BUILD_NUMBER": buildNumber,
"BUILDKITE_BUILD_ID": buildID,
},
CircleCI: {
"TRAVIS": "",
"CIRCLECI": "true",