Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support Google Cloud Build #521

Merged
merged 4 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.7.5
github.com/suzuki-shunsuke/github-comment-metadata v0.1.0
github.com/suzuki-shunsuke/go-ci-env v1.1.0
github.com/suzuki-shunsuke/go-ci-env/v3 v3.0.0
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0
github.com/suzuki-shunsuke/go-httpclient v1.0.0
github.com/suzuki-shunsuke/go-timeout v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ github.com/suzuki-shunsuke/flute/v2 v2.0.0 h1:cDpi+h9wceJuOEjOxTvZNQXsIS+lv2Ism4
github.com/suzuki-shunsuke/flute/v2 v2.0.0/go.mod h1:wsUAUZbz+DSjv+XDv/gZeEX/mMu1y9kzIXCN5Slu6NY=
github.com/suzuki-shunsuke/github-comment-metadata v0.1.0 h1:89uGvBINoWZ4p2dGj7S695bm/L1H175eMp/D47ltSPs=
github.com/suzuki-shunsuke/github-comment-metadata v0.1.0/go.mod h1:GNDhEmWAJ6Bbk9rIds0mAMF4noyPV3EqwqLetnEoNLg=
github.com/suzuki-shunsuke/go-ci-env v1.1.0 h1:eGpItM2bEDtHFXYYEm8zz+kDGxWlhOtwNK58cREa1QU=
github.com/suzuki-shunsuke/go-ci-env v1.1.0/go.mod h1:kO9UgcQIAH4Pu4ESkUJHPuQEtesxHPKkpQqeJHJzzdk=
github.com/suzuki-shunsuke/go-ci-env/v3 v3.0.0 h1:DoYPwXHqvWDcU4zB4d81ed1q1MsYrsknTco5qP3+ivM=
github.com/suzuki-shunsuke/go-ci-env/v3 v3.0.0/go.mod h1:VmLj5u0w7Yf/IJIzZ+TWiB7mVT3pRKPMeb0Jssk7YsA=
github.com/suzuki-shunsuke/go-cliutil v0.0.0-20181211154308-176f852d9bca/go.mod h1:Vq3NkhgmA9DT/2UZ08x/3A34xxvzQ/vTMABnTWKoMbY=
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0 h1:oVXrrYNGBq4POyITQNWKzwsYz7B2nUcqtDbeX4BfeEc=
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0/go.mod h1:kDFtLeftDiIUUHXGI3xq5eJ+uAOi50FPrxPENTHktJ0=
Expand Down
89 changes: 89 additions & 0 deletions pkg/platform/google_cloud_build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package platform

import (
"fmt"
"os"
"strconv"

"github.com/suzuki-shunsuke/go-ci-env/v3/cienv"
)

type GoogleCloudBuild struct {
getenv func(string) string
}

func NewGoogleCloudBuild(param *cienv.Param) *GoogleCloudBuild {
if param == nil || param.Getenv == nil {
return &GoogleCloudBuild{
getenv: os.Getenv,
}
}
return &GoogleCloudBuild{
getenv: param.Getenv,
}
}

func (cb *GoogleCloudBuild) ID() string {
return "google-cloud-build"
}

func (cb *GoogleCloudBuild) Match() bool {
return cb.getenv("GOOGLE_CLOUD_BUILD") != ""
}

func (cb *GoogleCloudBuild) RepoOwner() string {
return ""
}

func (cb *GoogleCloudBuild) RepoName() string {
return ""
}

func (cb *GoogleCloudBuild) Ref() string {
return ""
}

func (cb *GoogleCloudBuild) Tag() string {
return ""
}

func (cb *GoogleCloudBuild) Branch() string {
return ""
}

func (cb *GoogleCloudBuild) PRBaseBranch() string {
return ""
}

func (cb *GoogleCloudBuild) SHA() string {
return cb.getenv("COMMIT_SHA")
}

func (cb *GoogleCloudBuild) IsPR() bool {
return cb.getenv("_PR_NUMBER") != ""
}

func (cb *GoogleCloudBuild) PRNumber() (int, error) {
pr := cb.getenv("_PR_NUMBER")
if pr == "" {
return 0, nil
}
b, err := strconv.Atoi(pr)
if err == nil {
return b, nil
}
return 0, fmt.Errorf("_PR_NUMBER is invalid. It failed to parse _PR_NUMBER as an integer: %w", err)
}

func (cb *GoogleCloudBuild) JobURL() string {
region := cb.getenv("_REGION")
if region == "" {
region = "global"
}
return fmt.Sprintf(
"https://console.cloud.google.com/cloud-build/builds;region=%s/%s?project=%s",
region,
cb.getenv("BUILD_ID"),
cb.getenv("PROJECT_ID"),
)
}
9 changes: 6 additions & 3 deletions pkg/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"

"github.com/suzuki-shunsuke/github-comment/pkg/option"
"github.com/suzuki-shunsuke/go-ci-env/cienv"
"github.com/suzuki-shunsuke/go-ci-env/v3/cienv"
)

type Platform struct {
Expand Down Expand Up @@ -116,16 +116,19 @@ func (pt *Platform) CI() string {
if pt.platform == nil {
return ""
}
return pt.platform.CI()
return pt.platform.ID()
}

func (pt *Platform) ComplementExec(opts *option.ExecOptions) error {
return pt.complement(&opts.Options)
}

func Get(param *Param) *Platform {
cienv.Add(func(param *cienv.Param) cienv.Platform {
return NewGoogleCloudBuild(param)
})
return &Platform{
platform: cienv.Get(),
platform: cienv.Get(nil),
generic: &generic{
param: param,
},
Expand Down
11 changes: 11 additions & 0 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"html/template"
"os"
"strings"

"github.com/Masterminds/sprig/v3"
Expand All @@ -17,11 +18,21 @@ type ParamGetTemplates struct {
}

func GetTemplates(param *ParamGetTemplates) map[string]string {
cloudBuildRegion := os.Getenv("_REGION")
if cloudBuildRegion == "" {
cloudBuildRegion = "global"
}
buildLinks := map[string]string{
"circleci": `[workflow](https://circleci.com/workflow-run/{{env "CIRCLE_WORKFLOW_ID" }}) [job]({{env "CIRCLE_BUILD_URL"}}) (job: {{env "CIRCLE_JOB"}})`,
"codebuild": `[Build link]({{env "CODEBUILD_BUILD_URL"}})`,
"drone": `[build]({{env "DRONE_BUILD_LINK"}}) [step]({{env "DRONE_BUILD_LINK"}}/{{env "DRONE_STAGE_NUMBER"}}/{{env "DRONE_STEP_NUMBER"}})`,
"github-actions": `[Build link](https://github.com/{{env "GITHUB_REPOSITORY"}}/actions/runs/{{env "GITHUB_RUN_ID"}})`,
"google-cloud-build": fmt.Sprintf(
"[Build link](https://console.cloud.google.com/cloud-build/builds;region=%s/%s?project=%s)",
cloudBuildRegion,
os.Getenv("BUILD_ID"),
os.Getenv("PROJECT_ID"),
),
}

builtinTemplates := map[string]string{
Expand Down