Makefile 13.6 KB
Newer Older
1
NAME ?= gitlab-runner
2
PACKAGE_NAME ?= $(NAME)
3
export VERSION := $(shell ./ci/version)
4
REVISION := $(shell git rev-parse --short=8 HEAD || echo unknown)
5
BRANCH := $(shell git show-ref | grep "$(REVISION)" | grep -v HEAD | awk '{print $$2}' | sed 's|refs/remotes/origin/||' | sed 's|refs/heads/||' | sort | head -n 1)
6
BUILT := $(shell date -u +%Y-%m-%dT%H:%M:%S%z)
7
export TESTFLAGS ?= -cover
8 9 10 11 12 13 14

LATEST_STABLE_TAG := $(shell git -c versionsort.prereleaseSuffix="-rc" -c versionsort.prereleaseSuffix="-RC" tag -l "v*.*.*" --sort=-v:refname | awk '!/rc/' | head -n 1)
export IS_LATEST :=
ifeq ($(shell git describe --exact-match --match $(LATEST_STABLE_TAG) >/dev/null 2>&1; echo $$?), 0)
export IS_LATEST := true
endif

15
PACKAGE_CLOUD ?= ayufan/gitlab-ci-multi-runner
16
PACKAGE_CLOUD_URL ?= https://packagecloud.io/
17
BUILD_PLATFORMS ?= -os '!netbsd' -os '!openbsd'
Kamil Trzciński's avatar
Kamil Trzciński committed
18
S3_UPLOAD_PATH ?= master
19

20
# Keep in sync with docs/install/linux-repository.md
21
DEB_PLATFORMS ?= debian/wheezy debian/jessie debian/stretch debian/buster \
22
    ubuntu/trusty ubuntu/xenial ubuntu/artful ubuntu/bionic \
23
    raspbian/wheezy raspbian/jessie raspbian/stretch raspbian/buster \
24
    linuxmint/qiana linuxmint/rebecca linuxmint/rafaela linuxmint/rosa linuxmint/sarah linuxmint/serena linuxmint/sonya
25
DEB_ARCHS ?= amd64 i386 armel armhf
26 27
RPM_PLATFORMS ?= el/6 el/7 \
    ol/6 ol/7 \
28
    fedora/26 fedora/27 fedora/28
Kamil Trzciński's avatar
Kamil Trzciński committed
29
RPM_ARCHS ?= x86_64 i686 arm armhf
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

PKG = gitlab.com/gitlab-org/$(PACKAGE_NAME)
COMMON_PACKAGE_NAMESPACE=$(PKG)/common

BUILD_DIR := $(CURDIR)
TARGET_DIR := $(BUILD_DIR)/out

ORIGINAL_GOPATH = $(shell echo $$GOPATH)
LOCAL_GOPATH := $(CURDIR)/.gopath
GOPATH_SETUP := $(LOCAL_GOPATH)/.ok
GOPATH_BIN := $(LOCAL_GOPATH)/bin
PKG_BUILD_DIR := $(LOCAL_GOPATH)/src/$(PKG)

export GOPATH = $(LOCAL_GOPATH)
export PATH := $(GOPATH_BIN):$(PATH)
45 46 47

# Packages in vendor/ are included in ./...
# https://github.com/golang/go/issues/11659
48
export OUR_PACKAGES ?= $(subst _$(BUILD_DIR),$(PKG),$(shell go list ./... | grep -v '/vendor/'))
49

50 51
GO_LDFLAGS ?= -X $(COMMON_PACKAGE_NAMESPACE).NAME=$(PACKAGE_NAME) -X $(COMMON_PACKAGE_NAMESPACE).VERSION=$(VERSION) \
              -X $(COMMON_PACKAGE_NAMESPACE).REVISION=$(REVISION) -X $(COMMON_PACKAGE_NAMESPACE).BUILT=$(BUILT) \
52 53
              -X $(COMMON_PACKAGE_NAMESPACE).BRANCH=$(BRANCH) \
              -s -w
54
GO_FILES ?= $(shell find . -name '*.go' | grep -v './.gopath/')
55
export CGO_ENABLED ?= 0
56

57 58 59 60

# Development Tools
GOX = $(GOPATH_BIN)/gox
MOCKERY = $(GOPATH_BIN)/mockery
Alessio Caiazza's avatar
Alessio Caiazza committed
61
DEVELOPMENT_TOOLS = $(GOX) $(MOCKERY)
62

63 64
MOCKERY_FLAGS = -note="This comment works around https://github.com/vektra/mockery/issues/155"

65 66
.PHONY: clean version mocks

Alessio Caiazza's avatar
Alessio Caiazza committed
67 68 69
all: deps docker build

include Makefile.docker.mk
Kamil Trzciński's avatar
Kamil Trzciński committed
70

Kamil Trzciński's avatar
Kamil Trzciński committed
71
help:
72
	# Commands:
73
	# make all => deps build
74
	# make version - show information about current version
75 76
	#
	# Development commands:
77
	# make install - install the version suitable for your OS as gitlab-runner
78 79 80
	# make docker - build docker dependencies
	#
	# Testing commands:
Kamil Trzciński's avatar
Kamil Trzciński committed
81
	# make test - run project tests
82
	# make codequality - run code quality analysis
83 84 85
	#
	# Deployment commands:
	# make deps - install all dependencies
Kamil Trzciński's avatar
Kamil Trzciński committed
86 87 88 89 90
	# make build - build project for all supported OSes
	# make package - package project using FPM
	# make packagecloud - send all packages to packagecloud
	# make packagecloud-yank - remove specific version from packagecloud

91
version:
92 93
	@echo Current version: $(VERSION)
	@echo Current revision: $(REVISION)
94 95
	@echo Current branch: $(BRANCH)
	@echo Build platforms: $(BUILD_PLATFORMS)
96 97
	@echo DEB platforms: $(DEB_PLATFORMS)
	@echo RPM platforms: $(RPM_PLATFORMS)
98
	@echo IS_LATEST: $(IS_LATEST)
99

100
deps: $(DEVELOPMENT_TOOLS)
Kamil Trzciński's avatar
Kamil Trzciński committed
101

102
codequality:
103
	./scripts/codequality analyze --dev
104

Alessio Caiazza's avatar
Alessio Caiazza committed
105
build: $(GOX)
106
	# Building $(NAME) in version $(VERSION) for $(BUILD_PLATFORMS)
107
	gox $(BUILD_PLATFORMS) \
108
		-ldflags "$(GO_LDFLAGS)" \
109 110
		-output="out/binaries/$(NAME)-{{.OS}}-{{.Arch}}" \
		$(PKG)
Kamil Trzciński's avatar
Kamil Trzciński committed
111

112
build_simple: $(GOPATH_SETUP)
113 114 115
	# Building $(NAME) in version $(VERSION) for current platform
	go build \
		-ldflags "$(GO_LDFLAGS)" \
116 117
		-o "out/binaries/$(NAME)" \
		$(PKG)
118

119
build_current: docker build_simple
120

Alessio Caiazza's avatar
Alessio Caiazza committed
121
check_race_conditions:
122 123
	@./scripts/check_race_conditions $(OUR_PACKAGES)

Alessio Caiazza's avatar
Alessio Caiazza committed
124
test: $(PKG_BUILD_DIR) docker
Kamil Trzciński's avatar
Kamil Trzciński committed
125
	# Running tests...
126 127 128 129 130 131
	go test $(OUR_PACKAGES) $(TESTFLAGS)

parallel_test_prepare: $(GOPATH_SETUP)
	# Preparing test commands
	@./scripts/go_test_with_coverage_report prepare

132
parallel_test_execute: $(GOPATH_SETUP) pull_images_for_tests
133 134 135 136 137 138
	# executing tests
	@./scripts/go_test_with_coverage_report execute

parallel_test_coverage_report: $(GOPATH_SETUP)
	# Preparing coverage report
	@./scripts/go_test_with_coverage_report coverage
Kamil Trzciński's avatar
Kamil Trzciński committed
139

140 141 142 143
pull_images_for_tests: $(GOPATH_SETUP)
	# Pulling images required for some tests
	@go run ./scripts/pull-images-for-tests/main.go

Alessio Caiazza's avatar
Alessio Caiazza committed
144
install:
145
	go install --ldflags="$(GO_LDFLAGS)" $(PKG)
146

147 148 149
dockerfiles:
	make -C dockerfiles all

150 151
# We rely on user GOPATH 'cause mockery seems not to be able to find dependencies in vendor directory
mocks: $(MOCKERY)
152
	rm -rf ./helpers/service/mocks
153
	find . -type f ! -path '*vendor/*' -name 'mock_*' -delete
154
	GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./vendor/github.com/ayufan/golang-kardianos-service -output=./helpers/service/mocks -name='(Interface)'
155
	GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./helpers/docker -all -inpkg
156
	GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./helpers/certificate -all -inpkg
157
	GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./cache -all -inpkg
158
	GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./common -all -inpkg
159
	GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./log -all -inpkg
160
	GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./session -all -inpkg
161
	GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./shells -all -inpkg
162

163
test-docker:
164 165 166 167 168 169 170
	make test-docker-image IMAGE=centos:6 TYPE=rpm
	make test-docker-image IMAGE=centos:7 TYPE=rpm
	make test-docker-image IMAGE=debian:wheezy TYPE=deb
	make test-docker-image IMAGE=debian:jessie TYPE=deb
	make test-docker-image IMAGE=ubuntu-upstart:precise TYPE=deb
	make test-docker-image IMAGE=ubuntu-upstart:trusty TYPE=deb
	make test-docker-image IMAGE=ubuntu-upstart:utopic TYPE=deb
171 172

test-docker-image:
173 174
	tests/test_installation.sh $(IMAGE) out/$(TYPE)/$(PACKAGE_NAME)_amd64.$(TYPE)
	tests/test_installation.sh $(IMAGE) out/$(TYPE)/$(PACKAGE_NAME)_amd64.$(TYPE) Y
175

176 177 178 179 180 181
build-and-deploy:
	make build BUILD_PLATFORMS="-os=linux -arch=amd64"
	make package-deb-fpm ARCH=amd64 PACKAGE_ARCH=amd64
	scp out/deb/$(PACKAGE_NAME)_amd64.deb $(SERVER):
	ssh $(SERVER) dpkg -i $(PACKAGE_NAME)_amd64.deb

182 183 184 185
build-and-deploy-binary:
	make build BUILD_PLATFORMS="-os=linux -arch=amd64"
	scp out/binaries/$(PACKAGE_NAME)-linux-amd64 $(SERVER):/usr/bin/gitlab-runner

186 187 188 189
package: package-deps package-prepare package-deb package-rpm

package-deps:
	# Installing packaging dependencies...
Alessio Caiazza's avatar
Alessio Caiazza committed
190
	which fpm 1>/dev/null || gem install rake fpm --no-ri --no-rdoc
191 192

package-prepare:
193
	chmod 755 packaging/root/usr/share/gitlab-runner/
