scripts/release_helper.sh: also remove leftover directories from grml-live.build...
[grml-live.git] / scripts / release_helper.sh
1 #!/bin/bash
2 # Filename:      scripts/release_helper.sh
3 # Purpose:       helper script to build grml-live Debian packages
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2 or any later version.
7 ################################################################################
8 ## How to set up autobuild (for automatically updating grml-live.git and
9 ## build packages out of git tree and install them, e.g. for usage on
10 ## daily.grml.org):
11 # echo "deb file:/home/grml-live-git/grml-live.build-area/ ./" >> /etc/apt/sources.list.d/grml-live.list
12 # adduser --disabled-login --disabled-password grml-live-git
13 # visudo -> add "grml-live-git ALL=NOPASSWD: /usr/bin/apt-get"
14 # su - grml-live-git
15 # mkdir /home/grml-live-git/grml-live.build-area
16 # git clone git://git.grml.org/grml-live.git
17 # git config --global user.name "Grml-Live Git Autobuild"
18 # git config --global user.email "grml-live-git@$(hostname)"
19 #
20 # Finally install a cron job (as user grml-live-git) like:
21 # 30 00 * * * cd /home/grml-live-git/grml-live.git/ && env AUTOBUILD=1 scripts/release_helper.sh >/home/grml-live-git/grml-live-build.log
22 #
23 # Tip: To find out the build date of the installed grml-live package just run:
24 #
25 # % apt-cache policy grml-live | grep 'Installed.*autobuild'
26 #  Installed: 0.13.1~autobuild1300450381
27 #
28 # and run "date -ud @$STRING" where $STRING is the number
29 # behind the "autobuild", like:
30 # % date -ud @1300450081
31 # Fri Mar 18 12:08:01 UTC 2011
32 ################################################################################
33
34 set -e
35 set -u
36
37 export LC_ALL=C
38 export LANG=C
39
40 debian_version=''
41 script_version=''
42
43 autobuild_branch="autobuild_$(date +%Y%m%d_%H%M%S)"
44
45 if [ -n "${AUTOBUILD:-}" ] ; then
46   git checkout master
47   git pull
48   git checkout -b "$autobuild_branch"
49 else
50   if git status --porcelain | grep -q '^?? '; then
51     printf "Uncommited changes in current working tree. Please commit/clean up.\n"
52     exit 1
53   fi
54 fi
55
56 printf "Building debian/changelog: "
57 if [ -n "${AUTOBUILD:-}" ] ; then
58   # since=$(git show -s --pretty="tformat:%h")
59   eval $(grep '^GRML_LIVE_VERSION=' grml-live)
60   DATE=$(date -R)
61   UNIXTIME=$(date +%s)
62
63   cat > debian/changelog << EOF
64 grml-live (${GRML_LIVE_VERSION}~autobuild${UNIXTIME}) UNRELEASED; urgency=low
65
66   * Automatically built package based on the state of
67     git repository at http://git.grml.org/?p=grml-live.git
68     on $DATE ->
69
70     $(git log --format=oneline -1)
71
72  -- grml-live Auto Build <grml-live-git@$(hostname)>  $DATE
73
74 EOF
75   git add debian/changelog
76   git commit -m "Releasing ${GRML_LIVE_VERSION}-~autobuild${UNIXTIME} (auto build)"
77 else
78   since=v$(dpkg-parsechangelog | awk '/^Version:/ {print $2}')
79   git-dch --ignore-branch --since=$since \
80           --id-length=7 --meta --multimaint-merge -S
81   printf "OK\n"
82 fi
83
84 if [ -z "${AUTOBUILD:-}" ] ; then
85   if ! $EDITOR debian/changelog ; then
86     printf "Exiting as editing debian/changelog returned an error." >&2
87     exit 1
88   fi
89 fi
90
91 debian_version="$(dpkg-parsechangelog | awk '/^Version:/ {print $2}')"
92
93 dorelease="true"
94 if echo "$debian_version" | grep -q '\.gbp' ; then
95   printf "Building snapshot version, not releasing.\n"
96   dorelease="false"
97 fi
98
99 printf "Updating GRML_LIVE_VERSION string in grml-live script: "
100 sed -i "s/^GRML_LIVE_VERSION=.*/GRML_LIVE_VERSION='$debian_version'/" grml-live
101 printf "OK\n"
102
103 printf "Comparing versions of debian/changelog with grml-live version string: "
104 script_version="$(awk -F= '/^GRML_LIVE_VERSION=/ {print $2}' grml-live | sed "s/'//g")"
105 debian_version="$(dpkg-parsechangelog | awk '/^Version:/ {print $2}')"
106
107 if [[ "$script_version" == "$debian_version" ]] ; then
108   printf "OK\n"
109 else
110   printf "FAILED\n."
111   printf "Debian package version ($debian_version) does not match script version ($script_version).\n"
112   exit 1
113 fi
114
115 if $dorelease || [ -n "${AUTOBUILD:-}" ] ; then
116   git add debian/changelog grml-live
117   git commit -s -m "Release new version ${debian_version}."
118 fi
119
120 printf "Building debian packages:\n"
121 if [ -n "${AUTOBUILD:-}" ] ; then
122   [ -d ../grml-live.build-area ] || mkdir ../grml-live.build-area
123   rm -rf ../grml-live.build-area/grml-live* # otherwise we're keeping files forever...
124   git-buildpackage --git-ignore-branch --git-ignore-new --git-export-dir=../grml-live.build-area -us -uc
125 else
126   git-buildpackage --git-ignore-branch --git-ignore-new $*
127   printf "Finished execution of $(basename $0). Do not forget to tag release ${debian_version}\n"
128 fi
129
130 if [ -n "${AUTOBUILD:-}" ] ; then
131    (
132      cd ../grml-live.build-area
133      dpkg-scanpackages . /dev/null > Packages
134    )
135    git checkout master
136    git branch -D ${autobuild_branch} || true
137    sudo apt-get update
138    PACKAGES=$(dpkg --list grml-live\* | awk '/^ii/ {print $2}')
139    sudo apt-get -y --allow-unauthenticated install $PACKAGES
140 fi
141
142 ## END OF FILE #################################################################