Skip to content

Instantly share code, notes, and snippets.

@pysysops
Last active January 15, 2016 11:40
Show Gist options
  • Select an option

  • Save pysysops/108443b3efd368fa0f22 to your computer and use it in GitHub Desktop.

Select an option

Save pysysops/108443b3efd368fa0f22 to your computer and use it in GitHub Desktop.
Snippet of Job template that I use to create jobs for each branch of projects that have been active in the past 14 days. Basically adds to a folder of Jenkins generated Job Builder YAML that gets updated on scm change. Hacky but it's a start.
#!/bin/bash
#This script is a magical script to generate jobs for each active branch of a git repo.
#Add some randomness to give bitbucket / git a break
sleep $(( ( RANDOM % 10 ) + 10 ))
cd $WORKSPACE
daysToKeep=14
#Jenkins generated configs go in here...
[ -d jenkins ] || mkdir jenkins
echo -n "#File generated by Jenkins. Any changes will be destroyed!" > ./jenkins/{name}-branches.yaml
mkdir ./TMP
git clone {giturl} ./TMP
cd TMP
for branch in $( git branch -r | grep -v HEAD )
do
active=$(git rev-list -n1 --since="14 days ago" $branch)
#Master and develop branch should not be built by feature discovery job
echo $branch | egrep -q "master|develop|support\/"
if [ $? -eq 0 ]; then
active=1
fi
#Output YAML for any branch where the active variable is not null
if [ ! -z $active ]
then
gitbranch=$(echo $(echo $branch | sed 's|origin/||g'))
jobSuffix=$(echo $(echo $branch | sed 's|origin/||g' | sed 's|/|-|g'))
#Magic for branch merging stuff
gitmerge={gitmerge}
echo $branch | egrep -q "support\/"
if [ $? -eq 0 ]; then
gitmerge="$gitbranch"
fi
echo $branch | egrep -q "master|develop|release|uat"
if [ $? -eq 0 ]; then
gitmerge='master'
fi
echo "" >> ../jenkins/{name}-branches.yaml
echo "- project:" >> ../jenkins/{name}-branches.yaml
#Handle branches named the same but in a different case - hack, hack, hack...
#But yes, we seem to enjoy creating branches like ST-xxx and st-xxx
grep -q -i "{name}-$jobSuffix'$" ../jenkins/{name}-branches.yaml
if [ $? -eq 0 ]; then
echo " name: '{name}-$jobSuffix-1'" >> ../jenkins/{name}-branches.yaml
else
echo " name: '{name}-$jobSuffix'" >> ../jenkins/{name}-branches.yaml
fi
echo " giturl: '{giturl}'" >> ../jenkins/{name}-branches.yaml
echo " gitbranch: '$gitbranch'" >> ../jenkins/{name}-branches.yaml
echo " gitmerge: '$gitmerge'" >> ../jenkins/{name}-branches.yaml
echo " jobtype: '{jobtype}'" >> ../jenkins/{name}-branches.yaml
echo " jobs:" >> ../jenkins/{name}-branches.yaml
echo " - {jobtype}-jobs" >> ../jenkins/{name}-branches.yaml
fi
done
cd $WORKSPACE
rm -rf TMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment