Add GitHub Actions detection and update the Pulumi CI escape hatch with more CI vars (#4758)

* Add GitHub Actions detection. Update the Pulumi CI escape hatch with more CI vars.
This commit is contained in:
Praneet Loke 2020-06-08 17:24:12 -07:00 committed by GitHub
parent b93259c6bb
commit 407896730c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 13 deletions

View file

@ -7,6 +7,9 @@ CHANGELOG
- Export `CustomTimeouts` in the Python SDK
[#4747](https://github.com/pulumi/pulumi/pull/4747)
- Add GitHub Actions CI detection
[#4758](https://github.com/pulumi/pulumi/pull/4758)
- Allow users to specify base64 encoded strings as GOOGLE_CREDENTIALS
[#4773](https://github.com/pulumi/pulumi/pull/4773)

View file

@ -84,9 +84,11 @@ var detectors = map[SystemName]system{
},
},
GitHub: baseCI{
Name: GitHub,
EnvVarsToDetect: []string{"GITHUB_WORKFLOW"},
GitHubActions: githubActionsCI{
baseCI{
Name: GitHubActions,
EnvVarsToDetect: []string{"GITHUB_ACTIONS"},
},
},
GitLab: gitlabCI{
baseCI: baseCI{

View file

@ -30,9 +30,13 @@ type genericCICI struct {
func (g genericCICI) DetectVars() Vars {
v := Vars{}
v.Name = SystemName(os.Getenv("PULUMI_CI_SYSTEM"))
v.BranchName = os.Getenv("PULUMI_CI_BRANCH_NAME")
v.BuildID = os.Getenv("PULUMI_CI_BUILD_ID")
v.BuildNumber = os.Getenv("PULUMI_CI_BUILD_NUMBER")
v.BuildType = os.Getenv("PULUMI_CI_BUILD_TYPE")
v.BuildURL = os.Getenv("PULUMI_CI_BUILD_URL")
v.CommitMessage = os.Getenv("PULUMI_COMMIT_MESSAGE")
v.PRNumber = os.Getenv("PULUMI_PR_NUMBER")
v.SHA = os.Getenv("PULUMI_CI_PULL_REQUEST_SHA")
return v

View file

@ -0,0 +1,70 @@
// Copyright 2016-2019, 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 (
"encoding/json"
"fmt"
"os"
"strconv"
)
// githubActionsCI represents the GitHub Actions CI system.
type githubActionsCI struct {
baseCI
}
// githubPR represents the `pull_request` payload posted by GitHub to trigger
// workflows for PRs. Note that this is only a partial representation as we
// don't need anything other than the PR number.
// See https://developer.github.com/webhooks/event-payloads/#pull_request.
type githubPR struct {
Action string `json:"action"`
Number int64 `json:"number"`
}
// githubActionsPullRequestEvent represents the webhook payload for a pull_request event.
// https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request
type githubActionsPullRequestEvent struct {
PullRequest githubPR `json:"pull_request"`
}
// DetectVars detects the GitHub Actions env vars.
// nolint: lll
// See https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables.
func (t githubActionsCI) DetectVars() Vars {
v := Vars{Name: GitHubActions}
v.BuildID = os.Getenv("GITHUB_RUN_ID")
v.BuildNumber = os.Getenv("GITHUB_RUN_NUMBER")
v.BuildType = os.Getenv("GITHUB_EVENT_NAME")
v.SHA = os.Getenv("GITHUB_SHA")
v.BranchName = os.Getenv("GITHUB_REF")
repoSlug := os.Getenv("GITHUB_REPOSITORY")
if repoSlug != "" && v.BuildID != "" {
v.BuildURL = fmt.Sprintf("https://github.com/%s/actions/runs/%s", repoSlug, v.BuildID)
}
// Try to use the pull_request webhook payload to extract the PR number.
// For Pull Requests, GitHub stores the payload of the webhook that triggered the
// workflow in a path. The path is identified by GITHUB_EVENT_PATH.
if v.BuildType == "pull_request" {
eventPath := os.Getenv("GITHUB_EVENT_PATH")
var prEvent githubActionsPullRequestEvent
if err := json.Unmarshal([]byte(eventPath), &prEvent); err == nil {
v.PRNumber = strconv.FormatInt(prEvent.PullRequest.Number, 10)
}
}
return v
}

View file

@ -37,16 +37,16 @@ const (
// to their CI build.
GenericCI SystemName = "Generic CI"
GitHub SystemName = "GitHub"
GitLab SystemName = "GitLab"
GoCD SystemName = "GoCD"
Hudson SystemName = "Hudson"
Jenkins SystemName = "Jenkins"
MagnumCI SystemName = "Magnum CI"
Semaphore SystemName = "Semaphore"
TaskCluster SystemName = "TaskCluster"
TeamCity SystemName = "TeamCity"
Travis SystemName = "Travis CI"
GitHubActions SystemName = "GitHub Actions"
GitLab SystemName = "GitLab CI/CD"
GoCD SystemName = "GoCD"
Hudson SystemName = "Hudson"
Jenkins SystemName = "Jenkins"
MagnumCI SystemName = "Magnum CI"
Semaphore SystemName = "Semaphore"
TaskCluster SystemName = "TaskCluster"
TeamCity SystemName = "TeamCity"
Travis SystemName = "Travis CI"
)
// SystemName is a recognized CI system.