iT邦幫忙

2021 iThome 鐵人賽

DAY 20
1
DevOps

DevOps 好想學!新手也能打造雲端 Study Lab系列 第 20

Day20 - GitLab CI 更新 Manifest Image Tag

如何建立 Deploy Stage

Day15 的教學裡,我們透過 Helm Chart 在 Kubernetes 安裝應用程式。在實際的軟體開發過程中,同樣的程式我們會分成 Dev、Stage、Prod 三套環境,分別提供撰寫、測試、部屬功能,每個環境的配置如 Replica 、 Image Tag 也會有些許不同,若想利用同個 Helm Chart 創建不同配置的環境,可以建立多個 values 檔案來達成需求。

https://ithelp.ithome.com.tw/upload/images/20210920/20139235UDourJpbva.png

有了多個 values 檔案,GitLabCI 更新應用程式的方式就是修改對應環境的 values 檔案,將裡面的 Image Tag 修改到正確版本,透過 GitOps 的方式,就可以將應用程式同步到新的版本。

https://ithelp.ithome.com.tw/upload/images/20210920/20139235MVMSpxMGLr.png

今天的目標是完成 Deploy Stage ,有兩個任務需要完成

  • 在 Helm Chart 建立多個 Values 檔案
  • 使用 GitLabCI 更新 Values 檔案設置的 Image Tag

在 Helm Chart 建立 Multiple Values Files

  1. 進入 Cloud Shell 網站,點擊終端機輸入指令

  2. 建立 values.stage.yamlvalues.production.yaml 檔案

cd ~/webapp
cp values.yaml values.stage.yaml
cp values.yaml values.production.yaml
ls

(輸出結果)

charts  Chart.yaml  templates  values.production.yaml  values.stage.yaml  values.yaml
  1. 建立 Commit 並 Push 到 GitLab 上
git add .
git commit -m "add stage and production values.yaml"
git push origin master
  1. 輸入 GitLab 帳號密碼後按Enter
Username for 'https://gitlab.com': 
Password for 'https://user@gitlab.com':
  1. GitLab 網站,找到 Webapp Manifest 的 Repository , 確定裡面有三個 values 檔案

https://ithelp.ithome.com.tw/upload/images/20210920/2013923561wgyZyrqq.png

建立 Personal Access Token

要讓 GitLabCI 可以更新 Values 檔案,需要有存取 Git Repository 的權限,可以藉由 Access Token 的方式,這裡就來介紹如何建置。

  1. GitLab Personal Access Tokens 網站

  2. 輸入以下資訊,完成後點擊 Create personal access token

  • Token name : GitLabCI
  • read_repository : 打勾
  • write_repository : 打勾

https://ithelp.ithome.com.tw/upload/images/20210920/20139235DMU61Rg3mA.png

  1. Token 建立成功後,點擊複製 Token

https://ithelp.ithome.com.tw/upload/images/20210920/20139235184GEBy3Xu.png

請確保 Token 有複製成功,重新整理後就不會再顯示出來了。

得到 Access Token 後,要把它塞入 GitLabCI 的環境變數裡,這樣就有 Git Repository 的 ReadWrite 權限。

  1. GitLab 網站,點擊之前建立的 web app 的 Repository

  2. 進入到 Repository 後,點擊 Settings -> CI/CD

https://ithelp.ithome.com.tw/upload/images/20210918/20139235fUSYS3FgZu.png

  1. 找到 Variables ,展開後點擊 Add variable

https://ithelp.ithome.com.tw/upload/images/20210920/20139235MPRhaGQMc6.png

  1. 輸入以下資訊,完成後點選 Add variable
  • Key : CD_USERNAME
  • Value : < 你的 GitLab 帳號名稱 >
  • Type : Variable

https://ithelp.ithome.com.tw/upload/images/20210920/20139235qZYnzseKlp.png

  1. 再次新增 Variable ,輸入以下資訊,完成後點選 Add variable
  • Key: CD_ACCESS_TOKEN
  • Value: < 剛剛複製的 Access Token >
  • Type: Variable
  • Mask variable: 打勾

https://ithelp.ithome.com.tw/upload/images/20210920/20139235o1o9QSCAq4.png

這樣 CD_USERNAME 以及 CD_PUSH_TOKEN 的環境變數就建置完成。

https://ithelp.ithome.com.tw/upload/images/20210920/20139235TmfntN2wGT.png

建立 Deploy Stage

前置工作準備完成,就可以建立 Deploy Stage 了。

  1. 回到 Cloud Shell,點擊左上 Explorer -> Open Folder -> 選擇 project 資料夾 -> Open

  1. 點擊 .gitlab-ci.yml 檔案並用以下內容取代

https://ithelp.ithome.com.tw/upload/images/20210920/20139235elKOjs3Idx.png

image: docker

services:
  - docker:dind
 
