Created
June 19, 2018 13:40
-
-
Save timbirk/3c6a5b81d4c30827699cc2e60e8dc458 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
echo "Started running at: $(date '+%Y-%m-%d %H:%M:%S %:z')" | |
if [[ ! `bundle show puppet | grep "puppet-5"` ]]; then | |
export PUPPET_VERSION=5.3.3 | |
fi | |
source ~/.github | |
## Use rbenv to install gems | |
export PATH=${HOME}/rbenv/shims:${HOME}/rbenv/bin:/usr/local/bin:/usr/local/sbin:$PATH | |
export RBENV_ROOT=${HOME}/rbenv | |
export RBENV_SHELL=bash | |
## Use rbenv to install gems | |
rbenv versions | |
rbenv local 2.3.1 | |
if [ -n "${PUPPET_VERSION}" ]; then | |
bundle update puppet | |
bundle show puppet | |
fi | |
bundle install --binstubs --path ~/.gem | |
## Purge any librarian config file that might have been created: | |
if [ -f spec/fixtures/.librarian/puppet/config ]; then | |
rm -fv spec/fixtures/.librarian/puppet/config | |
fi | |
# Here we only attempt to cut the release if a release file is present | |
if [ -f *.release ] && [ $(ls -1 *.release | wc -l) -eq 1 ]; then | |
echo "INFO: Release file found - Cutting release" | |
#first check that we have exactly one *.release file | |
if [ -f *.release ] && [ $(ls -1 *.release | wc -l) -gt 1 ];then | |
echo "ERROR: There needs to be exactly 1 *.release file present";exit 1 | |
else | |
#set release filename, and extract the required version increment | |
relfile=$(ls -1 *.release);incr=$(echo $relfile | awk -F '.' '{print $(NF-1)}') | |
fi | |
# Check that tests had passed on pre-merge commit | |
source ~/.github | |
gitrepo=$(echo $(git config --get remote.origin.url) | sed 's@.*/@@' | sed 's/.git//') | |
prcommit=$(git show-ref -s $(git log --merges -n 1 --pretty=format:"%s" | awk -F 'ITV/' '{print $2}')) | |
tests=$(${HOME}/scripts/git/get_pullrequest_status.rb -a ${oauth} -r ${gitrepo} -c 'Running puppet test jobs' -h ${prcommit} 2> /dev/null) | |
if [ ! -z $tests ] && [ $tests == 'success' ]; then | |
echo "OK: Tests are green, proceeding" | |
else | |
echo "ERROR: Tests were not green on merged PR - not releasing"; exit 1 | |
fi | |
#allow either cAsE for increment | |
lincr=$(echo $incr | tr '[A-Z]' '[a-z]') | |
#extract current version from itv.yaml | |
cver=$(cat itv.yaml | awk -F "'" '/^[[:blank:]]*version/ {print $2; exit}') | |
if [[ ! "$cver" =~ ^[0-9]+(\.[0-9]+)+(\.[0-9]+)?$ ]];then | |
echo "ERROR: unable to extract current version";exit 1 | |
fi | |
#handle version increment logic here | |
v=( ${cver//./ } ) | |
set +e | |
if [ $lincr == "major" ];then ((v[0]++)); v[1]=0; v[2]=0 | |
elif [ $lincr == "minor" ];then ((v[1]++)); v[2]=0 | |
elif [ $lincr == "patch" ];then ((v[2]++)) | |
else | |
echo "ERROR: incorrect version increment '$incr'" | |
echo " format of release filename must be: '\$branchname.(major|minor|patch).release'";exit 1 | |
fi | |
set -e | |
#iterate version | |
nver=( ${v[0]}.${v[1]}.${v[2]} ) | |
echo "Release has $lincr increment. Old version: $cver. New version $nver" | |
#apply version change to itv.yaml (via stage) | |
cat itv.yaml | sed s/^"version: '$cver'"/"version: '$nver'"/ > tmp.yaml | |
#verify that only 1 version has changed | |
if [ $(diff itv.yaml tmp.yaml | grep -c '>\|<') -ne 2 ];then | |
echo "ERROR: error updating itv.yaml version";exit 1 | |
else | |
mv tmp.yaml itv.yaml | |
fi | |
## Update metadata.json | |
bundle exec rake update_module | |
# Prepare new Changelog entry (using commit time of merge) and release description file | |
echo "v$nver" > /var/tmp/$relfile | |
echo "" >> /var/tmp/$relfile | |
echo "## $(git log -1 --format=%cd --date=short) - Release $nver" | tee -a tmp.md /var/tmp/$relfile | |
printf '\n' >> tmp.md; cat $relfile >> tmp.md | |
printf '\n' >> tmp.md; cat CHANGELOG.md >> tmp.md | |
mv tmp.md CHANGELOG.md | |
cat $relfile >> /var/tmp/$relfile;rm -f $relfile | |
#add link to PR on release notes | |
echo "" >> /var/tmp/$relfile | |
commithash=$(git log --pretty=tformat:"%H" -1); | |
if [ -z "$commithash" ]; then echo "ERROR: Unable to get commit hash"; exit 1; fi | |
echo "- [Link to Pull Request Changes](https://github.com/ITV/${gitrepo}/commit/${commithash})" >> /var/tmp/$relfile | |
# add, commit and push changes | |
echo "Pushing version changes to remote (and removing .release file)" | |
echo "git add CHANGELOG.md itv.yaml metadata.json $relfile; git commit -m \"Preparing changelog for release $nver\"; git push origin master" | |
git add --all CHANGELOG.md itv.yaml metadata.json $relfile; git commit -m "Preparing changelog for release $nver"; git push origin HEAD:master | |
if [ $? != 0 ]; then | |
echo "ERROR: Unable to push changes to remote";exit 1 | |
fi | |
# use jenkins' commit hash for release tag | |
jenkinscommithash=$(git log -1 | awk '/^commit / {print $2}') | |
if [ -z "$jenkinscommithash" ]; then echo "ERROR: Unable to get commit hash"; exit 1; fi | |
# finally, create the release | |
echo "Creating github release with the following tag/description" | |
echo "tag: v$nver" | |
echo "description:" | |
cat /var/tmp/$relfile | |
echo "hub release create -F /var/tmp/$relfile v$nver -t $jenkinscommithash" | |
/opt/cp-tools/bin/hub release create -F /var/tmp/$relfile v$nver -t $jenkinscommithash | |
if [ $? != 0 ]; then | |
echo "ERROR: Unable to create github release";exit 1 | |
fi | |
# clean up temp release file | |
rm -f /var/tmp/$relfile | |
else | |
echo "WARN: No release file present - Running Spec tests and danger only" | |
## Run test suite, ensuring we honour the Puppetfile.lock | |
LIBRARIAN_lock=true bundle exec rake test | |
if [ -f "Dangerfile" ]; then | |
if [ -n "ghprbGhRepository" ]; then | |
bundle exec danger | |
fi | |
fi | |
fi | |
echo "Finished running at: $(date '+%Y-%m-%d %H:%M:%S %:z')" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Definitely needs improvement... and ridding of clever or magic...