Skip to content

Commit

Permalink
Add support for Jenkins
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrudolph committed Aug 19, 2020
1 parent bd04528 commit 494a7c4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
49 changes: 48 additions & 1 deletion metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ func NewMetadata(envs map[string]string, now func() time.Time) (Metadata, error)
return newCircleMetadata(envs, now)
case envs["GITHUB_ACTIONS"] == "true":
return newGithubMetadata(envs, now)
case envs["JENKINS_HOME"] != "":
return newJenkinsMetadata(envs, now)
case envs["SEMAPHORE"] == "true":
return newSemaphoreMetadata(envs, now)
case envs["TRAVIS"] == "true":
return newTravisMetadata(envs, now)
default:
return nil, fmt.Errorf("unrecognized environment: system does not appear to be a supported CI provider (Buildkite, CircleCI, GitHub Actions, Semaphore, or Travis CI)")
return nil, fmt.Errorf("unrecognized environment: system does not appear to be a supported CI provider (Buildkite, CircleCI, GitHub Actions, Jenkins, Semaphore, or Travis CI)")
}
}

Expand Down Expand Up @@ -219,6 +221,51 @@ func (g *githubMetadata) MarshalYAML() (out []byte, err error) {
return marshalYAML(g)
}

var _ Metadata = (*jenkinsMetadata)(nil)

type jenkinsMetadata struct {
AbstractMetadata `yaml:",inline"`

GitBranch string `env:"GIT_BRANCH" yaml:"-"`
GitCommit string `env:"GIT_COMMIT" yaml:"-"`
GitURL string `env:"GIT_URL" yaml:"-"`
}

func newJenkinsMetadata(envs map[string]string, now func() time.Time) (Metadata, error) {
m := &jenkinsMetadata{}

if err := env.Parse(m, env.Options{Environment: envs}); err != nil {
return nil, err
}

m.Branch = m.GitBranch
m.CIProvider = "jenkins"
m.Commit = m.GitCommit
m.Timestamp = now()

url, ok := envs["BUILD_URL"]
if !ok || url == "" {
return nil, fmt.Errorf("missing required environment variable: BUILD_URL")
}
m.BuildURL = url

nwo, err := nameWithOwnerFromGitURL(m.GitURL)
if err != nil {
return nil, err
}
m.RepoNameWithOwner = nwo

if m.Check == "" {
m.Check = "jenkins"
}

return m, nil
}

func (b *jenkinsMetadata) MarshalYAML() (out []byte, err error) {
return marshalYAML(b)
}

var _ Metadata = (*semaphoreMetadata)(nil)

type semaphoreMetadata struct {
Expand Down
22 changes: 22 additions & 0 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ func TestNewMetadata(t *testing.T) {
},
fixture: "./testdata/github.yml",
},
{
name: "Jenkins",
envs: map[string]string{
"BUILD_URL": "https://some-jenkins-server.com/job/some-project/8675309",
"GIT_BRANCH": "origin/some-branch",
"GIT_COMMIT": "1f192ff735f887dd7a25229b2ece0422d17931f5",
"GIT_URL": "https://github.com/some-owner/some-repo.git",
"JENKINS_HOME": "/var/lib/jenkins",
},
fixture: "./testdata/jenkins.yml",
},
{
name: "Semaphore",
envs: map[string]string{
Expand Down Expand Up @@ -189,6 +200,17 @@ func TestNewMetadata_customCheckName(t *testing.T) {
expectedProvider: "github-actions",
expectedCheck: "some-custom-check-name",
},
{
name: "Jenkins",
envs: map[string]string{
"JENKINS_HOME": "/var/lib/jenkins",
"BUILDPULSE_CHECK_NAME": "some-custom-check-name",
"BUILD_URL": "https://some-jenkins-server.com/job/some-project/8675309",
"GIT_URL": "https://github.com/some-owner/some-repo.git",
},
expectedProvider: "jenkins",
expectedCheck: "some-custom-check-name",
},
{
name: "Semaphore",
envs: map[string]string{
Expand Down
7 changes: 7 additions & 0 deletions metadata/testdata/jenkins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:branch: origin/some-branch
:build_url: https://some-jenkins-server.com/job/some-project/8675309
:check: jenkins
:ci_provider: jenkins
:commit: 1f192ff735f887dd7a25229b2ece0422d17931f5
:repo_name_with_owner: some-owner/some-repo
:timestamp: 2020-07-11T01:02:03Z

0 comments on commit 494a7c4

Please sign in to comment.