Skip to content

Instantly share code, notes, and snippets.

@adrianbiro
Last active July 12, 2023 12:35
Show Gist options
  • Save adrianbiro/03a2812792280ee97f0b00e78944d6df to your computer and use it in GitHub Desktop.
Save adrianbiro/03a2812792280ee97f0b00e78944d6df to your computer and use it in GitHub Desktop.

new

#!/bin/bash
echo -e "---------------------------------------------------------------\ncd $(git rev-parse --show-cdup)jenkins_bkp"
cd "$(git rev-parse --show-cdup)jenkins_bkp"
echo -e "---------------------------------------------------------------\noc project smart-deva"
oc project smart-deva
echo -e "---------------------------------------------------------------\noc rsync --include="config.xml" --exclude="/*/*/,state.xml" --delete=true $(oc get pod | awk '/jenkins/&&!/agent/{print $1}'):/var/lib/jenkins/jobs/smartcase/jobs/ bkp/"

oc rsync --include="config.xml" --exclude="/*/*/,state.xml,nextBuildNumber" --delete=true $(oc get pod | awk '/jenkins/&&!/agent/{print $1}'):/var/lib/jenkins/jobs/smartcase/jobs/ bkp/

echo -e "---------------------------------------------------------------\nGIT"
cd "$(git rev-parse --show-toplevel)"
echo -e "---------------------------------------------------------------\nChecking diff for changes\n\n"
git status -sb
if (( $(git diff | wc -l) > 0 )); then
	git config --global user.name "jenkins bkp"
	git config --global user.email "jenkins@smart.cz"
	git config --list | grep user
	echo -e "---------------------------------------------------------------\ngit add --all"
	git add --all
	echo -e "---------------------------------------------------------------\ngit git commit -m 'jenkins bkp'"
	git commit --allow-empty -m "jenkins bkp"
	echo -e "---------------------------------------------------------------\ngit push origin master"
	git push origin HEAD:master
else
	echo -e "\nNothing to commit, working tree clean."
fi

Old

jobs in /var/lib/jenkins/jobs/<namespace>/jobs

delete job queue

/*
https://jenkins-<org>/script
*/
Jenkins.instance.queue.clear()

java -jar jenkins-cli.jar -s https://jenkins-<org>/ -webSocket clear-queue

Before restart

oc delete pod $(oc get pod -o wide | awk '/jenkins-ocp3-agent/{print $1}')

Deploy

oc rollout latest dc/jenkins

In case of any problem with the deployment

oc rollout retry dc/jenkins

oc rollout cancel dc/jenkins

deploy template

adjust stript for operation

cd /var/lib/jenkins/jobs/smartcase/jobs
find . -maxdepth 1 -type d -name "Build*" -print0 | while IFS= read -r -d '' dir; do cp config.xml "${dir}"; done
# change value in template !!!set -name filter for find firts!!!
find . -maxdepth 2 -name "config.xml" -print0 | while IFS= read -r -d '' dir; do name="$(sed 's/\.\///g; s/\/config.xml//g' <<< ${dir})"; sed -i "s/XXXXXX/$name/g" "${dir}"; done

generate random string for trigger str=$(cat /proc/sys/kernel/random/uuid | sed 's/[-]//g' | head -c 300 | base64)

sed -i "s/<token>XXXXXX<\/token>/<token>$str<\/token>/g" ${dir}

API

https://gist.github.com/justlaputa/5634984 https://stackoverflow.com/questions/25687925/jenkins-api-get-a-list-of-jobs-filtered-by-build-parameter-what-jobs-have-bui

Jenkins metadata backup

Make backup

in pod

oc rsh $(oc get pod | awk '/jenkins/&&!/agent/{print $1}')

cd /var/lib/jenkins/jobs/smartcase/jobs

mkdir bkp

find . -maxdepth 2 -name 'config.xml' -print0 | while IFS= read -r -d '' dir; do name="$(sed 's/\.\///g; s/\//_/g' <<< ${dir})"; cp "${dir}" "bkp/${name}"; done
# after local steps
rm -rf bkp

local

