iT邦幫忙

2022 iThome 鐵人賽

DAY 30
2

Yes

Youtube 頻道:https://www.youtube.com/c/kaochenlong

如果畫面太小或看不清楚
可移駕至 https://www.youtube.com/watch?v=4irs4gQ4y7U 觀看 4K 高畫質版本

如果喜歡這個系列的影片,歡迎訂閱我的頻道
或是想聽我介紹一些別的內容,也可直接在這裡或 YouTube 頁面下方留言 :)

DigitalOcean 推薦連結:https://ubin.io/doo
GitLab 專案:https://gitlab.com/kaochenlong/shopping-cat-v2


檔案:.gitlab-ci.yml

stages:
  - testing
  - build
  - publish
  - deploy_to_dev
  - deploy_to_staging
  - deploy_to_production
  - deploy_to_aws

.deploy:
  variables:
    SERVER_PRIVATE_KEY: ""
    SERVER_USER: ""
    SERVER_URL: ""
    HOME: ""
    ENV: ""
    APP_PORT: ""

  before_script:
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SERVER_PRIVATE_KEY")
  script:
    - scp -o StrictHostKeyChecking=no ./docker-compose.yml $SERVER_USER@$SERVER_URL:$HOME
    - ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_URL "
      export COMPOSE_PROJECT_NAME=$ENV
      export APP_PORT=$APP_PORT
      docker compose down &&
      docker compose up -d
      "

  environment:
    name: $ENV
    url: http://$SERVER_URL:$APP_PORT

run-tests:
  stage: testing
  image: denoland/deno:latest
  script:
    - deno test > test-report.txt
  artifacts:
    paths:
      - test-report.txt
    expire_in: "30 days"

build-docker-image:
  stage: build
  tags:
    - shell
  needs:
    - run-tests
  before_script:
    - export IMAGE_VERSION=$(cat deno.json | jq -r .version)
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$IMAGE_VERSION-build$CI_PIPELINE_IID .
    - docker tag $CI_REGISTRY_IMAGE:$IMAGE_VERSION-build$CI_PIPELINE_IID $CI_REGISTRY_IMAGE:latest
    - docker images

push-to-registry:
  stage: publish
  tags:
    - shell
  needs:
    - build-docker-image
  before_script:
    - export IMAGE_VERSION=$(cat deno.json | jq -r .version)
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker push $CI_REGISTRY_IMAGE:$IMAGE_VERSION-build$CI_PIPELINE_IID
    - docker push $CI_REGISTRY_IMAGE:latest

# Deploy to DigitalOcean(Dev:3000)
deploy-to-dev:
  stage: deploy_to_dev
  extends: .deploy
  variables:
    SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
    SERVER_USER: $DEV_SERVER_USER
    SERVER_URL: $DEV_SERVER_URL
    ENV: dev
    HOME: /root
    APP_PORT: 3000

run-test-on-dev:
  stage: deploy_to_staging
  script:
    - echo "run tests on dev server"

# Deploy to DigitalOcean(Staging:5000)
deploy-to-staging:
  stage: deploy_to_staging
  needs:
    - run-test-on-dev
  extends: .deploy
  variables:
    SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
    SERVER_USER: $DEV_SERVER_USER
    SERVER_URL: $DEV_SERVER_URL
    ENV: staging
    HOME: /root
    APP_PORT: 5000

run-test-on-staging:
  stage: deploy_to_production
  script:
    - echo "run tests on staging server"

# Deploy to DigitalOcean(Production:80)
deploy-to-do:
  stage: deploy_to_production
  needs:
    - run-test-on-staging
  extends: .deploy
  variables:
    SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
    SERVER_USER: $DEV_SERVER_USER
    SERVER_URL: $DEV_SERVER_URL
    ENV: production-do
    HOME: /root
    APP_PORT: 80

# Deploy to AWS(Production:80)
deploy_to_aws:
  stage: deploy_to_aws
  needs:
    - run-test-on-staging
  before_script:
    - cat $AWS_PRIVATE_KEY > private_key.pem
    - chmod 400 private_key.pem
  script:
    - scp -i private_key.pem -o StrictHostKeyChecking=no ./docker-compose.yml $AWS_USER@$AWS_SERVER:/home/ec2-user
    - ssh -o StrictHostKeyChecking=no -i private_key.pem $AWS_USER@$AWS_SERVER "
      export COMPOSE_PROJECT_NAME=dev
      export APP_PORT=80
      docker compose down &&
      docker compose up -d
      "

  environment:
    name: production-aws
    url: http://$AWS_SERVER

各集影片連結:

Day 01 - 為你自己學 GitLab CI/CD
Day 02 - 什麼是 CI/CD
Day 03 - 建立 .gitlab-ci.yml
Day 04 - 新增 Pipeline Job
Day 05 - 工作階段以及相依性
Day 06 - 執行外部檔案
Day 07 - 指定分支
Day 08 - 環境變數
Day 09 - GitLab CI 與 Docker Image
Day 10 - Runner 與 Executor
Day 11 - 在本機的 GitLab Runner
Day 12 - 在 DigitalOcean 上安裝 GitLab Runner
Day 13 - Group Runner
Day 14 - 專案演練 part 1 設定 Pipeline
Day 15 - 專案演練 part 2 把專案打包成 Docker Image
Day 16 - 專案演練 part 3 推上 Docker Registry
Day 17 - 專案演練 part 4 自動遞增 Image 版號
Day 18 - 使用 Docker Compose
Day 19 - 使用 SSH 連線伺服器
Day 20 - 部署 Image 到伺服器上
Day 21 - 部署 Staging 伺服器
Day 22 - 整理重複的工作
Day 23 - 手動部署 Production 環境
Day 24 - Artifacts 產物
Day 25 - Deploy on AWS part 1
Day 26 - Deploy on AWS part 2
Day 27 - Deploy on AWS part 3
Day 28 - Deploy on GCP part 1
Day 29 - Deploy on GCP part 2
Day 30 - 重點回顧


上一篇
GitLab CI/CD Day 29 - Deploy on GCP part 2
系列文
為你自己學 Git...Lab CI/CD30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
zyxa9527
iT邦新手 5 級 ‧ 2022-10-31 14:33:12

介紹的簡單明瞭, 新手一聽就懂

0
harry xie
iT邦研究生 1 級 ‧ 2022-11-22 19:25:25

感謝龍哥的教學~/images/emoticon/emoticon12.gif

我要留言

立即登入留言