From 4913bbd06b9ce73453ff5667f4b195be75adcb0e Mon Sep 17 00:00:00 2001 From: dd Date: Tue, 2 Jul 2024 15:47:09 +0200 Subject: added option to exclude branch --- auto/helper-logic | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ manual/seed-jobs.sh | 19 +++++++++++++++- readme.md | 18 ++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/auto/helper-logic b/auto/helper-logic index acbf773..236035b 100644 --- a/auto/helper-logic +++ b/auto/helper-logic @@ -4,6 +4,7 @@ RED='\033[0;91m' GREEN='\033[0;32m' LIGHTBLUE='\033[0;94m' +GRAY='\033[0;90m' NOCOLOR='\033[0m' # Define color indicators. @@ -36,6 +37,20 @@ if [ "$1" == "--force" ]; then STAGE_CHECK_DISABLED=true fi +# Filters to limit jobs to specific branch +EXCLUDED_DESCRIPTION="" +SELECTED_BRANCH="$BRANCH" +if [ "$BRANCH" == "sagitta" ]; then + EXCLUDED_DESCRIPTION="equuleus-only" + SELECTED_BRANCH_REGEX="(sagitta|current)" +elif [ "$BRANCH" == "equuleus" ]; then + EXCLUDED_DESCRIPTION="sagitta-only" + SELECTED_BRANCH_REGEX="equuleus" +else + >&2 echo -e "${RED}Unknown branch: $SELECTED_BRANCH, please provide valid \$BRANCH (sagitta or equuleus)${NOCOLOR}" + exit 1 +fi + function PrintHeader { # Print banner echo "#################################################" @@ -72,6 +87,11 @@ function PrintErrorIndicator { exit 1 } +function PrintJobExcluded { + tput el + echo -e "[ ${GRAY}Skipped${NOCOLOR} ] Package: $1 - Branch: $2 (excluded)" +} + function PrintJobNotStarted { tput el echo -e "[ ${LIGHTBLUE}Not Started${NOCOLOR} ] Package: $1 - Branch: $2" @@ -222,6 +242,23 @@ function ProvisionJob { BRANCH_REGEX=$(echo "$1" | jq -r .branchRegex) JENKINS_FILE_PATH=$(echo "$1" | jq -r .jenkinsfilePath) + # Branch filter + if [ "$SELECTED_BRANCH" != "" ] && [[ "$BRANCH_REGEX" == *"|"* ]]; then + if [ "$SELECTED_BRANCH" == "sagitta" ]; then + if [[ "$BRANCH_REGEX" == *"current"* ]]; then + if [[ "$BRANCH_REGEX" == *"sagitta"* ]]; then + BRANCH_REGEX="(sagitta|current)" + else + BRANCH_REGEX="current" + fi + else + BRANCH_REGEX="sagitta" + fi + else + BRANCH_REGEX="equuleus" + fi + fi + # Create the job xml file. JOBPATH="$WORKDIR/$JOB_NAME.xml" PROJECT="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" @@ -285,6 +322,13 @@ function ProvisionJobs { JOBS+=( "$JOB_NAME|$BRANCH" ) done + # Branch filter + DESCRIPTION=$(echo "$item" | jq -r .description) + if [ "$DESCRIPTION" == "$EXCLUDED_DESCRIPTION" ]; then + PrintEmptyIndicator "$JOB_NAME (excluded - $DESCRIPTION)" + continue + fi + # Provision the job. ProvisionJob $item done < <(cat $1 | jq -c '.[]') @@ -320,6 +364,12 @@ function ProvisionJobs { JOB_NAME="${jobSplit[0]}" JOB_BRANCH="${jobSplit[1]}" + # Branch filter + if ! echo $JOB_BRANCH | grep -E "$SELECTED_BRANCH_REGEX" > /dev/null; then + PrintJobExcluded $JOB_NAME $JOB_BRANCH + continue + fi + URL="${JENKINS_URL}/job/${JOB_NAME}/job/${JOB_BRANCH}/1/api/json" if [ "$(curl -o /dev/null -s -w "%{http_code}\n" "${URL}")" -eq 404 ]; then @@ -432,6 +482,12 @@ function BuildJobs { JOB_NAME="${jobSplit[0]}" JOB_BRANCH="${jobSplit[1]}" + # Branch filter + if ! echo $JOB_BRANCH | grep -E "$SELECTED_BRANCH_REGEX" > /dev/null; then + PrintJobExcluded $JOB_NAME $JOB_BRANCH + continue + fi + # Download job data and parse it. URL="${JENKINS_URL}/job/${JOB_NAME}/job/${JOB_BRANCH}/lastBuild/api/json" DATA=$(curl -Ss -g --fail-with-body "$URL") @@ -507,6 +563,12 @@ function BuildJobs { JOB_NAME="${jobSplit[0]}" JOB_BRANCH="${jobSplit[1]}" + # Branch filter + if ! echo $JOB_BRANCH | grep -E "$SELECTED_BRANCH_REGEX" > /dev/null; then + PrintJobExcluded $JOB_NAME $JOB_BRANCH + continue + fi + # Download job data and parse it. URL="${JENKINS_URL}/job/${JOB_NAME}/job/${JOB_BRANCH}/lastBuild/api/json" DATA=$(curl -Ss -g --fail-with-body "$URL") diff --git a/manual/seed-jobs.sh b/manual/seed-jobs.sh index 2cf488b..b148a9b 100755 --- a/manual/seed-jobs.sh +++ b/manual/seed-jobs.sh @@ -40,6 +40,7 @@ projectsJobsPath="../jobs/project-jobs.json" jenkinsUser=${jenkinsUser:-$JENKINS_USER} jenkinsToken=${jenkinsToken:-$JENKINS_TOKEN} jenkinsUrl="http://${jenkinsUser}:${jenkinsToken}@$jenkinsHost" +selectedBranch="$BRANCH" mode="$1" availableModes=("create" "build") @@ -60,6 +61,16 @@ echo -n "testing jenkins connection: " get > /dev/null echo "ok" +excludedDescription="" +if [ "$selectedBranch" == "sagitta" ]; then + excludedDescription="equuleus-only" +elif [ "$selectedBranch" == "equuleus" ]; then + excludedDescription="sagitta-only" +else + >&2 echo -e "ERROR: Unknown branch: $selectedBranch, please provide valid \$BRANCH (sagitta or equuleus)" + exit 1 +fi + if [[ "$mode" == "create" ]]; then jobsPath="$workDir/jobs.json" cat "$dockerContainerJobsPath" "$projectsJobsPath" | jq -s 'add' > "$jobsPath" @@ -74,6 +85,11 @@ if [[ "$mode" == "create" ]]; then branchRegex=$(echo "$item" | jq -r .branchRegex) jenkinsfilePath=$(echo "$item" | jq -r .jenkinsfilePath) + if [ "$description" == "$excludedDescription" ]; then + echo " excluded ($description)" + continue + fi + # create job.xml by using jobTemplate.xml jobPath="$workDir/$jobName.xml" project="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" @@ -115,6 +131,7 @@ elif [[ "$mode" == "build" ]]; then done else - echo "ERROR: unknown mode '$mode'" + >&2 echo "ERROR: unknown mode '$mode'" echo "available modes: ${availableModes[*]}" + exit 1 fi diff --git a/readme.md b/readme.md index 0930acb..524a883 100644 --- a/readme.md +++ b/readme.md @@ -80,6 +80,15 @@ mv /tmp/vyos-jenkins-master /opt/vyos-jenkins cd /opt/vyos-jenkins ``` +**If you want to build only specific branch** + +Configure `BRANCH` environment variable to desired branch before you run any script. +Not defined or empty value means all branches (the default). + +```bash +export BRANCH="sagitta" +``` + **Then execute each script and follow instructions:** - `1-prereqs.sh`- installs dependencies. @@ -599,6 +608,15 @@ export JENKINS_USER= export JENKINS_TOKEN= ``` +**If you want to build only specific branch** + +Configure `BRANCH` environment variable to desired branch before you run the script. +Not defined or empty value means all branches (the default). + +```bash +export BRANCH="sagitta" +``` + **Create jobs** Then wait for branch indexing to complete. -- cgit v1.2.3