cd "$(git rev-parse --show-cdup)jenkins_bkp"

rm -rf bkp
 
oc rsync $(oc get pod | awk '/jenkins/&&!/agent/{print $1}'):/var/lib/jenkins/jobs/smartcase/jobs/bkp .

Restore backup

oc rsync bkp $(oc get pod | awk '/jenkins/&&!/agent/{print $1}'):/var/lib/jenkins/jobs/smartcase/jobs/
oc rsh $(oc get pod | awk '/jenkins/&&!/agent/{print $1}')
find bkp/ -print0 | while IFS= read -r -d '' name; do dir="$(sed 's/_config.xml//; s/bkp\///' <<< ${name})"; mkdir -p "${dir}"; cp "${name}" "${dir}/config.xml" ; done

Template

mv <template.xml> config.xml

Release_smartware_config.xml

find . -maxdepth 1 \( -name "Release smart-ts-lib" -o -name "Release smart-webapp-runner" -o -name "Release ngware" -o -name "Release smartware" -o -name "Release smart-framework" -o -name "Release*-si" -o -name "Release*-ui" -o -name "Release*-comps" \) -print0 | while IFS= read -r -d '' dir; do name="$(sed 's/\.\///g; s/\/config.xml//g' <<< ${dir})"; repo="${name##* }"; sed "s/XXXXXX/$name/g; s/RRRRRR/$repo/g" config.xml > "${dir}/config.xml"; done

# deploy token
find . -maxdepth 1 \( -name "Release smart-ts-lib" -o -name "Release smart-webapp-runner" -o -name "Release ngware" -o -name "Release smartware" -o -name "Release smart-framework" -o -name "Release*-si" -o -name "Release*-ui" -o -name "Release*-comps" \) -print0 |  while IFS= read -r -d '' dir; do str=$(cat /proc/sys/kernel/random/uuid | sed 's/[-]//g' | head -c 300 | base64); sed -i "s/<token>.*<\/token>/<token>$str<\/token>/g" "${dir}/config.xml"; echo "${dir} ${str}" >> token.txt; done

Build_smart_config.xml

find . -maxdepth 1 \( -name "Build*" -a ! \( -name "*-si" -o -name "*smart-*" -o -name "*oauth2-proxy" -o -name "*-ts" -o -name "*smart-*" \) \) -print0 | while IFS= read -r -d '' dir; do name="$(sed 's/\.\///g; s/\/config.xml//g' <<< ${dir})"; sed "s/XXXXXX/$name/g" config.xml > "${dir}/config.xml"; done

Build_im-ui_config.xml

find . -maxdepth 1 -name "Build*-ui" -print0 | while IFS= read -r -d '' dir; do name="$(sed 's/\.\///g; s/\/config.xml//g' <<< ${dir})"; repo="${name##* }"; sed "s/XXXXXX/$name/g; s/RRRRRR/$repo/g" config.xml > "${dir}/config.xml"; done

Build_smart-ts_config.xml

find .  -maxdepth 1 -name "Build smart-ts*" -print0 | while IFS= read -r -d '' dir; do name="$(sed 's/\.\///g; s/\/config.xml//g' <<< ${dir})"; repo="${name##* }"; sed "s/XXXXXX/$name/g; s/RRRRRR/$repo/g" config.xml > "${dir}/config.xml"; done

Reload jenkins

run one-by-one

/*
https://jenkins-smart-deva.nocp.vs.csin.cz/script
*/
 
Jenkins.instance.queue.clear()
oc delete pod $(oc get pod -o wide | awk '/jenkins-ocp3-agent/{print $1}')
oc rollout latest dc/jenkins
# In case of any problem with the deployment
oc rollout retry dc/jenkins
oc rollout cancel dc/jenkins

apps

Smart moduly
repo smart
hook
===
Release:
smart-framework
smartware
ngware
smart-webapp-runner
gti-ui-comps
smart-ts-lib
smart-ui-comps
*-si
gti-*-ui
(smart-ts-hs-service)
====
UI
Build smart-ts-* mimo smart-ts-lib
===
Integracni vetev
Smartware
NGWare
smart-Framework
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment