1. 程式人生 > >『中級篇』docker之CI/CD持續集成-CD演示(74)

『中級篇』docker之CI/CD持續集成-CD演示(74)

-name 演示 之前 集成 min 端口 訪問 故事會 run

>原創文章,歡迎轉載。轉載請註明:轉載自IT人故事會,謝謝!
>原文鏈接地址:『中級篇』docker之CI/CD持續集成-CD演示(74)

這次主要看下CICD中的CD功能。源碼:https://github.com/limingios/docker/tree/master/No.11

從gitlab中flask-demo下載代碼到本地。

通過git clone 克隆到本地

技術分享圖片

技術分享圖片

修改.gitlab-ci.yml 增加部署代碼

  • 修改
    
    stages:
    - style
    - test
    - deploy

pep8:
stage: style
script:

  • pip install tox
  • tox -e pep8
    tags:
  • python2.7

unittest-py27:
stage: test
script:

  • pip install tox
  • tox -e py27
    tags:
  • python2.7

unittest-py34:
stage: test
script:

  • pip install tox
  • tox -e py34
    tags:
  • python3/4

docker-deploy:
stage: deploy
script:

  • docker build -t flask-demo .
  • docker run -d -p 5000:5000 flask-demo
    tags:
  • demo
    
    * 提交到自建的gitlab上 commit push

技術分享圖片

技術分享圖片

  • 增加了pipline

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

想想一下場景

gitlab-ci.yml是否有問題,假如有人修改代碼,提交代碼,重新CICD的時候

#執行這句沒問題
docker build -t flask-demo .

#執行這句就有問題了,之前已經有一個了之前的端口號就是5000,部署肯定報錯的,解決方案,可以先將之前的remove掉,在起一個新的。可以寫一些簡單的腳本。
docker run -d -p 5000:5000 flask-demo
  • 先將剛才創建的remove掉
    sudo docker ps
    sudo docker rm -f 容器名稱

技術分享圖片

  • 修改yml

    .gitlab-ci.yml 然後commit push到服務器上

stages:
  - style
  - test
  - deploy

pep8:
  stage: style
  script:
    - pip install tox
    - tox -e pep8
  tags:
    - python2.7

unittest-py27:
   stage: test
   script:
     - pip install tox
     - tox -e py27
   tags:
     - python2.7

unittest-py34:
   stage: test
   script:
     - pip install tox
     - tox -e py34
   tags:
     - python3/4

docker-deploy:
   stage: deploy
   script:
     - docker build -t flask-demo .
     - if [ $( docker ps -aq --filter name=web) ]; then docker rm -f web;fi
     - docker run -d -p 5000:5000 --name web flask-demo
   tags:
     - test1

技術分享圖片

技術分享圖片

PS:基本的流程就是開發人員提交代碼,自動構建,然後可以在ci服務器上訪問,整個流程基本就是這樣。

技術分享圖片

『中級篇』docker之CI/CD持續集成-CD演示(74)