From a5f72ddbeb7335039df91a5dc174dfec5a44e22e Mon Sep 17 00:00:00 2001 From: Adam Wilczek <2548692+arwilczek90@users.noreply.github.com> Date: Mon, 15 Nov 2021 11:10:07 -0600 Subject: [PATCH] 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 --- CHANGELOG_PENDING.md | 1 + sdk/go/common/util/ciutil/buildkite.go | 56 ++++++++++++++++++++++++++ sdk/go/common/util/ciutil/detect.go | 8 ++-- sdk/go/common/util/ciutil/vars_test.go | 7 ++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 sdk/go/common/util/ciutil/buildkite.go diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 98f445d43..f9e9f2789 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -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 diff --git a/sdk/go/common/util/ciutil/buildkite.go b/sdk/go/common/util/ciutil/buildkite.go new file mode 100644 index 000000000..b73db618b --- /dev/null +++ b/sdk/go/common/util/ciutil/buildkite.go @@ -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 +} diff --git a/sdk/go/common/util/ciutil/detect.go b/sdk/go/common/util/ciutil/detect.go index 95ed2d80c..6fdfa2be2 100644 --- a/sdk/go/common/util/ciutil/detect.go +++ b/sdk/go/common/util/ciutil/detect.go @@ -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{ diff --git a/sdk/go/common/util/ciutil/vars_test.go b/sdk/go/common/util/ciutil/vars_test.go index 5b43a925f..5915d5df1 100644 --- a/sdk/go/common/util/ciutil/vars_test.go +++ b/sdk/go/common/util/ciutil/vars_test.go @@ -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",