From 89f4842a2be50daed865ff4c2eb67ddd91c03f34 Mon Sep 17 00:00:00 2001 From: Nightapes Date: Wed, 21 Aug 2019 21:39:05 +0200 Subject: [PATCH] fear(ci): add github actions --- .github/workflows/main.yml | 4 +++- internal/ci/ci.go | 1 + internal/ci/ci_test.go | 22 +++++++++++++++++++++ internal/ci/github_actions.go | 36 +++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 internal/ci/github_actions.go diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ab907b0..61a02b7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,9 +28,11 @@ jobs: golangci-lint run ./... - name: Run tests - run: go test -v ./... + run: go test ./... - name: Build binary + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | go build -o build/go-semantic-release-temp ./cmd/go-semantic-release/ go build -o build/go-semantic-release -ldflags "-w -s --X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/ diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 4f1acd6..066271c 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -42,6 +42,7 @@ func GetCIProvider(gitUtil *gitutil.GitUtil, envs map[string]string) (*ProviderC services := []Service{ Travis{}, + GithubActions{}, Git{gitUtil: gitUtil}, // GIt must be the last option to check } diff --git a/internal/ci/ci_test.go b/internal/ci/ci_test.go index 38a24a1..e6cd8bb 100644 --- a/internal/ci/ci_test.go +++ b/internal/ci/ci_test.go @@ -89,6 +89,28 @@ func TestCi(t *testing.T) { result: &ci.ProviderConfig{IsPR: false, PR: "", PRBranch: "", Branch: "master", Tag: "TAG", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "https://travis-ci.com/owner/repo/builds/1234", Service: "travis", Name: "Travis CI"}, hasError: false, }, + { + service: "Github Actions PR", + envs: map[string]string{ + "GITHUB_EVENT_NAME": "pull_request", + "GITHUB_SHA": "190bfd6aa60022afd0ef830342cfb07e33c45f37", + "GITHUB_REF": "master", + "GITHUB_ACTION": "action", + }, + result: &ci.ProviderConfig{IsPR: true, PR: "", PRBranch: "", Branch: "master", Tag: "", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "", Service: "GithubActions", Name: "GithubActions CI"}, + hasError: false, + }, + { + service: "Github Actions Push", + envs: map[string]string{ + "GITHUB_EVENT_NAME": "push", + "GITHUB_SHA": "190bfd6aa60022afd0ef830342cfb07e33c45f37", + "GITHUB_REF": "refs/heads/feature-branch-1", + "GITHUB_ACTION": "action", + }, + result: &ci.ProviderConfig{IsPR: false, PR: "", PRBranch: "", Branch: "refs/heads/feature-branch-1", Tag: "", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "", Service: "GithubActions", Name: "GithubActions CI"}, + hasError: false, + }, } for _, config := range testConfigs { diff --git a/internal/ci/github_actions.go b/internal/ci/github_actions.go new file mode 100644 index 0000000..02b06ed --- /dev/null +++ b/internal/ci/github_actions.go @@ -0,0 +1,36 @@ +package ci + +import ( + "fmt" + + log "github.com/sirupsen/logrus" +) + +//GithubActions struct +type GithubActions struct{} + +//Detect if on GithubActions +func (t GithubActions) detect(envs map[string]string) (*ProviderConfig, error) { + + if _, exists := envs["GITHUB_ACTION"]; !exists { + return nil, fmt.Errorf("not running on Github Actions") + } + + isPR := false + + value := envs["GITHUB_EVENT_NAME"] + + if value == "pull_request" { + isPR = true + } else { + log.Debugf("GITHUB_EVENT_NAME=%s, not running on pr", value) + } + + return &ProviderConfig{ + Service: "GithubActions", + Name: "GithubActions CI", + Commit: envs["GITHUB_SHA"], + Branch: envs["GITHUB_REF"], + IsPR: isPR, + }, nil +}