aboutsummaryrefslogtreecommitdiffstats
path: root/files/usr/local/bin/build.sh
blob: 583a5574a9c2625d501264698202acb674270bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh

set -eu

readonly APORTSDIR=$HOME/aports
readonly REPODEST=$HOME/packages
readonly REPOS="main community testing non-free"
readonly MIRROR=http://dl-cdn.alpinelinux.org/alpine
readonly REPOURL=https://github.com/alpinelinux/aports

msg() {
	local color=${2:-green}
	case "$color" in
		red) color="31";;
		green) color="32";;
		yellow) color="33";;
		blue) color="34";;
		*) color="32";;
	esac
	printf "\033[1;%sm>>>\033[1;0m %s\n" "$color" "$1" | xargs >&2
}

die() {
	msg "$1" red
	exit 1
}

get_release() {
	local branch=$DRONE_COMMIT_BRANCH
	case $branch in
		*-stable) echo v${branch%-*};;
		master) echo edge;;
		*) die "Branch \"$branch\" not supported!"
	esac
}

build_aport() {
	local repo="$1" aport="$2"
	cd "$APORTSDIR/$repo/$aport"
	abuild -r
}

changed_repos() {
	cd "$APORTSDIR"
	for repo in $REPOS; do
		git diff --exit-code remotes/origin/$DRONE_COMMIT_BRANCH -- $repo >/dev/null \
			|| echo "$repo"
	done
}

set_repositories_for() {
	local target_repo="$1" repos= repo=
	local release=$(get_release)
	for repo in $REPOS; do
		repos="$repos $MIRROR/$release/$repo"
		[ "$repo" = "$target_repo" ] && break
	done
	sudo sh -c "printf '%s\n' $repos > /etc/apk/repositories"
	sudo apk update
}

changed_aports() {
	cd "$APORTSDIR"
	local repo="$1"
	local aports=$(git diff --name-only --diff-filter=ACMR --relative="$repo" \
		remotes/origin/$DRONE_COMMIT_BRANCH -- "*/APKBUILD" | xargs -I% dirname %)
	ap builddirs -d "$APORTSDIR/$repo" $aports 2>/dev/null | xargs -I% basename % | xargs
}

setup_system() {
	sudo sh -c "echo $MIRROR/$(get_release)/main > /etc/apk/repositories"
	sudo apk -U upgrade -a || apk fix || die "Failed to up/downgrade system"
	abuild-keygen -ain
	sudo sed -i 's/JOBS=[0-9]*/JOBS=$(nproc)/' /etc/abuild.conf
	mkdir -p "$REPODEST"
}

create_workspace() {
	msg "Cloning aports and applying PR$DRONE_PULL_REQUEST"
	git clone --depth=1 --branch $DRONE_COMMIT_BRANCH $REPOURL $APORTSDIR
	wget -qO- $REPOURL/pull/$DRONE_PULL_REQUEST.patch | git -C $APORTSDIR am
}


sysinfo() {
	printf ">>> Host system information (arch: %s, release: %s) <<<\n" "$(apk --print-arch)" "$(get_release)"
	printf "- Number of Cores: %s\n" $(nproc)
	printf "- Memory: %s Gb\n" $(awk '/^MemTotal/ {print ($2/1024/1024)}' /proc/meminfo)
	printf "- Free space: %s\n" $(df -hP / | awk '/\/$/ {print $4}')
}

aport_ok=
aport_ng=

sysinfo || true
setup_system || die "Failed to setup system"
create_workspace || die "Failed to create workspace"

for repo in $(changed_repos); do
	set_repositories_for "$repo"
	for pkgname in $(changed_aports "$repo"); do
		if build_aport "$repo" "$pkgname"; then
			checkapk || true
			aport_ok="$aport_ok $repo/$pkgname"
		else
			aport_ng="$aport_ng $repo/$pkgname"
		fi
	done
done

echo "### Build summary ###"

for ok in $aport_ok; do
	msg "$ok: build succesfully"
done

if [ -n "$aport_ng" ]; then
	die "Failed to build packages:$aport_ng"
elif [ -z "$aport_ok" ]; then
	msg "No packages found to be built." yellow
fi