Gitlab - Ubuntu18註冊gitlab-runner並激活CI/CD
Ubuntu18註冊gitlab-runner並激活CI/CD
gitlab-runner安裝
下載
# 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
建立 GitLab CI 使用者
useradd --comment 'gitLab-runner' --create-home gitlab-runner --shell /bin/bash
執行服務
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner gitlab-runner start
gitlab-runner註冊
開啟 gitlab 專案 -> 設定 -> CI / CD -> Runners 設定,獲取令牌
執行以下命令開始註冊:
sudo gitlab-runner register
輸入GitLab例項URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com ) https://xxx.xxx
輸入註冊令牌來註冊 Runner
Please enter the gitlab-ci token for this runner xxx
輸入 Runner 說明
Please enter the gitlab-ci description for this runner [hostame] gitlab-runner
輸入 Runner 的 tags
Please enter the gitlab-ci tags for this runner (comma separated): tag
輸入 Runner 執行方式
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell: shell
驗證
runner列表
runner配置修改
.gitlab-ci.yml
要實現CI/CD,首先要在專案根路徑下建立.gitlab-ci.yml檔案,我的目錄結構如下:
.gitlab-ci.yml檔案內容:
# CI/CD管道分三步,這裡可以根據需求自行配置 stages: - build - test - deploy build: stage: build # 測試構建 script: - gradle clean build -x test test: stage: test # 這裡可以寫一些測試相關的指令碼 script: - echo SKIPPING TEST deploy: stage: deploy script: # 修改部署指令碼檔案型別為可執行檔案 - chmod +x .gitlab-ci/deploy.sh # 執行指令碼 - sh .gitlab-ci/deploy.sh only: # 只有master分支執行這個步驟 - /^master.*$/ tags: # 對應註冊runner時候的tag名 - tag
deploy.sh檔案內容:
#!/usr/bin/env bash echo "Start deploying!" gradle build # 把檔案從本地傳到伺服器上去 scp /home/gitlab-runner/builds/n637Af4S/0/root/project-test/build/libs/project-test-0.0.1-SNAPSHOT.jar root@伺服器ip:/usr/local/project_test/project-test-0.0.1-SNAPSHOT.jar # 執行伺服器上的部署指令碼檔案 ssh root@伺服器ip "sh /usr/local/project_test/deploy.sh" echo "Deploy jar success!"
執行deploy.sh檔案需要兩個前提條件,一是需要gitlat伺服器可以免密登入待部署伺服器,而是要在待部署伺服器上也建立一個deploy.sh檔案。成功執行待部署伺服器上的deploy.sh檔案需要資料夾建立格式和我這裡相同。
待部署伺服器上的deploy.sh檔案:
#!/bin/bash #defined export JAVA_HOME=/usr/local/java/jdk1.8.0_231 PID=`ps -ef | grep java | grep project-test | awk '{print $2}'` if [ -z $PID_EXIST];then echo the process $PID is not exist else echo the process $PID exist kill -9 $PID fi echo "remover jar file" rm -rf /jar/project-test* echo "copy jar to dir" cp project-test-0.0.1-SNAPSHOT.jar /usr/local/project_test/jar/project-test.jar echo "start jar" nohup java -jar /usr/local/project_test/jar/project-test.jar >/dev/null 2>&1 & echo "started"
Q:提交程式碼後沒有觸發,一直停留在pending
A:報錯:This job is stuck, because you don't have any active runners that can run this job.
原因:註冊gitlab runner 的時候,有一步提示:Can run untagged jobs: [false/true],預設值為false。
這句話的意思是:是否在沒有標記tag的job上執行,如果選擇預設值false,那沒有標記tag的程式碼提交是不會觸發gitlab runner的,如果做測試,最好填true。
Q: bash:行91: git:未找到命令
A:gitlab伺服器需要安裝git
Q:bash:行90: gradle:未找到命令
A:gitlab伺服器需要安裝gradle