diff --git a/README.md b/README.md index 4bf712f..e53603f 100644 --- a/README.md +++ b/README.md @@ -349,6 +349,7 @@ Currently, supported CI are here: - Drone - Jenkins - GitLab CI +- GitHub Actions ### Private Repository Considerations GitHub private repositories require the `repo` and `write:discussion` permissions. diff --git a/ci.go b/ci.go index 7d48d8e..36f3794 100644 --- a/ci.go +++ b/ci.go @@ -136,3 +136,13 @@ func gitlabci() (ci CI, err error) { ci.PR.Number, err = strconv.Atoi(pr) return ci, err } + +func githubActions() (ci CI, err error) { + ci.URL = fmt.Sprintf( + "https://github.com/%s/runs/%s", + os.Getenv("GITHUB_REPOSITORY"), + os.Getenv("GITHUB_RUN_ID"), + ) + ci.PR.Revision = os.Getenv("GITHUB_SHA") + return ci, err +} diff --git a/ci_test.go b/ci_test.go index ceb286e..8966268 100644 --- a/ci_test.go +++ b/ci_test.go @@ -707,3 +707,55 @@ func TestGitLabCI(t *testing.T) { } } } + +func TestGitHubActions(t *testing.T) { + envs := []string{ + "GITHUB_SHA", + "GITHUB_REPOSITORY", + "GITHUB_RUN_ID", + } + saveEnvs := make(map[string]string) + for _, key := range envs { + saveEnvs[key] = os.Getenv(key) + os.Unsetenv(key) + } + defer func() { + for key, value := range saveEnvs { + os.Setenv(key, value) + } + }() + + // https://help.github.com/ja/actions/configuring-and-managing-workflows/using-environment-variables + testCases := []struct { + fn func() + ci CI + ok bool + }{ + { + fn: func() { + os.Setenv("GITHUB_SHA", "abcdefg") + os.Setenv("GITHUB_REPOSITORY", "mercari/tfnotify") + os.Setenv("GITHUB_RUN_ID", "12345") + }, + ci: CI{ + PR: PullRequest{ + Revision: "abcdefg", + Number: 0, + }, + URL: "https://github.com/mercari/tfnotify/runs/12345", + }, + ok: true, + }, + } + + for _, testCase := range testCases { + testCase.fn() + ci, err := githubActions() + if !reflect.DeepEqual(ci, testCase.ci) { + t.Errorf("got %q but want %q", ci, testCase.ci) + } + if (err == nil) != testCase.ok { + t.Errorf("got error %q", err) + } + } +} diff --git a/config/config.go b/config/config.go index b8fe115..ad9e62f 100644 --- a/config/config.go +++ b/config/config.go @@ -125,6 +125,8 @@ func (cfg *Config) Validation() error { // ok pattern case "jenkins": // ok pattern + case "github-actions": + // ok pattern default: return fmt.Errorf("%s: not supported yet", cfg.CI) } diff --git a/main.go b/main.go index c17c29e..e031976 100644 --- a/main.go +++ b/main.go @@ -77,6 +77,11 @@ func (t *tfnotify) Run() error { if err != nil { return err } + case "github-actions": + ci, err = githubActions() + if err != nil { + return err + } case "": return fmt.Errorf("CI service: required (e.g. circleci)") default: