diff --git a/agone_jenkins b/agone_jenkins index a711060..25f9061 100644 --- a/agone_jenkins +++ b/agone_jenkins @@ -2,10 +2,17 @@ pipeline { agent any environment { -COVERITY_URL = 'https://coverity.etechtips.com' + COVERITY_URL = 'https://coverity.etechtips.com' COVERITY_STREAM = 'agone-full' + COVERITY_PR_STREAM = 'agone-pr' // Add Coverity binaries to PATH if not globally set on the Jenkins agent PATH = "/opt/coverity/analysis/bin:${env.PATH}" + + // Full-build baseline artifact + COVERITY_BASELINE_ARCHIVE = 'agone-full-cov-int.tar.gz' + + //For a Multibranch Pipeline + AGONE_FULL_JOB = 'agone/main' } options { @@ -49,14 +56,29 @@ bazel build --spawn_strategy=standalone //:run passwordVariable: 'COV_PASSWORD', usernameVariable: 'COV_USER' )]) { - sh """ + sh ''' cov-commit-defects --dir cov-int \ - --url ${COVERITY_URL} \ - --stream ${COVERITY_STREAM} \ - --user ${COV_USER} \ - --password ${COV_PASSWORD} - """ + --url "${COVERITY_URL}" \ + --stream "${COVERITY_STREAM}" \ + --user "${COV_USER}" \ + --password "${COV_PASSWORD}" + ''' } + // 5. Package the completed full-build intermediate directory + sh ''' + rm -f "${COVERITY_BASELINE_ARCHIVE}" + tar -czf "${COVERITY_BASELINE_ARCHIVE}" cov-int + test -s "${COVERITY_BASELINE_ARCHIVE}" + + ''' + + // 6. Store Baseline as a jenkins artifact + archiveArtifacts ( + artifacts: "${COVERITY_BASELINE_ARCHIVE}", + fingerprint: true, + onlyIfSuccessful: true + ) + } } } @@ -75,14 +97,23 @@ bazel build --spawn_strategy=standalone //:run script { echo "Starting agone-pr build..." echo "The current branch is: ${env.BRANCH_NAME}" - + // 1. Determine target branch and fetch changes def targetBranch = env.CHANGE_TARGET ?: 'main' - sh "git fetch origin ${targetBranch}" - // 2. Identify changed files relative to the target branch + sh """ + git fetch origin \ + +refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch} + """ + def mergeBase = sh( + script: "git merge-base HEAD origin/${targetBranch}", + returnStdout: true + ).trim() + + echo "Merge base: ${mergeBase}" + def changedFiles = sh( - script: "git diff --name-only origin/${targetBranch}...HEAD", + script: "git diff --name-only ${mergeBase} HEAD", returnStdout: true ).trim() @@ -109,22 +140,38 @@ bazel build --spawn_strategy=standalone //:run echo "Affected Bazel targets:\n${affectedTargets}" - // 4. Clean PR-specific Coverity directory - sh 'rm -rf cov-int-pr' + copyArtifacts ( + projectName: "${AGONE_FULL_JOB}", + selector: last_successful(), + filter: "${COVERITY_BASELINE_ARCHIVE}", + fingerprintArtifacts: true + ) + + // 4a. recover nightly pr + sh ''' + rm -rf cov-int-pr baseline-cov-int + mkdir baseline-cov-int + tar -xzf "${COVERITY_BASELINE_ARCHIVE}" \ + -C baseline-cov-int + test -d baseline-cov-int/cov-int + + mv baseline-cov-int/cov-int cov-int-pr + rmdir baseline-cov-int + ''' // 5. Build only the affected targets wrapped in cov-build sh """ - cov-build --dir cov-int-pr \ + cov-build --dir cov-int-pr --bazel \ bazel build --spawn_strategy=standalone ${affectedTargets.replaceAll('\n', ' ')} """ // 6. Run fast desktop analysis on only changed files - sh """ + sh ''' cov-run-desktop --dir cov-int-pr \ - --target-url ${COVERITY_URL} \ - --stream ${COVERITY_STREAM} \ + --target-url "${COVERITY_URL}" \ + --stream "${COVERITY_PR_STREAM}" \ ${changedFiles.replaceAll('\n', ' ')} - """ + ''' } } }