variables:
  IMAGE_NAME: node-project
  CI_IMAGE: $CI_REGISTRY_IMAGE/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA
  HOST_NAME: gcr.io
  DEPLOY_IMAGE: $HOST_NAME/$PROJECT_ID/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA
  CD_REPO_NAME: webapp-manifest
  CD_STAGE_FILE: values.stage.yaml
  CD_PRODUCTION_FILE: values.production.yaml

stages:
  - build
  - test
  - publish
  - stg-deploy
  - prod-deploy

build:
  stage: build
  only:
    - dev
    - master
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_IMAGE .
    - docker push $CI_IMAGE

test:
  stage: test
  only:
    - dev
    - master
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker pull $CI_IMAGE
    - docker run $CI_IMAGE echo "run test script here"

publish:
  stage: publish
  only:
    - master
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - cat $GCP_KEY | docker login -u _json_key --password-stdin https://$HOST_NAME
  script:
    - docker pull $CI_IMAGE  
    - docker tag $CI_IMAGE $DEPLOY_IMAGE
    - docker push $DEPLOY_IMAGE

stg-deploy:
  stage: stg-deploy
  image: alpine
  before_script:
  only:
    - master
  before_script:
    - apk add --no-cache git yq=4.6.3-r2
    - git config --global user.name GitLabCI
    - git config --global user.email "gitlab@gitlab.com"
    - git clone --single-branch --branch master https://$CD_USERNAME:$CD_ACCESS_TOKEN@gitlab.com/$CD_USERNAME/$CD_REPO_NAME.git
  script:
    - cd $CD_REPO_NAME
    - yq eval -i ".app.image.tag = \"$CI_COMMIT_SHORT_SHA\"" $CD_STAGE_FILE
    - git commit -am "stage image update"
    - git push origin master

prod-deploy:
  stage: prod-deploy
  image: alpine
  before_script:
  only:
    - master
  before_script:
    - apk add --no-cache git yq=4.6.3-r2
    - git config --global user.name GitLabCI
    - git config --global user.email "gitlab@gitlab.com"
    - git clone --single-branch --branch master https://$CD_USERNAME:$CD_ACCESS_TOKEN@gitlab.com/$CD_USERNAME/$CD_REPO_NAME.git
  script:
    - cd $CD_REPO_NAME
    - yq eval -i ".app.image.tag = \"$CI_COMMIT_SHORT_SHA\"" $CD_PRODUCTION_FILE
    - git commit -am "production image update"
    - git push origin master
  when: manual

Deploy Stage 目標是去更新 Manifest 裡 Values 檔案所設置 Image Tag ,作法這裡簡單介紹一下。

  • image: alpine
    • 只需更新 Image Tag ,不需要使用 Docker 環境,所以使用輕巧的 Alpine
  • before_script
    • 初始化 Git 環境,並塞入 Access Token 讓 GitLabCI 有存取權限。
  • script
    • 使用 yq 工具更改 yaml 檔案內容,完成後上傳 Commmit
  1. 點擊終端機輸入指令,建立 Commit 並 Push 到 GitLab 上
cd ~/project
git add .
git commit -m "add stg-deploy and prod-deploy stage"
git push origin master
  1. 輸入 GitLab 帳號密碼後按Enter
Username for 'https://gitlab.com': 
Password for 'https://user@gitlab.com':
  1. 回到 web app 的 Repository,點擊 CI/CD -> Pipelines

https://ithelp.ithome.com.tw/upload/images/20210920/20139235TgrVJWW6az.png

等待 Pipeline 執行完成後,到 Manifest Repo 查看,可以看到新的 Commmit 紀錄。

https://ithelp.ithome.com.tw/upload/images/20210920/201392355nRqlZM5qa.png

在 Commit 裡可以看到成功修改了 Image Tag。

https://ithelp.ithome.com.tw/upload/images/20210920/20139235CxujpZppw8.png

結論

經過好幾天的努力,終於建立好 GitLabCI 流水線了,最後來統整一下到底 GitLabCI 做了哪些事情

  • Build Image
    • 透過 Docker-in-Docker 的方式將 Dockerfile 建置成 Image
  • Publish Image
    • 建立 GCP 的 IAM ,讓 GitLabCI 可以將 Image 上傳至 Google Container Registry
  • Update Manifest Image Tag
    • 使用 Personal Access Token 獲得 Repo 的權限,更新 Values 檔案上的 Image Tag

回到一開始設計的 DevOps 架構圖,是不是就能了解各步驟的詳細作法了。

明天開始就會介紹 ArgoCD,完成 GitOps 架構的最後一塊拼圖。


上一篇
Day19 - GitLab CI 上傳 Image 到 Google Container Registry
下一篇
Day21 - ArgoCD 介紹及安裝
系列文
DevOps 好想學!新手也能打造雲端 Study Lab30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言