gitlab ci搭建和使用

关于选择ce/ee版本,请看 https://about.gitlab.com/installation/ce-or-ee/, 这里选择ee版本,如果方便日后加入ee版的功能,如果不购买license,功能就和ce版本差不多

1. Install

first install


sudo apt-get update
sudo apt-get install -y curl openssh-server ca-certificates
sudo apt-get install -y postfix
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash
sudo apt-get install gitlab-ee 

if you need configure, refer to the ouput tips:

+# Gitaly configuration file
+# This file is managed by gitlab-ctl. Manual changes will be
+# erased! To change the contents below, edit /etc/gitlab/gitlab.rb
+# and run:
+# sudo gitlab-ctl reconfigure

2. 修改默认端口

加一下gitlab.example.com 的解析
nginx[‘listen_port’] = 8092
gitlab-ctl reconfigure

如果需要启用https,请参考其他教程

安装gitlab runner

选一台机器安装runner,这里选一台专门的编译机器
https://docs.gitlab.com/runner/install/linux-repository.html
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash

sudo apt-get install gitlab-runner
gitlab-runner register

注册token在root管理员的amin->runners界面可以找到

注册的时候我选的是shell环境,因为这是专门的编译机器,如果用docker的话,如果更新image的时候需要重新绑定,比较麻烦,直接用runner机器的环境就可以。
值得说明的是,runner支持tag,在编写ci脚本的时候,可以指定哪些tag的机器去执行。

注册进来之后,可以对某些project打开ci runner功能,默认是所有项目都可以使用这个ci runner。ci也可以指定某些tag的runner可以运行,对于sandbox和formal环境,可以使用不同的tag,做不同的测试比较方便。

测试

以go开发作为一个例子,新建一个工程

.
├── env.sh
├── .gitlab-ci.yml
├── main.go
├── Makefile
└── README.md

env.sh

#!/bin/bash
export GOROOT=/opt/tools/go
export GOPATH=/opt/go_work
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN:$GOROOT/bin

main.go

package main

import (
	"github.com/gorilla/mux"
	"github.com/miaomiao3/log"
	"net/http"
	"time"
)

func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	//w.WriteHeader(200)
	w.Write([]byte(`{"version": 2}`))
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", HealthCheckHandler)
	httpServer := &http.Server{
		Addr: "0.0.0.0:8082",
		// Good practice to set timeouts to avoid Slowloris attacks.
		WriteTimeout: time.Second * 15,
		ReadTimeout:  time.Second * 15,
		IdleTimeout:  time.Second * 60,
		Handler:      r, // Pass our instance of gorilla/mux in.
	}
	log.Info(httpServer.ListenAndServe())
}

Makefile

main:
	go build main.go

.gitlab-ci.yaml

stages:
  - build
  - test
  - deploy

try_build:
  stage: build
  script: 
    - source env.sh
    - make main

try_test:
  stage: test
  script: 
    - echo 'type your test script here'
    - echo 'test finished'

.sandbox_trigger: &sandbox_trigger
  only:
    - /develop/

.formal_trigger: &formal_trigger
  only:
    - /master/

sandbox_deploy:
  stage: deploy
  <<: *sandbox_trigger
  script: 
    - echo 'type your sandbox deploy script here'
    - echo 'deploy finished'

formal_deploy:
  stage: deploy
  <<: *formal_trigger
  script: 
    - echo 'type your formal deploy script here'
    - echo 'deploy finished'

这个ci文件是一个简单例子,详细的ci语法请参考https://docs.gitlab.com/ee/ci/yaml/

推送,然后就可以看到ci成功的提示,这里要注意一下gitlab-runner这个用户的权限(gitlab-runner在安装的时候添加的默认用户)

后续

根据工作环境,打造适合部门的ci模板,结合k8s,可以有效提高devops的效率。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值