Ubuntu GitLab CI Docker ASP.NET Core 2.0 自动化发布和部署(1)
相關(guān)博文:
- Ubuntu 簡單安裝和配置 GitLab
- Ubuntu 簡單安裝 Docker
- Ubuntu Docker 簡單安裝 GitLab
- Ubuntu Docker 安裝和配置 GitLab CI 持續(xù)集成
服務(wù)器版本 Ubuntu 16.04 LTS。
經(jīng)過上面四篇博文中的相關(guān)安裝和配置,我們主要完成了兩個容器的創(chuàng)建和運(yùn)行:gitlab和gitlab-runner(GitLab 站點(diǎn)和 GitLab CI 服務(wù)):
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 696d559ce382 gitlab/gitlab-runner:latest "/usr/bin/dumb-ini..." 5 days ago Up 25 minutes gitlab-runner ff95f354200d gitlab/gitlab-ce:latest "/assets/wrapper" 7 days ago Up 6 days (healthy) 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:8888->22/tcp gitlab本篇博文目的:使用 GitLab CI 腳本編譯 ASP.NET Core 2.0 程序,然后將編譯后的文件傳輸?shù)椒?wù)器上,最后使用 SSH 連接服務(wù)器,并運(yùn)行程序,完成發(fā)布和部署。
簡單來說,就是我們每次使用git push提交完代碼,自動完成發(fā)布和部署。
我們再理一下實(shí)現(xiàn)上面目的關(guān)鍵點(diǎn):
我花了很長時間配置第三步,其實(shí)最后解決也很簡單,當(dāng)然都是馬后炮的結(jié)論?,下面我們分別來進(jìn)行操作。
1. 創(chuàng)建 ASP.NET Core 2.0 示例程序
我自己創(chuàng)建示例程序:http://40.125.206.47/team/hwapp
注:服務(wù)器快過期了,大家可以隨便搞?。
自己創(chuàng)建的話,也很簡單,官方教程:https://www.microsoft.com/net/core#linuxubuntu
我再搬運(yùn)下命令(安裝 .NET Core 2.0,并創(chuàng)建 ASP.NET Core 2.0 示例程序):
$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg $ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg $ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'$ sudo apt-get update $ sudo apt-get install dotnet-sdk-2.0.0$ dotnet new webapi -o hwapp $ cd hwapp最后,綁定下 ASP.NET Core 2.0 程序端口:
public class Program {public static void Main(string[] args){BuildWebHost(args).Run();}public static IWebHost BuildWebHost(string[] args) =>WebHost.CreateDefaultBuilder(args).UseKestrel() //add code.UseUrls($"http://*:8088") //add code.UseStartup<hwapp.Startup>().Build(); }2. .gitlab-ci.yml 文件配置
我的.gitlab-ci.yml文件配置(http://40.125.206.47/team/hwapp/blob/master/.gitlab-ci.yml):
image: microsoft/aspnetcore-build stages:- build- deploy_dev before_script:# Install ssh-agent if not already installed, it is required by Docker.# (change apt-get to yum if you use a CentOS-based image)- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'# Run ssh-agent (inside the build environment)- eval $(ssh-agent -s)# Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store# error: https://gitlab.com/gitlab-examples/ssh-private-key/issues/1# - echo "$SSH_PRIVATE_KEY_DEV"- ssh-add <(echo "$SSH_PRIVATE_KEY_DEV")# For Docker builds disable host key checking. Be aware that by adding that# you are suspectible to man-in-the-middle attacks.# WARNING: Use this only with the Docker executor, if you use it with shell# you will overwrite your user's SSH config.- mkdir -p ~/.ssh- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' build_job:stage: buildonly:- masterscript:- dotnet restore- dotnet build deploy_dev_job:stage: deploy_devenvironment:name: developmentonly:- masterscript:# 發(fā)布程序并部署運(yùn)行- dotnet publish -c Release --output bin/publish- scp -r bin/publish root@$DEPLOY_SERVER_DEV:/home/xishuai/wwwroot/hwapp- ssh root@$DEPLOY_SERVER_DEV "supervisorctl restart hwapp && curl http://localhost:8088/api/values"上面是我最終調(diào)試成功后的.gitlab-ci.yml文件配置,其實(shí)整個的構(gòu)建和發(fā)布流程,從上面的配置中都可以看出。
這里記錄下一些東西:
配置一開始的image,設(shè)置的是我們用于構(gòu)建的鏡像(也就是說后面所有的腳本執(zhí)行,都是在基于這個鏡像創(chuàng)建的容器中),如果不設(shè)置的話,默認(rèn)使用的是我們一開始配置 GitLab CI 填寫的 Docker Image,也可以手動編輯vim /srv/gitlab-runner/config/config.toml進(jìn)行修改,我這里使用的是microsoft/aspnetcore-build鏡像,只用于 ASP.NET Core 應(yīng)用程序的編譯和構(gòu)建。
stage可以理解為臺階,每走一步相當(dāng)于job,當(dāng)然,這里的臺階可以走很多步,需要注意的是,每上一個臺階或者每走一步,都必須基于上一個臺階或上一步執(zhí)行成功,before_script執(zhí)行在這些步驟之前,可以理解為準(zhǔn)備工作。
environment將執(zhí)行的job歸納為哪一種執(zhí)行環(huán)境,你可以設(shè)置開發(fā)環(huán)境和正式環(huán)境,我們可以通過通過后臺進(jìn)行查看:
3. GitLab CI 服務(wù)器使用 SSH 連接到測試服務(wù)器
什么意思呢?就是我們需要在 GitLab CI 構(gòu)建環(huán)境中,使用 SSH 連接到測試服務(wù)器,這樣我們才可以做接下來的一些操作。
官方配置:SSH keys when using the Docker executor
.gitlab-ci.yml示例配置:
before_script:# Install ssh-agent if not already installed, it is required by Docker.# (change apt-get to yum if you use a CentOS-based image)- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'# Run ssh-agent (inside the build environment)- eval $(ssh-agent -s)# Add the SSH key stored in SSH_PRIVATE_KEY_DEV variable to the agent store- ssh-add <(echo "$SSH_PRIVATE_KEY_DEV")# For Docker builds disable host key checking. Be aware that by adding that# you are suspectible to man-in-the-middle attacks.# WARNING: Use this only with the Docker executor, if you use it with shell# you will overwrite your user's SSH config.- mkdir -p ~/.ssh- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'# In order to properly check the server's host key, assuming you created the# SSH_SERVER_HOSTKEYS variable previously, uncomment the following two lines# instead.# - mkdir -p ~/.ssh# - '[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts'在進(jìn)行配置之前,我們需要理一下這個步驟,要不然思路容易混亂,會造成一些問題,可以參考這篇文章:Fixing 'Enter passphrase for /dev/fd/63' in a Gitlab CI job
需要強(qiáng)調(diào)一點(diǎn):別在 GitLab CI 容器中進(jìn)行 SSH 配置,因?yàn)?CI 構(gòu)建腳本執(zhí)行在另外的容器中,并且這個容器是動態(tài)進(jìn)行創(chuàng)建的,也沒辦法在這個動態(tài)容器中進(jìn)行配置(指的是手動生成 RSA 密鑰)。
所以,我們只能手動生成 RSA 密鑰,然后強(qiáng)制添加到容器中的 SSH 配置中(通過 RSA 密鑰內(nèi)容)。
配置步驟:
首先,在任何一臺服務(wù)器上,創(chuàng)建 RSA 無密碼的密鑰:
$ ssh-keygen -t rsa -P '' $ cat /root/.ssh/id_rsa然后復(fù)制 RSA 密鑰內(nèi)容,添加到/Project/Settings/Pipelines的Secret variables配置中(命名為SSH_PRIVATE_KEY_DEV):
這里需要特別注意,復(fù)制內(nèi)容為(包含開頭和結(jié)尾的注釋信息):
-----BEGIN RSA PRIVATE KEY----- xxxxxxx -----END RSA PRIVATE KEY-----我一開始復(fù)制沒有包含注釋信息,然后就一直報(bào)下面的錯誤:
錯誤代碼:
$ ssh-add <(echo "$SSH_PRIVATE_KEY_DEV") Enter passphrase for /dev/fd/63: ERROR: Job failed: exit code 1這里的$SSH_PRIVATE_KEY_DEV,就是上面我們在Secret variables中,添加的 RSA 密鑰內(nèi)容。
錯誤信息就是說需要輸入 RSA 密鑰的密碼,但我創(chuàng)建的確實(shí)是無密碼的 RSA 密鑰,也就是說這個密鑰是無效的,我被這個問題折磨了好幾天?,其他人的記錄:
- "Enter passphrase for /dev/fd/63" error(有我的回復(fù)?)
- "Enter passphrase for /dev/fd/63" error
- https://gitlab.com/gitlab-examples/ssh-private-key/-/jobs/376082(受這個兄弟的啟發(fā))
配置好這一步之后,然后重新測試下,我們就可以看到下面的執(zhí)行信息了:
$ ssh-add <(echo "$SSH_PRIVATE_KEY_DEV") Identity added: /dev/fd/63 (/dev/fd/63)接著我們需要將這個 RSA 密鑰對應(yīng)的公鑰,上傳到需要連接到的服務(wù)器(也就是我們的測試服務(wù)器),命令如下:
$ ssh-copy-id root@40.125.201.75到此,GitLab CI 中 SSH 的配置基本上完成了,你可以在.gitlab-ci.yml中添加連接腳本,進(jìn)行測試:
- ssh root@$DEPLOY_SERVER_DEV "ls && cd /"一開始,我們說到使用scp進(jìn)行服務(wù)器之間的文件傳輸,因?yàn)閟cp可以基于 SSH 連接進(jìn)行傳輸文件,所以我們直接進(jìn)行文件傳輸了,示例代碼:
- scp -r bin/publish root@$DEPLOY_SERVER_DEV:/home/xishuai/wwwroot/hwappscp命令參考:http://www.runoob.com/linux/linux-comm-scp.html
4. 使用 Supervisor 進(jìn)行站點(diǎn)程序的進(jìn)程管理
可以參考之前的文章:Ubuntu 安裝和使用 Supervisor(進(jìn)程管理)
這里貼一下,supervisorctl的常用命令:
| supervisorctl stop program_name | 停止某個進(jìn)程 |
| supervisorctl start program_name | 啟動某個進(jìn)程 |
| supervisorctl restart program_name | 重啟某個進(jìn)程 |
| supervisorctl stop all | 停止全部進(jìn)程 |
| supervisorctl reload | 載入最新的配置文件,停止原有進(jìn)程并按新的配置啟動、管理所有進(jìn)程 |
| supervisorctl update | 根據(jù)最新的配置文件,啟動新配置或有改動的進(jìn)程,配置沒有改動的進(jìn)程不會受影響而重啟 |
5. 最終效果
Pipelines 管道(地址:http://40.125.206.47/team/hwapp/pipelines)
Build_Job 構(gòu)建任務(wù)(地址:http://40.125.206.47/team/hwapp/-/jobs/113)
Deploy_Dev_Job 發(fā)布和部署任務(wù)(地址:http://40.125.206.47/team/hwapp/-/jobs/115)
寫在最后:
- GitLab CI & ASP.NET Core 2.0 發(fā)布和部署(完成):使用 CI 腳本編譯程序,然后將編譯后的文件傳輸?shù)椒?wù)器上,最后運(yùn)行程序,完成發(fā)布和部署。
- GitLab CI & ASP.NET Core 2.0 & Docker 發(fā)布和部署(下篇):項(xiàng)目中添加Dockerfile文件,使用 CI 腳本構(gòu)建自定義鏡像,然后在服務(wù)器上拉取并創(chuàng)建相應(yīng)容器,最后啟動容器,完成發(fā)布和部署。
總結(jié)
以上是生活随笔為你收集整理的Ubuntu GitLab CI Docker ASP.NET Core 2.0 自动化发布和部署(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么在我的世界服务器注册,我的世界服务器
- 下一篇: notes系统服务器地址,notes怎么