Skip to content

Commit

Permalink
Add support to GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Mar 13, 2020
1 parent f863e19 commit eef78a3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
52 changes: 52 additions & 0 deletions ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit eef78a3

Please sign in to comment.