1. 准备Makefile
根目录下创建名为Makefile的文件。内容如下:
代码语言:javascript复制export VERSION=1.0.0
export ENV=prod
export PROJECT=bpsp
TOPDIR=$(shell pwd)
# OBJ_DIR=$(OUTPUT)/$(PROJECT)
# OBJTAR=$(OBJ_DIR).tar.gz
SOURCE_BINARY_DIR=$(TOPDIR)/bin
SOURCE_BINARY_FILE=$(SOURCE_BINARY_DIR)/$(PROJECT)
SOURCE_MAIN_FILE=main.go
BUILD_TIME=`date %Y%m%d%H%M%S`
BUILD_FLAG=-ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)"
# all: build pack
all: build
@echo "ALL DONE"
@echo "Program: " $(PROJECT)
@echo "Version: " $(VERSION)
@echo "ENV: " $(ENV)
build:
@echo "start go build...."
@rm -rf $(SOURCE_BINARY_DIR)/*
@go build $(BUILD_FLAG) -o $(SOURCE_BINARY_FILE) $(SOURCE_MAIN_FILE)
# pack:
# @echo "nrpacking...."
# @tar czvf $(OBJTAR) -C $(OBJ_DIR) .
其中,bpsp可以换成任意项目名称。
2. 准备.gitlab.ci.yml
2.1绑定Runner到项目
手动绑定:进入项目>>Settings>>Runners>>Set up a specific Runner manually:
image.png
2.2下载GitLab Runner
代码语言:javascript复制# CentOS
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
sudo yum install gitlab-ci-multi-runner
2.3注册GitLab Runner
CentOS官方文档说明 其中,token密码从2.1图中打码的地方获取。
2.4查看Runner状态
代码语言:javascript复制sudo gitlab-runner list
# Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
# my-runner Executor=docker Token=ShLLfihpPJYzm717qa41 URL=https://gitlab.com
2.5 .gitlab-ci.yml编写
代码语言:javascript复制image: golang:alpine
before_script:
- export
- mkdir -p /go/src/gitlab.com/jaderabbit/bpsp
- cp -r . /go/src/gitlab.com/jaderabbit/bpsp/
- cd /go/src/gitlab.com/jaderabbit/bpsp/
stages:
- build
# jobs
build-jobs:
stage: build
script:
- go build .
2.6本地测试
代码语言:javascript复制gitlab-runner exec docker build-jobs