Skip to content

Commit 0ff6fe6

Browse files
authored
feat(runner): Integrate runner for Jenkins jobs (#411)
Signed-off-by: Mattia Buccarella <[email protected]>
1 parent 01e1902 commit 0ff6fe6

File tree

7 files changed

+244
-43
lines changed

7 files changed

+244
-43
lines changed

app/controlplane/api/gen/frontend/workflowcontract/v1/crafting_schema.ts

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/api/workflowcontract/v1/crafting_schema.pb.go

Lines changed: 47 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/api/workflowcontract/v1/crafting_schema.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ message CraftingSchema {
4444
GITHUB_ACTION = 1;
4545
GITLAB_PIPELINE = 2;
4646
AZURE_PIPELINE = 3;
47+
JENKINS_JOB = 4;
4748
}
4849
}
4950

internal/attestation/crafter/runner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func NewRunner(t schemaapi.CraftingSchema_Runner_RunnerType) supportedRunner {
4444
return &runners.GitlabPipeline{}
4545
case schemaapi.CraftingSchema_Runner_AZURE_PIPELINE:
4646
return &runners.AzurePipeline{}
47+
case schemaapi.CraftingSchema_Runner_JENKINS_JOB:
48+
return &runners.JenkinsJob{}
4749
default:
4850
return &runners.Generic{}
4951
}

internal/attestation/crafter/runners/generic.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ func (r *Generic) CheckEnv() bool {
2727
return true
2828
}
2929

30+
// Returns a list of environment variables names. This list is used to
31+
// automatically inject environment variables into the attestation.
3032
func (r *Generic) ListEnvVars() []string {
3133
return []string{}
3234
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// Copyright 2023 The Chainloop Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package runners
17+
18+
import "os"
19+
20+
type JenkinsJob struct{}
21+
22+
const JenkinsJobID = "jenkins-job"
23+
24+
func NewJenkinsJob() *JenkinsJob {
25+
return &JenkinsJob{}
26+
}
27+
28+
// Checks whether we are within a Jenkins job
29+
func (r *JenkinsJob) CheckEnv() bool {
30+
for _, envVarName := range []string{"JENKINS_HOME", "BUILD_URL"} {
31+
if os.Getenv(envVarName) == "" {
32+
return false
33+
}
34+
}
35+
36+
return true
37+
}
38+
39+
func (r *JenkinsJob) ListEnvVars() []string {
40+
return []string{
41+
// Some info about the job
42+
"JOB_NAME",
43+
"BUILD_URL",
44+
45+
// Some info about the commit (Jenkins Git Plugin)
46+
// NOTE: Commenting these vars out because they are not always present
47+
// and current chainloop behavior requires these to be set.
48+
// "GIT_BRANCH",
49+
// "GIT_COMMIT",
50+
51+
// Some info about the agent
52+
"AGENT_WORKDIR",
53+
"NODE_NAME",
54+
}
55+
}
56+
57+
func (r *JenkinsJob) ResolveEnvVars() map[string]string {
58+
return resolveEnvVars(r.ListEnvVars())
59+
}
60+
61+
func (r *JenkinsJob) String() string {
62+
return JenkinsJobID
63+
}
64+
65+
func (r *JenkinsJob) RunURI() string {
66+
return os.Getenv("BUILD_URL")
67+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//
2+
// Copyright 2023 The Chainloop Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package runners
17+
18+
import (
19+
"os"
20+
"testing"
21+
22+
"github.com/stretchr/testify/suite"
23+
)
24+
25+
type jenkinsJobSuite struct {
26+
suite.Suite
27+
runner *JenkinsJob
28+
}
29+
30+
func (s *jenkinsJobSuite) TestCheckEnv() {
31+
testCases := []struct {
32+
name string
33+
env map[string]string
34+
want bool
35+
}{
36+
{
37+
name: "empty",
38+
env: map[string]string{},
39+
want: false,
40+
},
41+
{
42+
name: "missing JENKINS_HOME",
43+
env: map[string]string{
44+
"BUILD_URL": "http://some-build-url/",
45+
},
46+
want: false,
47+
},
48+
{
49+
name: "missing BUILD_URL",
50+
env: map[string]string{
51+
"JENKINS_HOME": "http://some-jenkins-home/",
52+
},
53+
want: false,
54+
},
55+
{
56+
name: "all present",
57+
env: map[string]string{
58+
"BUILD_URL": "http://some-build-url/",
59+
"JENKINS_HOME": "http://some-jenkins-home/",
60+
},
61+
want: true,
62+
},
63+
}
64+
65+
for _, tc := range testCases {
66+
s.T().Run(tc.name, func(t *testing.T) {
67+
os.Unsetenv("BUILD_URL")
68+
os.Unsetenv("JENKINS_HOME")
69+
70+
for k, v := range tc.env {
71+
t.Setenv(k, v)
72+
}
73+
74+
s.Equal(tc.want, s.runner.CheckEnv())
75+
})
76+
}
77+
}
78+
79+
func (s *jenkinsJobSuite) TestListEnvVars() {
80+
s.Equal([]string{
81+
"JOB_NAME",
82+
"BUILD_URL",
83+
"AGENT_WORKDIR",
84+
"NODE_NAME",
85+
}, s.runner.ListEnvVars())
86+
}
87+
88+
func (s *jenkinsJobSuite) TestResolveEnvVars() {
89+
s.Equal(jenkinsJobTestingEnvVars, s.runner.ResolveEnvVars())
90+
}
91+
92+
func (s *jenkinsJobSuite) TestRunURI() {
93+
s.Equal("http://some-build-url/", s.runner.RunURI())
94+
}
95+
96+
func (s *jenkinsJobSuite) TestRunnerName() {
97+
s.Equal("jenkins-job", s.runner.String())
98+
}
99+
100+
// Run before each test
101+
func (s *jenkinsJobSuite) SetupTest() {
102+
s.runner = NewJenkinsJob()
103+
t := s.T()
104+
for k, v := range jenkinsJobTestingEnvVars {
105+
t.Setenv(k, v)
106+
}
107+
}
108+
109+
var jenkinsJobTestingEnvVars = map[string]string{
110+
"JOB_NAME": "some-jenkins-job",
111+
"BUILD_URL": "http://some-build-url/",
112+
"AGENT_WORKDIR": "/home/sample/agent",
113+
"NODE_NAME": "some-node",
114+
}
115+
116+
// Run the tests
117+
func TestJenkinsJobRunner(t *testing.T) {
118+
suite.Run(t, new(jenkinsJobSuite))
119+
}

0 commit comments

Comments
 (0)