環境說明
一台git服務器(192.168.169.7),安裝gitlab,docker。
一台web服務器(192.168.169.6),安裝git,gitlab runner,docker,dotnet sdk。
為了方便講述,給兩台服務器取個別名吧,7醬和6醬😄
git服務器准備
安裝gitlab
gitlab提供多種安裝方式,7醬是Ubuntu系統😄,雖然docker方式簡直不要太簡單,但是還是記錄一下Ubuntu下普通方式安裝gitlab。
安裝依賴項
sudo apt-get install -y curl openssh-server ca-certificates
使用postfix來發郵件通知
sudo apt-get install -y postfix
安裝gitlab包
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
安裝
sudo EXTERNAL_URL="http://gitlab.example.com" apt-get install gitlab-ce
雖然有域名,但是懶得配置,url就填寫ip,比如http://192.168.169.7:8092,安裝之后第一次訪問這個地址的用戶就是管理員賬號了。
郵件配置,注意配置的郵箱要開啟smtp
安裝docker
如果有舊版本,先卸載
sudo apt-get remove docker docker-engine docker.io
一些前提設置
sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
開始安裝docker
sudo apt-get update
sudo apt-get install docker-ce
如果要安裝指定版本的,可以先查一下有哪些特定版本,然后再安裝指定的版本
apt-cache madison docker-ce
sudo apt-get install docker-ce=5:19.03.1~3-0~ubuntu-bionic docker-cd-cli=5:19.03.1~3-0~ubuntu-bionic containerd.io
設置自動啟動docker
systemctl enable docker
systemctl start docker
如果不是root用戶,添加當前用戶到docker用戶組
sudo usermod -aG docker $USER
[可選項],設置國內鏡像,通過修改docker配置文件,添加register-mirrors鍵值。如果沒有daemon.json文件,請新增。
sudo vi /etc/docker/daemon.json
{ "registry-mirrors": [ "https://registry.docker-cn.com", "http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn","https://cr.console.aliyun.com" ] }
重啟docker
sudo service docker restart
hello world from docker
docker run --rm hello-world
web服務器准備
6醬是CentOS😄
安裝git
sudo yum install -y git
配置git賬號
過程:略
安裝gitlab runner
首先在gitlab倉庫的設置中找到Runners選項,可以看到設置runner的步驟。

安裝gitlab-runner。這里以二進制文件方式安裝,其他安裝方式自行選擇。
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 sudo chmod +x /usr/local/bin/gitlab-runner sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner sudo gitlab-runner start
注冊gitlab runner,按照提示,一步一步填寫。
sudo gitlab-runner register

提示注冊成功,然后在項目倉庫runners這里可以看到剛剛激活的runner。

點擊可以查看runner詳情,最好將【Can run untagged jobs】改為【Yes】,否則提交代碼的時候只有tag才會觸發構建任務。

安裝dotnet sdk
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
sudo yum install dotnet-sdk-3.0
准備aspnetcore項目
在項目根目錄添加.gitlab-ci.yml文件,Dockerfile在項目目錄中。
stages: - deploy deploy_job: stage: deploy only: - master script: - cd /home/gitproject - rm -rf /home/gitproject/gitlabrunner - git clone git@192.168.169.7:rookies/gitlabrunner.git - cd ./gitlabrunner - dotnet build --configuration Release - rm -rf /home/dockerproject/gitlabrunner - docker kill gitlabci - docker rm gitlabci - docker rmi gitlabrunnerimage - dotnet publish -c Release --output /home/dockerproject/gitlabrunner - cd /home/dockerproject/gitlabrunner - docker build -t gitlabrunnerimage . - docker run -d -v /home/dockerproject/gitlabrunner:/app -p 3126:3126 --name gitlabci --restart always gitlabrunnerimage

pipeline任務構建過程

本次由於網絡原因,構建失敗,但是通過此番操作,可以實現利用gitlab runner來達到CI/CD的目的。
