Skip to content
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

[PoC] Add support for Jenkins #12489

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ linters:
disable-all: true
enable:
- unused
- exportloopref
- copyloopvar
- gosimple
- govet
- ineffassign
Expand Down
1 change: 0 additions & 1 deletion cmd/external-plugins/automated-approver/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ func (ac *ApproveCondition) checkChangedFiles(logger *zap.SugaredLogger, changes
defer logger.Sync()
logger.Debugf("Checking if PR changed only allowed files: %v", ac.ChangedFiles)
for _, change := range changes {
change := change
logger.Debugf("Checking file: %s", change.Filename)
matched := slices.ContainsFunc(ac.ChangedFiles, func(allowedFile string) bool {
filesMatcher := regexp.MustCompile(allowedFile)
Expand Down
61 changes: 61 additions & 0 deletions cmd/image-builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"slices"
"strconv"

Expand All @@ -21,6 +22,7 @@ const (
Prow CISystem = "Prow"
GithubActions CISystem = "GithubActions"
AzureDevOps CISystem = "AzureDevOps"
Jenkins CISystem = "Jenkins"
)

type Config struct {
Expand Down Expand Up @@ -155,6 +157,8 @@ func LoadGitStateConfig(ciSystem CISystem) (GitStateConfig, error) {
// Load from env specific for Github Actions
case GithubActions:
return loadGithubActionsGitState()
case Jenkins:
return loadJenkinsGitState()
default:
// Unknown CI System, return error and empty git state
return GitStateConfig{}, fmt.Errorf("unknown ci system, got %s", ciSystem)
Expand Down Expand Up @@ -326,6 +330,57 @@ func loadGithubActionsGitState() (GitStateConfig, error) {
}
}

func loadJenkinsGitState() (GitStateConfig, error) {
// Load from env specific for Jenkins Jobs
prID, isPullRequest := os.LookupEnv("CHANGE_ID")
gitURL := os.Getenv("GIT_URL")

owner, repo, err := extractOwnerAndRepoFromGitURL(gitURL)
if err != nil {
return GitStateConfig{}, fmt.Errorf("failed to extract owner and repository from git URL %s: %w", gitURL, err)
}

baseCommitSHA := os.Getenv("GIT_COMMIT")

gitState := GitStateConfig{
RepositoryName: repo,
RepositoryOwner: owner,
JobType: "postsubmit",
BaseCommitSHA: baseCommitSHA,
}

if isPullRequest {
pullNumber, err := strconv.Atoi(prID)
if err != nil {
return GitStateConfig{}, fmt.Errorf("failed to parse CHANGE_ID environment variable: %w", err)
}

baseRef := os.Getenv("CHANGE_BRANCH")
pullRequestHeadSHA := os.Getenv("CHANGE_HEAD_SHA")

gitState.JobType = "presubmit"
gitState.PullRequestNumber = pullNumber
gitState.BaseCommitRef = baseRef
gitState.PullHeadCommitSHA = pullRequestHeadSHA
gitState.isPullRequest = true
}

return gitState, nil
}

func extractOwnerAndRepoFromGitURL(gitURL string) (string, string, error) {
re := regexp.MustCompile(`.*/(.*)/(.*)`)

matches := re.FindStringSubmatch(gitURL)
fmt.Println(matches, gitURL)

if len(matches) != 3 {
return "", "", fmt.Errorf("failed to extract owner and repository from git URL")
}

return matches[1], matches[2], nil
}

// DetermineUsedCISystem return CISystem bind to system in which image builder is running or error if unknown
// It is used to avoid getting env variables in multiple parts of image builder
func DetermineUsedCISystem() (CISystem, error) {
Expand Down Expand Up @@ -358,5 +413,11 @@ func determineUsedCISystem(envGetter func(key string) string, envLookup func(key
return AzureDevOps, nil
}

// JENKINS_HOME environment variable is set in Jenkins
_, isJenkins := envLookup("JENKINS_HOME")
if isJenkins {
return Jenkins, nil
}

return "", fmt.Errorf("cannot determine ci system: unknown system")
}
48 changes: 48 additions & 0 deletions cmd/image-builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,47 @@ func TestLoadGitStateConfig(t *testing.T) {
PullHeadCommitSHA: "e47034172c36d3e5fb407b5ba57adf0f7868599d",
},
},
{
name: "load data from push event for jenkins",
options: options{
ciSystem: Jenkins,
},
env: map[string]string{
"BRANCH_NAME": "refs/heads/main",
"JENKINS_HOME": "/some/absolute/path",
"GIT_URL": "github.com/kyma-project/test-infra",
"GIT_COMMIT": "1234",
},
gitState: GitStateConfig{
RepositoryName: "test-infra",
RepositoryOwner: "kyma-project",
JobType: "postsubmit",
BaseCommitSHA: "1234",
},
},
{
name: "load data from pull request event for jenkins",
options: options{
ciSystem: Jenkins,
},
env: map[string]string{
"BRANCH_NAME": "refs/heads/main",
"JENKINS_HOME": "/some/absolute/path",
"CHANGE_ID": "14",
"GIT_URL": "github.com/kyma-project/test-infra",
"GIT_COMMIT": "1234",
"CHANGE_HEAD_SHA": "4321", // Must be explicitly set when calling docker run
},
gitState: GitStateConfig{
RepositoryName: "test-infra",
RepositoryOwner: "kyma-project",
JobType: "presubmit",
BaseCommitSHA: "1234",
PullRequestNumber: 14,
PullHeadCommitSHA: "4321",
isPullRequest: true,
},
},
}

for _, c := range tc {
Expand Down Expand Up @@ -436,6 +477,13 @@ func Test_determineCISystem(t *testing.T) {
},
ciSystem: GithubActions,
},
{
name: "detect running in jenkins",
env: mockEnv{
"JENKINS_HOME": "/some/absolute/path",
},
ciSystem: Jenkins,
},
{
name: "unknown ci system",
env: mockEnv{
Expand Down
3 changes: 3 additions & 0 deletions cmd/image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,12 @@ func main() {
os.Exit(0)
}
if o.buildInADO {
fmt.Printf("Git State1: %v\n", o.gitState.JobType)
err = buildInADO(o)
fmt.Printf("Git State2: %v\n", o.gitState.JobType)
if err != nil {
fmt.Printf("Image build failed with error: %s\n", err)
fmt.Printf("Git State3: %v\n", o.gitState.JobType)
os.Exit(1)
}
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion cmd/oidc-token-verifier/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22.3-alpine as builder
FROM golang:1.23-alpine as builder

# Add certificate authorities certificates.
# This let oidctokenverifier to use tls connection to the OIDC provider.
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/kyma-project/test-infra

go 1.22.0
go 1.23

toolchain go1.23.0

require (
Expand Down
Loading