mirror of https://github.com/Chizi123/Arch-autobuild-repo.git

Joel Grunbaum
2020-09-14 f6854c01a747f20b5be032c53d376543bb9d8ef3
commit | author | age
8ea4ef 1 #!/bin/bash
f6854c 2 #A basic bash script to automate the building of arch packages
JG 3 # Usage: main.sh init|add|build_all [-f force]
8ea4ef 4
JG 5 REPODIR=/repo/x86_64
6 BUILDDIR=/repo/build
7 REPONAME=
8 UPDATE=N
9 CLEAN=N
10 SIGN=N
11 KEY=""
12 NUM_OLD=5
13 export PACKAGER="John Doe <jd@change.me>"
14 EMAIL=""
15
16 ERRORS=""
17
18 #Helper for finding newest and oldest files
19 #Sourced from stack overflow
f6854c 20 # Usage: newold_matching_file [n/o] [filename]
8ea4ef 21 function newold_matching_file
JG 22 {
23     # Use ${1-} instead of $1 in case 'nounset' is set
24     local -r glob_pattern=${2-}
25
26     # To avoid printing garbage if no files match the pattern, set
27     # 'nullglob' if necessary
28     local -i need_to_unset_nullglob=0
29     if [[ ":$BASHOPTS:" != *:nullglob:* ]] ; then
30         shopt -s nullglob
31         need_to_unset_nullglob=1
32     fi
33
34     file=
35     for f in $glob_pattern ; do
36         if [ $1 == "n" ]; then
37             [[ -z $f || $f -nt $_file ]] && file=$f
38         elif [ $1 == "o" ]; then
39             [[ -z $f || $f -ot $_file ]] && file=$f
40         fi
41     done
42
43     # To avoid unexpected behaviour elsewhere, unset nullglob if it was
44     # set by this function
45     (( need_to_unset_nullglob )) && shopt -u nullglob
46
47     # Use printf instead of echo in case the file name begins with '-'
48     [[ -n $file ]] && printf '%s\n' "$file"
49
50     return 0
51 }
52
53 #Build latest version of a package
f6854c 54 # Usage: build_pkg [package name] [new?] [-f force]
8ea4ef 55 function build_pkg {
JG 56     #check if PKGBUILD has updated, don't rebuild if hasn't changed
57     if [[ ! -z $(git pull | grep "Already up to date.") && -z $(echo $1 | grep git) && -z $2 ]]; then
58         return 2
59     fi
f6854c 60
JG 61     #remove old versions before build
62     rm "$1*.pkg.tar.xz"
63
64     #make and force rebuild if is git package
8ea4ef 65     makepkg -s --noconfirm $([ $CLEAN == "Y" ] && echo "-c") $([ $SIGN == "Y" ] && echo "--sign --key $KEY") $([ "$2" == "-f" ] && echo -f)
JG 66     if [ $? != 0 ]; then
67         #Register error
f6854c 68         ERRORS="$ERRORS $1"
8ea4ef 69         return 1
JG 70     fi
71
72     #copy package to repo directory
f6854c 73     #latest="$(newold_matching_file n '*.pkg.tar.xz')"
JG 74     for f in '$1*.pkg.tar.xz'
75     do
76         cp $f $REPODIR/$f
77         repo-add $([ "$SIGN" == "Y" ] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.xz $REPODIR/$f
78     done
8ea4ef 79
JG 80     #Remove old versions of packages
f6854c 81     #TODO: Want to be able to keep multiple versions of old packages, future work
JG 82     #Currently old package versions stay in the repodir indefinately
83     # while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ]
84     # do
85     #     old=$(newold_matching_file o '*.pkg.tar.xz')
86     #     rm $REPODIR/$old $old
87     # done
8ea4ef 88     return 0
JG 89 }
90
91 #Update packages in BUILDDIR
f6854c 92 # Usage: build_all [-f force]
8ea4ef 93 function build_all {
JG 94     #system update
95     if [ $UPDATE == "Y" ]; then
96         sudo pacman -Syu --noconfirm
97     fi
98     #update every package currently stored
99     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -not -path '*/\.*' -type d)
100     do
101         cd $d
102         build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1
103     done
104
105     return 0
106 }
107
108 #Add a new package to be built
f6854c 109 #There is no name checking so be sure to put in the name correctly
JG 110 # Usage: add [package name]
8ea4ef 111 function add {
JG 112     cd $BUILDDIR
113     git clone https://aur.archlinux.org/$1.git
114     cd $1
115     build_pkg $1 new
116     return 0
117 }
118
119 #Check config and create build folders
f6854c 120 #Set variables before usage
JG 121 # Usage: init
8ea4ef 122 function init {
JG 123     #check for configuration here
124     [ -z $REPODIR ] && echo "Enter REPODIR" && return 1
125     [ -z $BUILDDIR ] && echo "Enter BUILDDIR" && return 2
126     [ -z $REPONAME ] && echo "Enter REPONAME" && return 3
127
128     #make build directories
129     [ ! -d $REPODIR ] && mkdir -p $REPODIR
130     [ ! -d $BUILDDIR ] && mkdir -p $BUILDDIR
131
132     #packages required to build others
133     sudo pacman -S --noconfirm base-devel git
134
135     #add repo to pacman.conf so can install own packages
136     if [ -z $(grep "$REPONAME" /etc/pacman.conf) ]; then
137         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
138     fi
139
140     #create GPG key for package signing
141     if [ "$SIGN" == "Y" && "$KEY" == "" ]; then
142         (
143             echo "Key-Type: RSA"
144             echo "Key-Length: 2048"
145             echo "Subkey-Type: RSA"
146             echo "Subkey-Length: 2048"
147             echo "Passphrase: \"\""
148             echo "Expire-Date: 0"
149             echo "Name-Real: John Doe"
150             echo "Name-Comment: Arch buildbot"
151             echo "Name-Email: $(whoami)@localhost"
152             echo "%commit"
153         ) | gpg --batch --generate-key
154         gpg --export --output $REPONAME.key --armor "John Doe"
155         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
156         echo "Please change the key information in this file"
157     fi
158
159     return 0
160 }
161
162 case $1 in
163     "init")
164         init;;
165     "add")
166         add $2;;
167     "build-all")
168         build_all $([ "$2" == "-f" ] && echo "-f")
169         if [ "$ERRORS" != "" ]; then
170             echo "Errors in packages $ERRORS"
171             if [ "$EMAIL" != "" ]; then
172                 printf "Build for $(date)\nErrors found in $ERRORS\nPlease address these soon" | sendmail $EMAIL
173             fi
174         else
175             echo "All packages built successfully"
176         fi
177         ;;
178     *)
179         printf "Invalid usage\nUsage: $0 init|add|build_all\n";;
180 esac