194
	chmod 755 packaging/root/usr/share/gitlab-runner/*
195

196
package-deb: package-deps package-prepare
Kamil Trzciński's avatar
Kamil Trzciński committed
197
	# Building Debian compatible packages...
Kamil Trzciński's avatar
Kamil Trzciński committed
198
	make package-deb-fpm ARCH=amd64 PACKAGE_ARCH=amd64
199
	make package-deb-fpm ARCH=386 PACKAGE_ARCH=i386
200
	make package-deb-fpm ARCH=arm PACKAGE_ARCH=armel
Kamil Trzciński's avatar
Kamil Trzciński committed
201
	make package-deb-fpm ARCH=arm PACKAGE_ARCH=armhf
202

203
package-rpm: package-deps package-prepare
Kamil Trzciński's avatar
Kamil Trzciński committed
204
	# Building RedHat compatible packages...
Kamil Trzciński's avatar
Kamil Trzciński committed
205
	make package-rpm-fpm ARCH=amd64 PACKAGE_ARCH=amd64
Kamil Trzciński's avatar
Kamil Trzciński committed
206
	make package-rpm-fpm ARCH=386 PACKAGE_ARCH=i686
Kamil Trzciński's avatar
Kamil Trzciński committed
207 208
	make package-rpm-fpm ARCH=arm PACKAGE_ARCH=arm
	make package-rpm-fpm ARCH=arm PACKAGE_ARCH=armhf
Kamil Trzciński's avatar
Kamil Trzciński committed
209 210

package-deb-fpm:
211
	@mkdir -p out/deb/
Kamil Trzciński's avatar
Kamil Trzciński committed
212
	fpm -s dir -t deb -n $(PACKAGE_NAME) -v $(VERSION) \
Kamil Trzciński's avatar
Kamil Trzciński committed
213
		-p out/deb/$(PACKAGE_NAME)_$(PACKAGE_ARCH).deb \
214 215 216
		--deb-priority optional --category admin \
		--force \
		--deb-compression bzip2 \
217 218
		--after-install packaging/scripts/postinst.deb \
		--before-remove packaging/scripts/prerm.deb \
219
		--url https://gitlab.com/gitlab-org/gitlab-runner \
220
		--description "GitLab Runner" \
221
		-m "GitLab Inc. <support@gitlab.com>" \
222
		--license "MIT" \
223
		--vendor "GitLab Inc." \
224 225 226 227 228
		--conflicts $(PACKAGE_NAME)-beta \
		--conflicts gitlab-ci-multi-runner \
		--conflicts gitlab-ci-multi-runner-beta \
		--provides gitlab-ci-multi-runner \
		--replaces gitlab-ci-multi-runner \
229 230
		--depends ca-certificates \
		--depends git \
231 232
		--depends curl \
		--depends tar \
233
		--deb-suggests docker-engine \
Kamil Trzciński's avatar
Kamil Trzciński committed
234
		-a $(PACKAGE_ARCH) \
Kamil Trzciński's avatar
Kamil Trzciński committed
235
		packaging/root/=/ \
Alessio Caiazza's avatar
Alessio Caiazza committed
236 237
		out/binaries/$(NAME)-linux-$(ARCH)=/usr/lib/gitlab-runner/gitlab-runner \
		out/helper-images/=/usr/lib/gitlab-runner/helper-images/
238
	@if [ -n "$(GPG_KEYID)" ]; then \
239 240 241
		dpkg-sig -g "--no-tty --digest-algo 'sha512' --passphrase '$(GPG_PASSPHRASE)'" \
			-k $(GPG_KEYID) --sign builder "out/deb/$(PACKAGE_NAME)_$(PACKAGE_ARCH).deb" ;\
	fi
242

Kamil Trzciński's avatar
Kamil Trzciński committed
243
package-rpm-fpm:
244
	@mkdir -p out/rpm/
Kamil Trzciński's avatar
Kamil Trzciński committed
245
	fpm -s dir -t rpm -n $(PACKAGE_NAME) -v $(VERSION) \
Kamil Trzciński's avatar
Kamil Trzciński committed
246
		-p out/rpm/$(PACKAGE_NAME)_$(PACKAGE_ARCH).rpm \
247 248
		--rpm-compression bzip2 --rpm-os linux \
		--force \
249 250
		--after-install packaging/scripts/postinst.rpm \
		--before-remove packaging/scripts/prerm.rpm \
251
		--url https://gitlab.com/gitlab-org/gitlab-runner \
252
		--description "GitLab Runner" \
253
		-m "GitLab Inc. <support@gitlab.com>" \
254
		--license "MIT" \
255
		--vendor "GitLab Inc." \
256 257 258 259 260
		--conflicts $(PACKAGE_NAME)-beta \
		--conflicts gitlab-ci-multi-runner \
		--conflicts gitlab-ci-multi-runner-beta \
		--provides gitlab-ci-multi-runner \
		--replaces gitlab-ci-multi-runner \
261 262 263
		--depends git \
		--depends curl \
		--depends tar \
Kamil Trzciński's avatar
Kamil Trzciński committed
264
		-a $(PACKAGE_ARCH) \
Kamil Trzciński's avatar
Kamil Trzciński committed
265
		packaging/root/=/ \
Alessio Caiazza's avatar
Alessio Caiazza committed
266 267
		out/binaries/$(NAME)-linux-$(ARCH)=/usr/lib/gitlab-runner/gitlab-runner \
		out/helper-images/=/usr/lib/gitlab-runner/helper-images/
268
	@if [ -n "$(GPG_KEYID)" ] ; then \
269 270 271 272 273 274 275
		echo "yes" | setsid rpm \
			--define "_gpg_name $(GPG_KEYID)" \
			--define "_signature gpg" \
			--define "__gpg_check_password_cmd /bin/true" \
			--define "__gpg_sign_cmd %{__gpg} gpg --batch --no-armor --digest-algo 'sha512' --passphrase '$(GPG_PASSPHRASE)' --no-secmem-warning -u '%{_gpg_name}' --sign --detach-sign --output %{__signature_filename} %{__plaintext_filename}" \
			--addsign out/rpm/$(PACKAGE_NAME)_$(PACKAGE_ARCH).rpm ;\
	fi
276

Kamil Trzciński's avatar
Kamil Trzciński committed
277 278 279 280
packagecloud: packagecloud-deps packagecloud-deb packagecloud-rpm

packagecloud-deps:
	# Installing packagecloud dependencies...
281
	gem install package_cloud --version "~> 0.3.0" --no-ri --no-rdoc
Kamil Trzciński's avatar
Kamil Trzciński committed
282

283
packagecloud-deb:
Kamil Trzciński's avatar
Kamil Trzciński committed
284
	# Sending Debian compatible packages...
285 286 287
	-for DIST in $(DEB_PLATFORMS); do \
		package_cloud push --url $(PACKAGE_CLOUD_URL) $(PACKAGE_CLOUD)/$$DIST out/deb/*.deb; \
	done
288 289

packagecloud-rpm:
Kamil Trzciński's avatar
Kamil Trzciński committed
290
	# Sending RedHat compatible packages...
291 292 293
	-for DIST in $(RPM_PLATFORMS); do \
		package_cloud push --url $(PACKAGE_CLOUD_URL) $(PACKAGE_CLOUD)/$$DIST out/rpm/*.rpm; \
	done
294 295 296

packagecloud-yank:
ifneq ($(YANK),)
Kamil Trzciński's avatar
Kamil Trzciński committed
297
	# Removing $(YANK) from packagecloud...
298 299
	-for DIST in $(DEB_PLATFORMS); do \
		for ARCH in $(DEB_ARCHS); do \
Kamil Trzciński's avatar
Kamil Trzciński committed
300 301 302
			package_cloud yank --url $(PACKAGE_CLOUD_URL) $(PACKAGE_CLOUD)/$$DIST $(PACKAGE_NAME)_$(YANK)_$$ARCH.deb & \
		done; \
	done; \
303 304
	for DIST in $(RPM_PLATFORMS); do \
		for ARCH in $(RPM_ARCHS); do \
Kamil Trzciński's avatar
Kamil Trzciński committed
305 306
			package_cloud yank --url $(PACKAGE_CLOUD_URL) $(PACKAGE_CLOUD)/$$DIST $(PACKAGE_NAME)-$(YANK)-1.$$ARCH.rpm & \
		done; \
Kamil Trzciński's avatar
Kamil Trzciński committed
307 308
	done; \
	wait
Kamil Trzciński's avatar
Kamil Trzciński committed
309 310 311
else
	# No version specified in YANK
	@exit 1
312
endif
313

Kamil Trzciński's avatar
Kamil Trzciński committed
314 315 316 317 318
s3-upload:
	export ARTIFACTS_DEST=artifacts; curl -sL https://raw.githubusercontent.com/travis-ci/artifacts/master/install | bash
	./artifacts upload \
		--permissions public-read \
		--working-dir out \
Kamil Trzciński's avatar
Kamil Trzciński committed
319
		--target-paths "$(S3_UPLOAD_PATH)/" \
320
		--max-size $(shell du -bs out/ | cut -f1) \
Kamil Trzciński's avatar
Kamil Trzciński committed
321
		$(shell cd out/; find . -type f)
322
	@echo "\n\033[1m==> Download index file: \033[36mhttps://$$ARTIFACTS_S3_BUCKET.s3.amazonaws.com/$$S3_UPLOAD_PATH/index.html\033[0m\n"
Kamil Trzciński's avatar
Kamil Trzciński committed
323

324 325
release_packagecloud:
	# Releasing to https://packages.gitlab.com/runner/
326
	@./ci/release_packagecloud "$$CI_JOB_NAME"
327

328
release_s3: prepare_windows_zip prepare_zoneinfo prepare_index
329 330
	# Releasing to S3
	@./ci/release_s3
331

Alessio Caiazza's avatar
Alessio Caiazza committed
332 333 334 335 336
out/binaries/gitlab-runner-windows-%.zip: out/binaries/gitlab-runner-windows-%.exe
	zip --junk-paths $@ $<
	cd out/ && zip -r ../$@ helper-images

prepare_windows_zip: out/binaries/gitlab-runner-windows-386.zip out/binaries/gitlab-runner-windows-amd64.zip
337

338 339 340 341
prepare_zoneinfo:
	# preparing the zoneinfo file
	@cp $$GOROOT/lib/time/zoneinfo.zip out/

342
prepare_index:
343
	# Preparing index file
344 345
	@./ci/prepare_index

346
release_docker_images:
347
	# Releasing Docker images
348
	@./ci/release_docker_images
349

350 351 352
check-tags-in-changelog:
	# Looking for tags in CHANGELOG
	@git status | grep "On branch master" 2>&1 >/dev/null || echo "Check should be done on master branch only. Skipping."
353
	@for tag in $$(git tag | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+$$" | sed 's|v||' | sort -g); do \
354 355 356 357 358 359
		state="MISSING"; \
		grep "^v $$tag" CHANGELOG.md 2>&1 >/dev/null; \
		[ "$$?" -eq 1 ] || state="OK"; \
		echo "$$tag:   \t $$state"; \
	done

360 361 362 363 364 365 366 367 368 369
prepare_release_checklist_issue: opts ?= ""
prepare_release_checklist_issue: major := $(shell cat VERSION | awk -F'.' '{print $$1}')
prepare_release_checklist_issue: minor := $(shell cat VERSION | awk -F'.' '{print $$2}')
prepare_release_checklist_issue:
	@go run ./scripts/prepare_release_checklist_issue.go \
		-issue-template-file ".gitlab/issue_templates/Release Checklist.md" \
		-major $(major) \
		-minor $(minor) \
		$(opts)

370 371 372 373 374
development_setup:
	test -d tmp/gitlab-test || git clone https://gitlab.com/gitlab-org/gitlab-test.git tmp/gitlab-test
	if prlctl --version ; then $(MAKE) -C tests/ubuntu parallels ; fi
	if vboxmanage --version ; then $(MAKE) -C tests/ubuntu virtualbox ; fi

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
# local GOPATH
$(GOPATH_SETUP): $(PKG_BUILD_DIR)
	mkdir -p $(GOPATH_BIN)
	touch $@

$(PKG_BUILD_DIR):
	mkdir -p $(@D)
	ln -s ../../../.. $@

# development tools
$(GOX): $(GOPATH_SETUP)
	go get github.com/mitchellh/gox

$(MOCKERY): $(GOPATH_SETUP)
	go get github.com/vektra/mockery/.../

clean:
	-$(RM) -rf $(LOCAL_GOPATH)
	-$(RM) -rf $(TARGET_DIR)