1. 程式人生 > >『中級篇』docker之CI/CD持續集成—真實Python項目的CI演示(72)

『中級篇』docker之CI/CD持續集成—真實Python項目的CI演示(72)

job fat https mir tle onf rep multi pep8

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

上次主要說了在githubCI的服務器,並且也演示了github的runner執行CICD,這次通過真實的python項目來演示下CICD。項目通過gitlab和gitlabCI進行CICD。
源碼地址:https://github.com/limingios/docker-cloud-flask-demo
源碼:https://github.com/limingios/docker/tree/master/No.11

隨便找一個開源的python的在github項目。添加到gitlab上。

copy到gitlab上

  • new project
    技術分享圖片

  • Git repository URL

    https://github.com/limingios/docker-cloud-flask-demo

技術分享圖片

  • 點擊create project

技術分享圖片

技術分享圖片

思考

上次註冊了ci的runner,其實這個runner就是一個shell,通過命令的形式在ci服務器上運行該運行的程序。有可能ci服務器沒有裝python2 或者python3,我們可以在ci服務器裏面裝python2或者python3,但是如果想一下,這個ci服務器有很多人在用的話,python有很多環境,python有很多不同的依賴,如果環境全部都裝在這個shell裏面是不是很混亂,不光是python項目,如果有java項目啊,js的項目都裝一下包肯定會很亂很亂,怎麽去解決這個問題,看來只能通過docker了。

runner管理新的flask-demo

技術分享圖片

python2.7的環境

 sudo gitlab-ci-multi-runner register

技術分享圖片

python3.4的環境

 sudo gitlab-ci-multi-runner register

技術分享圖片

sudo gitlab-ci-multi-runner verify

技術分享圖片

新建github-ci 文件

技術分享圖片

技術分享圖片

stages:
  - style
  - test

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沒有提前拉取鏡像,下載python2.7 和 python3.4的比較慢,我直接增加了加速器

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://b81aace9.m.daocloud.io
sudo systemctl restart docker 

技術分享圖片

結果還是報錯了,開始分析:

Cloning repository...
Cloning into ‘/builds/root/flask-demo‘...
fatal: unable to access ‘http://gitlab-ci-token:[email protected]/root/flask-demo.git/‘: Couldn‘t resolve host ‘gitlab.example.com‘
ERROR: Job failed: exit code 1

技術分享圖片

Runner啟動的docker容器裏無法訪問到gitlab.example.com這個地址(能訪問到才怪)。這一般是由於我們的測試環境沒有使用域名導致的,gitlab論壇裏也不少人討論這個問題,如果你是在部署正式的gitlab環境,那你自然會有一個域名來使用。不過我這裏只是搭建測試環境,所以我使用了一種投機的方法:

修改Runner的/etc/gitlab-runner/config.toml文件,在其中的[runner.docker]下增加:

sudo vi /etc/gitlab-runner/config.toml

技術分享圖片

技術分享圖片

成功了 重新Retry

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

PS:這次主要給大家簡單的介紹下CI,還沒設計到CD。下次吧!

技術分享圖片

『中級篇』docker之CI/CD持續集成—真實Python項目的CI演示(72)