# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Rasmus Thomsen <oss@cogitir.dev>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=gitlab-runner
pkgver=17.2.1
pkgrel=0
pkgdesc="GitLab runner for CI/CD jobs"
url="https://docs.gitlab.com/runner/"
arch="all"
license="MIT"
makedepends="go jq"
install="$pkgname.pre-install $pkgname.pre-upgrade"
pkgusers="gitlab-runner"
pkggroups="gitlab-runner"
subpackages="$pkgname-helper $pkgname-openrc"
source="https://gitlab.com/gitlab-org/gitlab-runner/-/archive/v$pkgver/gitlab-runner-v$pkgver.tar.gz
	$pkgname-$pkgver-tags.json::https://gitlab.com/api/v4/projects/250833/repository/tags/v$pkgver
	syslog-log-format.patch
	use-logrus-syslog-hook.patch
	update-go-mod.patch
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotate
	"
builddir="$srcdir/$pkgname-v$pkgver"
# check: need to be run in a git repo
# net: required for pulling dependencies
options="!check net"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

# secfixes:
#   15.10.0-r0:
#     - CVE-2022-1996

build() {
	# This is used to determine what version of
	# https://hub.docker.com/r/gitlab/gitlab-runner-helper/tags to use.
	local gitrev
	gitrev="$(jq -r '.commit.short_id' "$srcdir/$pkgname-$pkgver-tags.json")"
	echo "Tag v$pkgver resolved to SHA $gitrev" >&2

	local ldflags="
		-X gitlab.com/gitlab-org/$pkgname/common.NAME=$pkgname
		-X gitlab.com/gitlab-org/$pkgname/common.VERSION=$pkgver
		-X gitlab.com/gitlab-org/$pkgname/common.REVISION=$gitrev
		-X gitlab.com/gitlab-org/$pkgname/common.BUILT=$(date -d @"$SOURCE_DATE_EPOCH" -u +%Y-%m-%dT%H:%M:%S%z)
		-X gitlab.com/gitlab-org/$pkgname/common.BRANCH=master
		"
	# required for github.com/docker/docker
	export GO111MODULE=auto
	go build -ldflags "$ldflags" -o bin/gitlab-runner
	go build -ldflags "$ldflags" -o bin/gitlab-runner-helper \
		./apps/gitlab-runner-helper
}

package() {
	install -Dm755 bin/gitlab-runner -t "$pkgdir"/usr/bin/

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname

	install -d -m750 -o "$pkgusers" -g "$pkggroups" "$pkgdir"/etc/$pkgname
	install -d -m700 -o "$pkgusers" -g "$pkggroups" "$pkgdir"/var/lib/$pkgname
}

helper() {
	pkgdesc="$pkgdesc (helper)"

	install -Dm755 "$builddir"/bin/gitlab-runner-helper -t "$subpkgdir"/usr/bin/
}

sha512sums="
f3818371cc8cb3ed08800e64a309ebeb6fdd54363af83fbffcb417556c59a6b59c78bd2f3cd84263250bcb19003196eb4fbd240adbeccf63f4e52b7263f8e3d5  gitlab-runner-v17.2.1.tar.gz
f8c09ab4ab0a9e21c66943456211ed188d8b4200cca4c751c9ee2cff10e16106f60203407e2d4018083b5e211d3b8ffab4a6af6bf1f7390b3bd808cc2270af0f  gitlab-runner-17.2.1-tags.json
c2e2d36714d1f8fc49ed2de6b411a879183b40356af366e9e89e0ee246d0b5400ebbfc598f747e30dec813c8d89399d80a1e8218890baed525136a734e1ca626  syslog-log-format.patch
30726c362476b9f93b44c44435cd04e8210f04b25f878302c7edeca387094a616af7aa20ece6eb6dcf329a2702ef2079dcc002779deef09d78ecb447253f87bf  use-logrus-syslog-hook.patch
514094b596e351e991b41a6d215524bf2bb012aa065be64b7e02e598e99d5a8870aeebfcffd70290791273393cddb647c1c83b413525b462f3bf3f3d6eea6fdc  update-go-mod.patch
8cdcea1e07b30b720d0e18a12f06fff69f2a7804d0038b2bfd6a880a5c99eb4b2b06dd8ccd21edc501a2205d89decb176b725211a03253068fa45b6861e75f11  gitlab-runner.initd
45c3727a4b2544c59db50b3a93d917bbce14441453ebd0b81c981568ccc71338e564213f7bed449f0712fbcfb96ce6e51ac5759d5bde82e3986265e26d9db315  gitlab-runner.confd
2ddd42622b72135a95ac1c09061b61e73c4f89d4f0a86d1a3c902760d4a5f2c749a04f2d81568c973c83b1aae60ada71360a6bf74a31f8e5d9b10b90af4cdcfe  gitlab-runner.logrotate
"
