6

gitlab ci搭建和使用

 3 years ago
source link: https://blog.csdn.net/oqqYuan1234567890/article/details/89264584
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

关于选择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的效率。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK