使用Gitlab-CI 實現NetCore項目Docker化並部署到阿里雲K8S
先行條件:
1.了解NetCore項目基礎命令,如dotnet publish 等幾個常用命令。
2.了解Docker基礎命令
3.了解centos基礎命令
部署步驟:
大致會分為如下幾個步驟,后面會詳細解析
1.安裝 Runner
2.注冊 Runner
3.安裝Docker環境
4.編寫Dockerfile 腳本
5.編寫 .gitlab-ci.yml 腳本
6.Push 代碼
步驟分解:
1.安裝 Runner (文章末尾有專門的命令解析參考地址)
Runner簡介:Runner是配合 gitlab-ci 一起使用的,它可以拉取gitlab的代碼並且執行一些命令,例如編譯代碼 發布代碼等。並且最后將結果通知給Gitlab-CI。
我們Runner可以安裝在任何系統的任何位置,我這里把他安裝在centos系統中。
安裝步驟如下:
1.1.為您的系統下載其中一個二進制文件:
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
1.2.賦予它執行權限:
sudo chmod +x /usr/local/bin/gitlab-runner
1.3.創建GitLab CI用戶:
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
1.4.安裝並作為服務運行:
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
2.注冊 Runner
注冊ranner是指讓你剛剛安裝的 runner與你的gitlab關聯起來,使他有權限來訪問你的代碼並且發送通知給gitlab-ci。
2.1.執行注冊命令
先開打如下圖的頁面找到 URL 和 Token
然后執行如下命令:(文章末尾有專門的命令解析參考地址)
sudo gitlab-runner register
會提示您輸入 URL和Token 先把上圖的URL 復制 粘貼 回車。然后在復制Token 粘貼 回車。
下面就會提示輸入 Please enter the gitlab-ci description for this runner (輸入描述)可直接跳過或者隨便輸入或稍后再GitLabUI中輸入都可以。
接着提示輸入 Please enter the gitlab-ci tags for this runner (comma separated) (輸入標簽信息 )可直接跳過或稍后再GitLabUI中輸入都可以。
接着提示輸入 Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh (輸入Runner執行程序)我們這里輸入 : shell 然后回車。
最后到這里就說明runner已經安裝成功並且跟項目關聯成功了,我們可以在 GitLab中查看 如下圖:
3.安裝Docker環境
安裝Docker環境是為了我們把應用打包成 DockerImage 后上傳到 阿里雲的鏡像倉庫中 用於k8s的應用部署。
安裝Docker大家自行 google 這里不再介紹如何安裝。
4.編寫Dockerfile 腳本
如果是NetCore 項目Dockerfile 腳本應該在項目的根目錄下面(官方推薦)如下圖是我的項目。
Dockerfile 內容:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
COPY . /root/publish/api.config.internal
WORKDIR /root/publish/api.config.internal
ENV ASPNETCORE_URLS http://*:7012
ENV ConnectionStrings_ConfigDbContext server=10.10.10.228;port=5566;user id=uat_ApiConfigCenterManage_Web;password=4g4TesrWg4;database=ApiConfigCenterDB;persistsecurityinfo=True;SslMode=None
ENV LoggerSettings_ClientName configcenter.internalapi_aliyun_log
ENV LoggerSettings_ServerUrl http://ulog.colipu.com:8080/v1/logs
ENV LoggerSettings_Level error
ENTRYPOINT ["dotnet", "Api.ConfigCenter.InternalApi.dll"]
以上為的Docfile腳本,大家可以參考一下,腳本中必須的命令是 FROM COPY WORKDIR ENTRYPOINT 這幾個。至於為什么這樣寫大家可參考Docker官方文檔。
5.編寫 .gitlab-ci.yml 腳本
如果你的是netCore項目並且有解決方案,那么這個腳本應該創建在解決方案的根目錄下。
如下圖:
.gitlab-ci.yml 腳本內容:
stages: - build-image-internalapi - build-image-manageapi job1: stage: build-image-internalapi only: refs: - tags variables: - $CI_COMMIT_TAG =~ /^internalapi-.*/ script: # The output path is relative to the position of the csproj-file - dotnet publish -c Release -o ../publishinternalapi ./Api.ConfigCenter.InternalApi/Api.ConfigCenter.InternalApi.csproj --configfile Nuget.config - docker login -u sunqiang@aliyun -p Aa123456 registry-vpc.cn-shanghai.aliyuncs.com - cd publishinternalapi/ - docker build -t registry-vpc.cn-shanghai.aliyuncs.com/clp-cip-apisystem/cip-configcenter-internalapi:$CI_COMMIT_TAG . - docker push registry-vpc.cn-shanghai.aliyuncs.com/clp-cip-apisystem/cip-configcenter-internalapi:$CI_COMMIT_TAG job2: stage: build-image-manageapi only: refs: - tags variables: - $CI_COMMIT_TAG =~ /^manageapi-.*/ script: # The output path is relative to the position of the csproj-file - dotnet publish -c Release -o ../publishmanageapi ./Api.ConfigCenter.ManageApi/Api.ConfigCenter.ManageApi.csproj --configfile Nuget.config - docker login -u sunqiang@aliyun -p Aa123456 registry-vpc.cn-shanghai.aliyuncs.com - cd publishmanageapi/ - pwd - docker build -t registry-vpc.cn-shanghai.aliyuncs.com/clp-cip-apisystem/cip-configcenter-manageapi:$CI_COMMIT_TAG . - docker push registry-vpc.cn-shanghai.aliyuncs.com/clp-cip-apisystem/cip-configcenter-manageapi:$CI_COMMIT_TAG
注意:yml腳本對格式要求非常嚴格,所以不能有一點兒錯。
有了這個腳本之后,在你每次Push代碼的時候GitLab會檢測到項目的根目錄是否有.gitlab-ci.yml 文件 如果有就會執行里面的內容。
腳本解釋:(文章末尾有專門的命令解析參考地址)
stages:表示 定義構建的階段,我這里定義了2個階段,因為我這個解決方案下面有兩個項目。也可以定義一個那就是把腳本全部寫在一塊了。
job1:表示第一個階段的名字,這個是自定義的。
stage:定義job stage (默認:test
) 表示要執行哪個階段
only:定義一列git的分支。 refs tags 表示只通過標簽的形式觸發job任務來構建應用(如果不這樣寫,那你每次推送代碼的時候都會執行job)
variables:表示在job中是可以使用關鍵字variables
來定義job變量。
$CI_COMMIT_TAG =~ /^manageapi-.*/ 表示我以標簽 manageapi-v1 為例 要推送的項目 如下截圖
script:必須存在的。在這里面的可以寫任何腳本命令。其實就是命令行。
當最后執行完 docker push 命令的時候我的阿里雲鏡像倉庫就可以看到了
最后就是通過當前鏡像部署K8S應用了。
參考文檔:
Gitlab CI yaml官方配置文件翻譯:https://segmentfault.com/a/1190000010442764
Gitlab Runner:https://docs.gitlab.com/runner/install/linux-manually.html