公司前端大佬由於某些緣由離職了,走的比較匆忙,本身以前一直不多接觸這方面的東西,一直都是隻知其一;不知其二。這兩天我一邊學,一邊動手搭建,同時記錄整個搭建過程。html
這是一系列文章,從搭建 Gitlab 到 安裝、註冊Gitlab-runner 再到兩者結合去部署一個簡單的項目,經過這幾篇文章,你將學會如何在 Gitlab 上自動化打包部署本身的項目。前端
系列文章一共有四篇,包括:node
因爲本身一直作的是前端,對於Linux我不算熟練,若有錯誤的地方,請你們指出。linux
原文地址:安裝GITLAB-RUNNERgit
這篇是系列的第二篇,咱們會安裝 Gitlab-runner 並編寫一個簡單的 .gitlab-ci.yml 文件,看一下運行效果。
github
兩臺服務器,個人都是Linux CentOS 7.6 64位 一臺用須要安裝 Gitlab,關於如何安裝 Gitlab ,可查看這篇文章 阿里雲安裝GITLAB筆記。 另外一臺用於安裝 Gitlab-runner。docker
# Linux x86-64
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Linux x86
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386
# Linux arm
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
複製代碼
sudo chmod +x /usr/local/bin/gitlab-runner
複製代碼
curl -sSL https://get.docker.com/ | sh
複製代碼
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
複製代碼
sudo gitlab-runner register
複製代碼
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
# 沒有域名因此填的是IP
http://xx.xx.xxx.xx:8888
複製代碼
這裏的token分爲兩種 一種是 Shared Runner ,該 Runner 全部項目均可以使用
位置:頂部設置圖標🔧 -> 左側欄Overview -> Runner shell
另外一種是 Specific Runner ,該 Runner 指定具體某個項目纔可以使用
位置:進入某個項目 -> 左側欄Setting -> CI/CD -> 在內容區域找到Runners一項,點擊展開
Please enter the gitlab-ci token for this runner
# 這裏咱們使用 Shared Runner Token
xxxxxxx
複製代碼
Please enter the gitlab-ci description for this runner
test-gitlab-runner-description
複製代碼
Please enter the gitlab-ci tags for this runner (comma separated)
my-tag
複製代碼
這裏我使用 shell。ruby
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell
複製代碼
若是一切正常的話咱們會看到bash
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 複製代碼
Please enter the Docker image (eg. ruby:2.1):
alpine:latest
複製代碼
咱們回到Share Runners 就能夠看到咱們添加的 runner 了
# 定義 stages(階段,會依次執行)
stages:
- install_deps
- build_prod
- deploy_prod
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- node_modules/
- dist
# 安裝構建依賴
install_deps_job:
stage: install_deps
# 在哪一個分支纔會執行腳本
only:
# - dev
# - release
- master
script:
- echo '模擬安裝構建依賴階段'
tags:
- my-tag
# 構建預prod環境src目錄下應用
build_prod_job:
stage: build_prod
only:
- master
script:
- echo '構建預prod環境src目錄下應用階段'
tags:
- my-tag
# 部署生產環境
deploy_prod_job:
stage: deploy_prod
only:
- master
script:
- echo '部署生產環境階段'
tags:
- my-tag
複製代碼
而後你可能會看到報錯
Running with gitlab-runner 11.9.2 (fa86510e)
on desc Z1UPKJjn
Using Shell executor...
Running on iZwz98jvb8bcz40ko474qsZ...
bash: line 68: git: command not found
bash: line 66: cd: /home/gitlab-runner/builds/Z1UPKJjn/0/main-group/main-project: No such file or directory
ERROR: Job failed: exit status 1
複製代碼
報錯的緣由是個人服務器是一臺只安裝了 Gitlab-runner 的服務器,根據報錯提示,須要 git 來拉取 Gitlab 服務器上的代碼,因此咱們安裝 git:
yum -y install git
複製代碼
而後使用
git --version 查看 git 是否安裝成功
複製代碼
以後從新執行pipline或提交代碼,能夠看到一切運行正常:
This build is stuck, because the project doesn't have any runners online assigned to it. Go to Runners page
,這是由於未找到對應的 runner,緣由一:多是gitlab-runner註冊失敗,緣由二:多是.gitlab-ci.yml配置文件裏面 tags 沒有匹配到已註冊可用的 runner,在 stage 中加入對應 runner 註冊時輸入的 tags 便可。Install GitLab Runner manually on GNU/Linux Registering Runners GitLab Runner commands GitLab CI/CD Pipeline Configuration Reference Docker搭建本身的Gitlab CI Runner