-
Notifications
You must be signed in to change notification settings - Fork 38
feat(runner): Integrate runner for Jenkins jobs #411
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
app/controlplane/api/gen/frontend/workflowcontract/v1/crafting_schema.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
90 changes: 47 additions & 43 deletions
90
app/controlplane/api/workflowcontract/v1/crafting_schema.pb.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// Copyright 2023 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package runners | ||
|
||
import "os" | ||
|
||
type JenkinsJob struct{} | ||
|
||
const JenkinsJobID = "jenkins-job" | ||
|
||
func NewJenkinsJob() *JenkinsJob { | ||
return &JenkinsJob{} | ||
} | ||
|
||
// Checks whether we are within a Jenkins job | ||
func (r *JenkinsJob) CheckEnv() bool { | ||
for _, envVarName := range []string{"JENKINS_HOME", "BUILD_URL"} { | ||
if os.Getenv(envVarName) == "" { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (r *JenkinsJob) ListEnvVars() []string { | ||
return []string{ | ||
// Some info about the job | ||
"JOB_NAME", | ||
"BUILD_URL", | ||
|
||
// Some info about the commit (Jenkins Git Plugin) | ||
// NOTE: Commenting these vars out because they are not always present | ||
// and current chainloop behavior requires these to be set. | ||
// "GIT_BRANCH", | ||
// "GIT_COMMIT", | ||
|
||
// Some info about the agent | ||
"AGENT_WORKDIR", | ||
"NODE_NAME", | ||
} | ||
} | ||
|
||
func (r *JenkinsJob) ResolveEnvVars() map[string]string { | ||
return resolveEnvVars(r.ListEnvVars()) | ||
} | ||
|
||
func (r *JenkinsJob) String() string { | ||
return JenkinsJobID | ||
} | ||
|
||
func (r *JenkinsJob) RunURI() string { | ||
return os.Getenv("BUILD_URL") | ||
} |
119 changes: 119 additions & 0 deletions
119
internal/attestation/crafter/runners/jenkinsjob_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// Copyright 2023 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package runners | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type jenkinsJobSuite struct { | ||
suite.Suite | ||
runner *JenkinsJob | ||
} | ||
|
||
func (s *jenkinsJobSuite) TestCheckEnv() { | ||
testCases := []struct { | ||
name string | ||
env map[string]string | ||
want bool | ||
}{ | ||
{ | ||
name: "empty", | ||
env: map[string]string{}, | ||
want: false, | ||
}, | ||
{ | ||
name: "missing JENKINS_HOME", | ||
env: map[string]string{ | ||
"BUILD_URL": "http://some-build-url/", | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "missing BUILD_URL", | ||
env: map[string]string{ | ||
"JENKINS_HOME": "http://some-jenkins-home/", | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "all present", | ||
env: map[string]string{ | ||
"BUILD_URL": "http://some-build-url/", | ||
"JENKINS_HOME": "http://some-jenkins-home/", | ||
}, | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
s.T().Run(tc.name, func(t *testing.T) { | ||
os.Unsetenv("BUILD_URL") | ||
os.Unsetenv("JENKINS_HOME") | ||
|
||
for k, v := range tc.env { | ||
t.Setenv(k, v) | ||
} | ||
|
||
s.Equal(tc.want, s.runner.CheckEnv()) | ||
}) | ||
} | ||
} | ||
|
||
func (s *jenkinsJobSuite) TestListEnvVars() { | ||
s.Equal([]string{ | ||
"JOB_NAME", | ||
"BUILD_URL", | ||
"AGENT_WORKDIR", | ||
"NODE_NAME", | ||
}, s.runner.ListEnvVars()) | ||
} | ||
|
||
func (s *jenkinsJobSuite) TestResolveEnvVars() { | ||
s.Equal(jenkinsJobTestingEnvVars, s.runner.ResolveEnvVars()) | ||
} | ||
|
||
func (s *jenkinsJobSuite) TestRunURI() { | ||
s.Equal("http://some-build-url/", s.runner.RunURI()) | ||
} | ||
|
||
func (s *jenkinsJobSuite) TestRunnerName() { | ||
s.Equal("jenkins-job", s.runner.String()) | ||
} | ||
|
||
// Run before each test | ||
func (s *jenkinsJobSuite) SetupTest() { | ||
s.runner = NewJenkinsJob() | ||
t := s.T() | ||
for k, v := range jenkinsJobTestingEnvVars { | ||
t.Setenv(k, v) | ||
} | ||
} | ||
|
||
var jenkinsJobTestingEnvVars = map[string]string{ | ||
"JOB_NAME": "some-jenkins-job", | ||
"BUILD_URL": "http://some-build-url/", | ||
"AGENT_WORKDIR": "/home/sample/agent", | ||
"NODE_NAME": "some-node", | ||
} | ||
|
||
// Run the tests | ||
func TestJenkinsJobRunner(t *testing.T) { | ||
suite.Run(t, new(jenkinsJobSuite)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not related to your change, since the other runners have the same issue, but it's something that we could tackle in another patch.
It seems that the "factory functions" are not being used outside of tests, when they could also be used in
runner.go
in the switch clause.