From 5c4a90411145be96b1a5d4b8366dd193ebc581ec Mon Sep 17 00:00:00 2001 From: Santhosh Kesavan Date: Fri, 30 Jan 2026 23:34:56 +0530 Subject: [PATCH 1/3] SDEVX-9774: resolved the twice pipeline trigger --- .gitlab-ci.yml | 7 +++++ cancelOldPipeline.js | 70 ++++++++++++++++++++++++++++++++++++++++++++ utility/service.js | 38 +++++++++++++++++++++++- 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 cancelOldPipeline.js diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index da08cdf..a3c2ad1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,7 @@ image: veracode/scm-packaging:3.0.0 stages: + - Cancel Old Pipelines - Pre Checks - Build - Veracode Scan @@ -19,6 +20,12 @@ variables: - $ARTIFACTS_FOLDER when: always +Cancel Old Pipelines: + stage: Cancel Old Pipelines + script: + - npm i + - node ./cancelOldPipeline.js + Cli Version Check: stage : Pre Checks image : alpine:latest diff --git a/cancelOldPipeline.js b/cancelOldPipeline.js new file mode 100644 index 0000000..9e687d4 --- /dev/null +++ b/cancelOldPipeline.js @@ -0,0 +1,70 @@ +const { fetchAllPipelines, getPipelineVariables, cancelPipeline } = require("./utility/service"); + +const hostName = process.env.CI_SERVER_HOST; +const projectId = process.env.CI_PROJECT_ID; +const pipelineName = process.env.PIPELINE_NAME; +const sourceBranch = process.env.SOURCE_BRANCH; +const currentPipelineId = process.env.CI_PIPELINE_ID; +const currentPipelineCreatedAt = process.env.CI_PIPELINE_CREATED_AT; + + +async function cancelOldPipeline() { + try { + console.log(`Using pipeline name: ${pipelineName}`); + console.log(`Current branch: ${sourceBranch}`); + + // Fetch running pipelines + const pipelines = await fetchAllPipelines(hostName, projectId); + + if (!pipelines || pipelines.length === 0) { + console.log("No running pipelines found. Nothing to cancel."); + return; + } + + // Filter pipelines by name + const matchingPipelines = pipelines.filter(p => + p.name && p.name.toLowerCase() === pipelineName.toLowerCase() + ); + + if (matchingPipelines.length === 0) { + console.log("No matching pipelines found. Nothing to cancel."); + return; + } + + for (const pipeline of matchingPipelines) { + const pipelineId = pipeline.id; + + // Skip current pipeline itself + if (pipelineId === Number(currentPipelineId)) { + console.log(`Skipping current pipeline ${pipelineId}`); + continue; + } + + // Convert current pipeline creation time to epoch milliseconds + const currentEpoch = new Date(currentPipelineCreatedAt).getTime(); + const createdEpoch = new Date(pipeline.created_at).getTime(); + + // Skip newer pipelines + if (createdEpoch > currentEpoch) { + console.log(`Skipping newer pipeline ${pipelineId} created at ${pipeline.created_at}`); + continue; + } + + // Get pipeline variables + const vars = await getPipelineVariables(hostName, projectId, pipelineId) + + const pipelineBranch = vars.find(v => v.key === "SOURCE_BRANCH")?.value; + + if (pipelineBranch === sourceBranch) { + console.log(`Cancelling older pipeline ${pipelineId} created at ${pipeline.created_at}`); + await cancelPipeline(hostName, projectId, pipelineId) + } else { + console.log(`Pipeline ${pipelineId} branch ${pipelineBranch} does not match current branch ${sourceBranch}, skipping`); + } + } + } catch (error) { + console.log("Error cancelling pipelines:", error.response?.data || error.message); + } +} + +cancelOldPipeline(); diff --git a/utility/service.js b/utility/service.js index 25b9211..3848cd4 100644 --- a/utility/service.js +++ b/utility/service.js @@ -124,4 +124,40 @@ async function createComment(projectUrl, mergeRequestId, eventName, commitSha, f } } -module.exports = {checkLabelExists, createLabels, createIssue, listExistingOpenIssues, createWikiPage, createComment} \ No newline at end of file +async function fetchAllPipelines(hostName, veracodeProjectId) { + try { + const url = `https://${hostName}/api/v4/projects/${veracodeProjectId}/pipelines` + const response = await axios.get(url, { + ...headers, + params: { status: "running", per_page: 100 } + }); + return response.data; + } catch (error) { + console.log("Error while fetching all pipelines", error.response?.data || error.message); + return []; + } +} + +async function getPipelineVariables(hostName, veracodeProjectId, pipelineId) { + try { + const url = `https://${hostName}/api/v4/projects/${veracodeProjectId}/pipelines/${pipelineId}/variables` + const response = await axios.get(url, headers); + return response.data; + } catch (error) { + console.log("Error while fetching pipeline variable", error.response?.data || error.message); + return []; + } +} + +async function cancelPipeline(hostName, veracodeProjectId, pipelineId) { + try { + const url = `https://${hostName}/api/v4/projects/${veracodeProjectId}/pipelines/${pipelineId}/cancel` + const response = await axios.post(url, {}, headers); + return response.data; + } catch (error) { + console.log("Error while fetching pipeline variable", error.response?.data || error.message); + return null; + } +} + +module.exports = {checkLabelExists, createLabels, createIssue, listExistingOpenIssues, createWikiPage, createComment, fetchAllPipelines, getPipelineVariables, cancelPipeline} \ No newline at end of file From 073dbdd7237cc9128c045af6f37425d748eaa870 Mon Sep 17 00:00:00 2001 From: Santhosh Kesavan Date: Fri, 30 Jan 2026 23:46:18 +0530 Subject: [PATCH 2/3] SDEVX-9774: resolved the twice pipeline trigger --- veracode.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/veracode.yml b/veracode.yml index 3c64803..1cfc039 100644 --- a/veracode.yml +++ b/veracode.yml @@ -1,5 +1,3 @@ -debug: - false veracode_static_scan: push: trigger: true From 808a6c9d933c8ab935ec50597a22354c81ebf47b Mon Sep 17 00:00:00 2001 From: Santhosh Kesavan Date: Mon, 2 Feb 2026 16:32:44 +0530 Subject: [PATCH 3/3] SDEVX-9774: resolved the twice pipeline trigger --- .gitlab-ci.yml | 146 +++++++++++++++++++++---------------------- cancelOldPipeline.js | 14 +---- utility/service.js | 33 ++++++---- 3 files changed, 95 insertions(+), 98 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a3c2ad1..a400650 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,6 @@ image: veracode/scm-packaging:3.0.0 stages: - - Cancel Old Pipelines - Pre Checks - Build - Veracode Scan @@ -20,85 +19,82 @@ variables: - $ARTIFACTS_FOLDER when: always -Cancel Old Pipelines: - stage: Cancel Old Pipelines - script: - - npm i - - node ./cancelOldPipeline.js - Cli Version Check: stage : Pre Checks image : alpine:latest before_script: - - apk add --no-cache curl git - script: | - # Define installation directory and version file path - export CLI_FOLDER="$(pwd)/veracode-cli" - export VERSION_FILE="$CLI_FOLDER/VERSION" - mkdir -p "${CLI_FOLDER}" - - # Fetch latest Veracode CLI version - echo "Fetching the latest Veracode CLI version" - export LATEST_CLI_VERSION=$(curl -s https://tools.veracode.com/veracode-cli/LATEST_VERSION) - - # Validate the fetched version - if [[ -z "$LATEST_CLI_VERSION" ]]; then - echo "Error: Failed to fetch latest version" - exit 1 - fi - echo "LATEST CLI VERSION: ${LATEST_CLI_VERSION}" - - # Get the locally installed version (if exists) - if [[ -f "${VERSION_FILE}" ]]; then - export LOCAL_CLI_VERSION=$(cat "${VERSION_FILE}") - echo "LOCAL CLI VERSION: ${LOCAL_CLI_VERSION}" - else - export LOCAL_CLI_VERSION="NONE" # Default if no version exists - fi - - # Compare versions and update only if they are different - if [[ "$LOCAL_CLI_VERSION" != "$LATEST_CLI_VERSION" ]]; then - echo "New version detected. Downloading Veracode CLI..." - - export CLI_DOWNLOAD_URL="https://tools.veracode.com/veracode-cli/veracode-cli_${LATEST_CLI_VERSION}_linux_x86.tar.gz" - echo "CLI DOWNLOAD URL: ${CLI_DOWNLOAD_URL}" - - # Download and extract the CLI - curl -s -L "${CLI_DOWNLOAD_URL}" -o "${CLI_FOLDER}/veracode-cli.tar.gz" - tar -xzf "${CLI_FOLDER}/veracode-cli.tar.gz" -C "${CLI_FOLDER}" - - # Move the extracted files into the install directory (flatten structure) - mv "$CLI_FOLDER/veracode-cli_${LATEST_CLI_VERSION}_linux_x86"/* "$CLI_FOLDER" - - # Clean up the tarball and extracted directory - rm -rf "$CLI_FOLDER/veracode-cli_${LATEST_CLI_VERSION}_linux_x86" - rm -f "$CLI_FOLDER/veracode-cli.tar.gz" - - # Verify the extracted files - echo "Files in install directory:" - ls -l "$CLI_FOLDER" - - echo "Setting up Git configuration" - git config --global user.name "veracode" - git config --global user.email "cli@veracode.com" - - git remote set-url origin "https://gitlab-ci-token:${PRIVATE_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git" - - echo "Checkout main" - git checkout main 2>/dev/null || echo "Conflicts prevent checkout, Continuing..." # Ensure we are on the main branch - - echo "git pull origin main" - if git pull origin main; then - git add "$CLI_FOLDER/*" - git commit -m "Update Veracode CLI version to ${LATEST_CLI_VERSION}" - echo "push origin main" - git push origin main || (git pull --rebase origin main && git push origin main) || echo "Push still failed, Due to other pipline updates" + - apk add --no-cache nodejs npm curl git + script: + - npm i + - node ./cancelOldPipeline.js + - | + # Define installation directory and version file path + export CLI_FOLDER="$(pwd)/veracode-cli" + export VERSION_FILE="$CLI_FOLDER/VERSION" + mkdir -p "${CLI_FOLDER}" + + # Fetch latest Veracode CLI version + echo "Fetching the latest Veracode CLI version" + export LATEST_CLI_VERSION=$(curl -s https://tools.veracode.com/veracode-cli/LATEST_VERSION) + + # Validate the fetched version + if [[ -z "$LATEST_CLI_VERSION" ]]; then + echo "Error: Failed to fetch latest version" + exit 1 + fi + echo "LATEST CLI VERSION: ${LATEST_CLI_VERSION}" + + # Get the locally installed version (if exists) + if [[ -f "${VERSION_FILE}" ]]; then + export LOCAL_CLI_VERSION=$(cat "${VERSION_FILE}") + echo "LOCAL CLI VERSION: ${LOCAL_CLI_VERSION}" + else + export LOCAL_CLI_VERSION="NONE" # Default if no version exists + fi + + # Compare versions and update only if they are different + if [[ "$LOCAL_CLI_VERSION" != "$LATEST_CLI_VERSION" ]]; then + echo "New version detected. Downloading Veracode CLI..." + + export CLI_DOWNLOAD_URL="https://tools.veracode.com/veracode-cli/veracode-cli_${LATEST_CLI_VERSION}_linux_x86.tar.gz" + echo "CLI DOWNLOAD URL: ${CLI_DOWNLOAD_URL}" + + # Download and extract the CLI + curl -s -L "${CLI_DOWNLOAD_URL}" -o "${CLI_FOLDER}/veracode-cli.tar.gz" + tar -xzf "${CLI_FOLDER}/veracode-cli.tar.gz" -C "${CLI_FOLDER}" + + # Move the extracted files into the install directory (flatten structure) + mv "$CLI_FOLDER/veracode-cli_${LATEST_CLI_VERSION}_linux_x86"/* "$CLI_FOLDER" + + # Clean up the tarball and extracted directory + rm -rf "$CLI_FOLDER/veracode-cli_${LATEST_CLI_VERSION}_linux_x86" + rm -f "$CLI_FOLDER/veracode-cli.tar.gz" + + # Verify the extracted files + echo "Files in install directory:" + ls -l "$CLI_FOLDER" + + echo "Setting up Git configuration" + git config --global user.name "veracode" + git config --global user.email "cli@veracode.com" + + git remote set-url origin "https://gitlab-ci-token:${PRIVATE_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git" + + echo "Checkout main" + git checkout main 2>/dev/null || echo "Conflicts prevent checkout, Continuing..." # Ensure we are on the main branch + + echo "git pull origin main" + if git pull origin main; then + git add "$CLI_FOLDER/*" + git commit -m "Update Veracode CLI version to ${LATEST_CLI_VERSION}" + echo "push origin main" + git push origin main || (git pull --rebase origin main && git push origin main) || echo "Push still failed, Due to other pipline updates" + else + echo "Veracode CLI is already exists." + fi else - echo "Veracode CLI is already exists." - fi - else - echo "Veracode CLI is already up to date. No update required." - fi + echo "Veracode CLI is already up to date. No update required." + fi only: - main artifacts: diff --git a/cancelOldPipeline.js b/cancelOldPipeline.js index 9e687d4..711423a 100644 --- a/cancelOldPipeline.js +++ b/cancelOldPipeline.js @@ -14,24 +14,14 @@ async function cancelOldPipeline() { console.log(`Current branch: ${sourceBranch}`); // Fetch running pipelines - const pipelines = await fetchAllPipelines(hostName, projectId); + const pipelines = await fetchAllPipelines(hostName, projectId, pipelineName); if (!pipelines || pipelines.length === 0) { console.log("No running pipelines found. Nothing to cancel."); return; } - // Filter pipelines by name - const matchingPipelines = pipelines.filter(p => - p.name && p.name.toLowerCase() === pipelineName.toLowerCase() - ); - - if (matchingPipelines.length === 0) { - console.log("No matching pipelines found. Nothing to cancel."); - return; - } - - for (const pipeline of matchingPipelines) { + for (const pipeline of pipelines) { const pipelineId = pipeline.id; // Skip current pipeline itself diff --git a/utility/service.js b/utility/service.js index 3848cd4..0b2eb59 100644 --- a/utility/service.js +++ b/utility/service.js @@ -124,18 +124,29 @@ async function createComment(projectUrl, mergeRequestId, eventName, commitSha, f } } -async function fetchAllPipelines(hostName, veracodeProjectId) { - try { - const url = `https://${hostName}/api/v4/projects/${veracodeProjectId}/pipelines` - const response = await axios.get(url, { - ...headers, - params: { status: "running", per_page: 100 } - }); - return response.data; - } catch (error) { - console.log("Error while fetching all pipelines", error.response?.data || error.message); - return []; +async function fetchAllPipelines(hostName, veracodeProjectId, pipelineName) { + let pipelines = []; + let page = 1; + while(page) { + try { + const url = `https://${hostName}/api/v4/projects/${veracodeProjectId}/pipelines` + const response = await axios.get(url, { + ...headers, + params: { + status: "running", + name: pipelineName, + per_page: 100, + page + } + }); + pipelines.push(...response.data); + page = Number(response.headers['x-next-page']); + } catch (error) { + console.log("Error while fetching all pipelines", error.response?.data || error.message); + break; + } } + return pipelines; } async function getPipelineVariables(hostName, veracodeProjectId, pipelineId) {