目的:每提交一次代碼即觸發一次sonarqube完成代碼掃描
一、基本認知
1、gitlab-ci
GitLab-CI就是一套配合GitLab使用的持續集成系統(當然,還有其它的持續集成系統,同樣可以配合GitLab使用,比如Jenkins)。而且GitLab8.0以后的版本是默認集成了GitLab-CI並且默認啟用的。
2、gitlab-runner
GitLab-Runner是配合GitLab-CI進行使用的。一般地,GitLab里面的每一個工程都會定義一個屬於這個工程的軟件集成腳本,用來自動化地完成一些軟件集成工作。當這個工程的倉庫代碼發生變動時,比如有人push了代碼,GitLab就會將這個變動通知GitLab-CI。這時GitLab-CI會找出與這個工程相關聯的Runner,並通知這些Runner把代碼更新到本地並執行預定義好的執行腳本。
Runner類型
GitLab-Runner可以分類兩種類型:Shared Runner(共享型)和Specific Runner(指定型)。
Shared Runner:這種Runner(工人)是所有工程都能夠用的。只有系統管理員能夠創建Shared Runner。
Specific Runner:這種Runner(工人)只能為指定的工程服務。擁有該工程訪問權限的人都能夠為該工程創建specific runner。
二、安裝gitlab-runner(可以在gitlab服務器上,也可以在別的服務器上)
1、下載包 gitlab-runner-12.7.0-1.x86_64.rpm ,地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7/
# yum install -y gitlab-runner-12.7.0-1.x86_64.rpm
或者直接下載源,在線安裝。
# curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash # yum install gitlab-runner
2、注冊gitlab-runner
① Shared Runner注冊的url和token:
② Specific Runner注冊的url和token:
③ 注冊(shared runner)
# gitlab-runner register
[root@test2 SHELL]# gitlab-runner register Runtime platform arch=amd64 os=linux pid=53435 revision=d0b76032 version=12.0.2 Running in system-mode. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): http://172.16.19.104:10081/ #輸入URL Please enter the gitlab-ci token for this runner: -rwbBw2y7GmL7smxuoxt #輸入TOKEN Please enter the gitlab-ci description for this runner: [test2.laozios.com]: my-runner #后面腳本需要用到 Please enter the gitlab-ci tags for this runner (comma separated): sonar #后面腳本需要用到 Registering runner... succeeded runner=-rwbBw2y Please enter the executor: parallels, kubernetes, virtualbox, docker+machine, docker-ssh+machine, docker, docker-ssh, shell, ssh: shell #通過shell Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
④ 查看已注冊的runner
三、編寫.gitlab-ci.yml文件
sonar_preview: stage: test script: - /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=java -Dsonar.sources=. -Dsonar.host.url=http://172.16.68.154:9000 -Dsonar.login=13585323c4c8ac257c590d6e49c7b59dda5192f8 only: - master tags: - my-runner
四、提交代碼測試