Skip to content

Commit

Permalink
Refactor Jenkinsfile to improve e2eignore file handling and streamlin…
Browse files Browse the repository at this point in the history
…e build process
  • Loading branch information
ptankov committed Jan 11, 2025
1 parent 643f307 commit 1b27819
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -288,58 +288,59 @@ void checkE2EIgnoreFiles() {
}

def e2eignoreFile = ".e2eignore"
if (fileExists(e2eignoreFile)) {
def excludedFiles = readFile(e2eignoreFile).split('\n').collect{it.trim()}
def lastProcessedCommitFile="last-processed-commit.txt"
def lastProcessedCommitHash = ""

def build = currentBuild.previousBuild
while (build != null) {
try {
echo "Checking previous build: #$build.number"
copyArtifacts(projectName: env.JOB_NAME, selector: specific("$build.number"), filter: lastProcessedCommitFile)
lastProcessedCommitHash = readFile(lastProcessedCommitFile).trim()
echo "Last processed commit hash: $lastProcessedCommitHash"
break
} catch (Exception e) {
echo "No $lastProcessedCommitFile found in build $build.number. Checking earlier builds."
}
build = build.previousBuild
}
if ( ! fileExists(e2eignoreFile) ) {
echo "No $e2eignoreFile file found. Proceeding with execution."
return
}

if (lastProcessedCommitHash == "") {
echo "This is the first run. Using merge base as the starting point for the diff."
changedFiles = sh(script: "git diff --name-only \$(git merge-base HEAD origin/$CHANGE_TARGET)", returnStdout: true).trim().split('\n').findAll{it}
} else {
echo "Processing changes since last processed commit: $lastProcessedCommitHash"
changedFiles = sh(script: "git diff --name-only $lastProcessedCommitHash HEAD", returnStdout: true).trim().split('\n').findAll{it}
def excludedFiles = readFile(e2eignoreFile).split('\n').collect{it.trim()}
def lastProcessedCommitFile = "last-processed-commit.txt"
def lastProcessedCommitHash = ""

def build = currentBuild.previousBuild
while (build != null) {
try {
echo "Checking previous build: #$build.number"
copyArtifacts(projectName: env.JOB_NAME, selector: specific("$build.number"), filter: lastProcessedCommitFile)
lastProcessedCommitHash = readFile(lastProcessedCommitFile).trim()
echo "Last processed commit hash: $lastProcessedCommitHash"
break
} catch (Exception e) {
echo "No $lastProcessedCommitFile found in build $build.number. Checking earlier builds."
}
build = build.previousBuild
}

if (lastProcessedCommitHash == "") {
echo "This is the first run. Using merge base as the starting point for the diff."
changedFiles = sh(script: "git diff --name-only \$(git merge-base HEAD origin/$CHANGE_TARGET)", returnStdout: true).trim().split('\n').findAll{it}
} else {
echo "Processing changes since last processed commit: $lastProcessedCommitHash"
changedFiles = sh(script: "git diff --name-only $lastProcessedCommitHash HEAD", returnStdout: true).trim().split('\n').findAll{it}
}

echo "Excluded files: $excludedFiles"
echo "Changed files: $changedFiles"
echo "Excluded files: $excludedFiles"
echo "Changed files: $changedFiles"

def excludedFilesRegex = excludedFiles.collect{it.replace("**", ".*").replace("*", "[^/]*")}
needToRunTests = !changedFiles.every{changed -> excludedFilesRegex.any{regex -> changed ==~ regex}}
def excludedFilesRegex = excludedFiles.collect{it.replace("**", ".*").replace("*", "[^/]*")}
needToRunTests = !changedFiles.every{changed -> excludedFilesRegex.any{regex -> changed ==~ regex}}

if (needToRunTests) {
echo "Some changed files are outside of the e2eignore list. Proceeding with execution."
if (needToRunTests) {
echo "Some changed files are outside of the e2eignore list. Proceeding with execution."
} else {
if (currentBuild.previousBuild?.result in ['FAILURE', 'ABORTED', 'UNSTABLE']) {
echo "All changed files are e2eignore files, and previous build was unsuccessful. Propagating previous state."
currentBuild.result = currentBuild.previousBuild?.result
error "Skipping execution as non-significant changes detected and previous build was unsuccessful."
} else {
if (currentBuild.previousBuild?.result in ['FAILURE', 'ABORTED', 'UNSTABLE']) {
echo "All changed files are e2eignore files, and previous build was unsuccessful. Propagating previous state."
currentBuild.result = currentBuild.previousBuild?.result
error "Skipping execution as non-significant changes detected and previous build was unsuccessful."
} else {
echo "All changed files are e2eignore files. Aborting pipeline execution."
}
echo "All changed files are e2eignore files. Aborting pipeline execution."
}

sh """
echo \$(git rev-parse HEAD) > $lastProcessedCommitFile
"""
archiveArtifacts "$lastProcessedCommitFile"
} else {
echo "No $e2eignoreFile file found. Proceeding with execution."
}

sh """
echo \$(git rev-parse HEAD) > $lastProcessedCommitFile
"""
archiveArtifacts "$lastProcessedCommitFile"
}

def isPRJob = false
Expand Down

0 comments on commit 1b27819

Please sign in to comment.