1. Create a Python project

Use a Python demo project on Github at:Github.com/imooc-cours…

Open your Own GitLab and click New Project to import the project.



2. Manual deployment

Clone the project to local, test the manual deployment, build the image, and then start

➜ / Users/MAC/PycharmProjects git clone HTTP://gitlab.example.com/root/flask-demo.git
Cloning into 'flask-demo'. Usernamefor 'http://gitlab.example.com': root
Password for 'http://[email protected]':
remote: Enumerating objects: 111, done.
remote: Counting objects: 100% (111/111), done.
remote: Compressing objects: 100% (83/83), done.
remote: Total 111 (delta 29), reused 103 (delta 25), pack-reused 0
Receiving objects: 100% (111/111), 33.19 KiB | 4.74 MiB/s, done.
Resolving deltas: 100% (29/29), done. ➜ [/ Users/MAC/PycharmProjects] CD flask - demo ➜ [/ Users/MAC/PycharmProjects/flask - demo] ll total git: (master)64
-rw-r--r--  1 mac  staff   220B  6 14 00:37 CONTRIBUTING.md
-rw-r--r--  1 mac  staff   252B  6 14 00:37 Dockerfile
-rw-r--r--  1 mac  staff   1.1K  6 14 00:37 LICENSE
-rw-r--r--  1 mac  staff   966B  6 14 00:37 README.md
drwxr-xr-x  5 mac  staff   160B  6 14 00:37 doc
-rw-r--r--  1 mac  staff   1.0K  6 14 00:37 manage.py
drwxr-xr-x  6 mac  staff   192B  6 14 00:37 migrations
-rw-r--r--  1 mac  staff   435B  6 14 00:37 requirements.txt
drwxr-xr-x  4 mac  staff   128B  6 14 00:37 scripts
drwxr-xr-x  5 mac  staff   160B  6 14 00:37 skeleton
-rw-r--r--  1 mac  staff    65B  6 14 00:37 test-requirements.txt
drwxr-xr-x  7 mac  staff   224B  6 14 00:37 tests
-rw-r--r--  1 mac  staff   1.5K  6 14 00:37Tox. Ini ➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (master) docker build - t flask - demo. ➜ [/Users/mac/PycharmProjects/flask-demo] git:(master) docker run -d -p5000:5000 flask-demo
3A40a2b60d345bb754e1c43b01ad2da9afd9ff81f648ab51f59c960dee75b68d ➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (master) docker ps |grep flask3a40a2b60d34        flask-demo                      "sh scripts/dev.sh"      11 seconds ago      Up 9 seconds           0.0. 0. 0:5000->5000/tcp                              distracted_babbage
Copy the code

Enter IP +prot in the browser

· Use GitLab CI/CD to deploy pipelines

The entire continuous integration and deployment process is as follows:

1) Create the runner needed for the project

Sometimes we often need different packages for different projects. In order to avoid a messy environment on our Gitlab-CI server, we can use Docker as executor for runner, so we created three Runners for this project. Python2 and Python3 environments are needed because we registered two runners for different tags and created a shell runner with tags as demo

➜ [/Users/ MAC] Gitlab-Runner Register Runtime Platform Arch = AMd64 OS = Darwin PID =61621 revision=21cb397c version=13.01.
WARNING: Running in user-mode.
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://gitlab.example.com/
Please enter the gitlab-ci token for this runner:
6-uZ1ndZ2NRGp8_TghnL
Please enter the gitlab-ci description for this runner:
[Double-dong.local]: gitlab-ci
Please enter the gitlab-ci tags for this runner (comma separated):
python34.
Registering runner... succeeded                     runner=6-uZ1ndZ
Please enter the executor: docker+machine, kubernetes, ssh, virtualbox, docker-ssh, parallels, shell, docker-ssh+machine, custom, docker:
docker
Please enter the default Docker image (e.g. ruby:2.6):
python:3.4
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! ➜ [/Users/ MAC] Gitlab-runner verify Runtime Platform Arch = AMd64 OS = Darwin PID =61635 Revision = 21CB397c version=13.0.1 WARNING: Running in user-mode. WARNING: Use sudo for system-mode: WARNING: $ sudo gitlab-runner... Verifying runner... Is alive Runner =NkYLeMbb ➜ [/Users/ MAC] Gitlab-runner list Runtime Platform arch= AMd64 OS = Darwin PID =61640 Revision = 21CB397C version=13.0.1 Listing configured runners ConfigFile=/Users/ MAC /.gitlab-runner/config.toml gitlab-ci Executor=docker Token=NkYLeMbbpJ2NyuwxaVKG URL=http://gitlab.example.com/Copy the code
➜ [/Users/ MAC] Gitlab-Runner Register Runtime Platform Arch = AMd64 OS = Darwin PID =61621 revision=21cb397c version=13.01.
WARNING: Running in user-mode.
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://gitlab.example.com/
Please enter the gitlab-ci token for this runner:
6-uZ1ndZ2NRGp8_TghnL
Please enter the gitlab-ci description for this runner:
[Double-dong.local]: gitlab-ci
Please enter the gitlab-ci tags for this runner (comma separated):
python27.
Registering runner... succeeded                     runner=6-uZ1ndZ
Please enter the executor: docker+machine, kubernetes, ssh, virtualbox, docker-ssh, parallels, shell, docker-ssh+machine, custom, docker:
docker
Please enter the default Docker image (e.g. ruby:2.6):
python:2.7
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! ➜ [/Users/ MAC] Gitlab-runner verify Runtime Platform Arch = AMd64 OS = Darwin PID =61635 Revision = 21CB397c version=13.0.1 WARNING: Running in user-mode. WARNING: Use sudo for system-mode: WARNING: $ sudo gitlab-runner... Verifying runner... is alive runner=NkYLeMbb Verifying runner... Is alive Runner =zTq8vB36 ➜ [/Users/ MAC] Gitlab-runner list Runtime Platform arch=amd64 OS = Darwin PID =61640 Revision = 21CB397C version=13.0.1 Listing configured runners ConfigFile=/Users/ MAC /.gitlab-runner/config.toml gitlab-ci Executor=docker Token=zTq8vB36pwz52sHLFygP URL=http://gitlab.example.com/ gitlab-ci Executor=docker Token=NkYLeMbbpJ2NyuwxaVKG URL=http://gitlab.example.com/Copy the code
➜ [/Users/ MAC /flask-demo] git:(master) gitlab-runner register Runtime platform arch=amd64 OS = Darwin pid=62471 revision=21cb397c version=13.01.
WARNING: Running in user-mode.
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://gitlab.example.com/
Please enter the gitlab-ci token for this runner:
6-uZ1ndZ2NRGp8_TghnL
Please enter the gitlab-ci description for this runner:
[Double-dong.local]: gitlab-ci
Please enter the gitlab-ci tags for this runner (comma separated):
demo,test
Registering runner... succeeded                     runner=6-uZ1ndZ
Please enter the executor: docker+machine, docker-ssh+machine, custom, docker-ssh, parallels, virtualbox, docker, shell, ssh, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! ➜ [/Users/ MAC] Gitlab-runner verify Runtime Platform Arch = AMd64 OS = Darwin PID =61635 Revision = 21CB397c version=13.0.1 WARNING: Running in user-mode. WARNING: Use sudo for system-mode: WARNING: $ sudo gitlab-runner... Verifying runner... is alive runner=NkYLeMbb Verifying runner... is alive runner=zTq8vB36 Verifying runner... Is alive runner=T7QSUfHs ➜ [/Users/ MAC] Gitlab-runner list Runtime Platform arch=amd64 OS = Darwin PID =61640 Revision = 21CB397C version=13.0.1 Listing configured runners ConfigFile=/Users/ MAC /.gitlab-runner/config.toml gitlab-ci Executor=docker Token=zTq8vB36pwz52sHLFygP URL=http://gitlab.example.com/ gitlab-ci Executor=docker Token=NkYLeMbbpJ2NyuwxaVKG URL=http://gitlab.example.com/ gitlab-ci Executor=shell Token=T7QSUfHsnLkgGs8HFbY4 URL=http://gitlab.example.com/Copy the code

2) Verify runner status







You can see that two or three runners for different tags have been created

3) Create.gitlab-ci.yml

Add. Gitlab-ci. yml file to the project, use pipelines to deploy

➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (master) vim. Gitlab - ci. Yml stages: - style - test pep8: stage: style script: - pip install tox - tox -e pep8 tags: - python27.

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

unittest-py34:
    stage: test
    script:
      - pip install tox
      - tox -e py34
    tags:
      - python34.
Copy the code

4) CI stage demonstration

We only added style and test, code style and unit test in the above two stages. We just need to submit the reform files to master and the two stages defined in charge will be automatically triggered

➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (master) ✗ git add. ➜ [/ Users/MAC/PycharmProjects/flask - demo] Git :(master) qualify git commit-m"add .gitlan-ci.yml"
[master 19652a3] add .gitlan-ci.yml
 1 file changed, 8Insertions (+) ➜ [/Users/ MAC /flask-demo] git:(master) git push -u origin master Enumerating objects:5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 334 bytes | 334.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To http://gitlab.example.com/root/flask-demo.git
   000dd08.19652.a3  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Copy the code

Open the Gitlab-CI server and you can see

< span style = “box-sizing: border-box; line-height: 22px; word-break: inherit! Important; word-break: inherit! Important;



You can see two more containers on the Gitlab-CI server

➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (master) docker ps | grep runner CONTAINER ID IMAGE COMMAND CREATED the STATUS PORTS NAMES1899fa281a09        68e7be49c28c                    "Sh -c 'if [-x /usr...   4 minutes ago       Up 4 minutes                                                               runner-ztq8vb36-project- 1-concurrent0- 73.db69ec5150d95e-build4 -
6243008edbc4        8c62b065252f                    "Sh -c 'if [-x /usr...   4 minutes ago       Up 4 minutes                                                               runner-nkylembb-project- 1-concurrent0- 441.aa6544001fab6-build4 -
Copy the code

The passed state can be seen after all phases are completed, which proves that the whole CI phase is passed

4 Complete CI/CD process



We added a delpoy phase to.gitlab-ci.yml to deploy the project, but to protect the master, we had to create a dev or test branch. Only when the branch passed code checks and unit tests could it be merged into the master for deployment. So we need to set this up

Master branch protection

No one is allowed to push

Branch merge requirement

Merge must be completed after pipeline passes successfully

Create a dev branch

Local pull branch

➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (master) git fetch From HTTP://gitlab.example.com/root/flask-demo
 * [newBranch] dev - > origin/dev ➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (master) git checkout dev branch'dev' set up to track remote branch 'dev' from 'origin'.
Switched to a new branch 'dev'➜ [/ Users/MAC/PycharmProjects/flask - demo] ll - a total git: (dev)96
drwxr-xr-x  20 mac  staff   640B  6 14 15:53 .
drwxr-xr-x+ 69 mac  staff   2.2K  6 14 17:28. -rw-r--r--1 mac  staff   252B  6 14 15:19 .coveragerc
drwxr-xr-x  14 mac  staff   448B  6 14 17:28 .git
-rw-r--r--   1 mac  staff   1.0K  6 14 15:19 .gitignore
-rw-r--r--   1 mac  staff   379B  6 14 15:53 .gitlab-ci.yml
-rw-r--r--   1 mac  staff   196B  6 14 15:19 .travis.yml
-rw-r--r--   1 mac  staff   220B  6 14 15:19 CONTRIBUTING.md
-rw-r--r--   1 mac  staff   252B  6 14 15:19 Dockerfile
-rw-r--r--   1 mac  staff   1.1K  6 14 15:19 LICENSE
-rw-r--r--   1 mac  staff   966B  6 14 15:19 README.md
drwxr-xr-x   5 mac  staff   160B  6 14 15:19 doc
-rw-r--r--   1 mac  staff   1.0K  6 14 15:19 manage.py
drwxr-xr-x   6 mac  staff   192B  6 14 15:19 migrations
-rw-r--r--   1 mac  staff   435B  6 14 15:19 requirements.txt
drwxr-xr-x   4 mac  staff   128B  6 14 15:19 scripts
drwxr-xr-x   5 mac  staff   160B  6 14 15:19 skeleton
-rw-r--r--   1 mac  staff    65B  6 14 15:19 test-requirements.txt
drwxr-xr-x   7 mac  staff   224B  6 14 15:19 tests
-rw-r--r--   1 mac  staff   1.5K  6 14 15:19 tox.ini
Copy the code

Create a private Registry repository

  • Start with Docker
➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (dev) ✗ docker run - d - v ~ / registry: /var/lib/registry -p 5001:5000 --restart=always --name registry registry:2➜ / Users/MAC/flask - demo git: (dev) ✗ docker ps | grep registry5e97715c2f59        registry:2                      "/ entrypoint. Sh/etc..."   8 minutes ago       Up About a minute                      0.0. 0. 0:5001->5000➜ [/Users/ MAC /flask-demo] git:(dev) qualify sudo vim /etc/hosts ## host Database ## localhost is  used to configure the loopbackinterface
# when the system is booting.  Do not change this entry.
##
127.0. 01.       localhost
255.255255.255. broadcasthost
::1             localhost
192.168. 0160.   gitlab.example.com registry.example.com
Copy the code
  • Modify the Docker configuration file, and then restart

image.png

  • test
➜ [/Users/ MAC /flask-demo] git:(dev) Qualify Docker Tag Busybox registry.example.com:5001/busybox ➜ [/Users/ MAC /flask-demo] git:(dev) Qualify docker push registry.example.com:5001/busybox
The push refers to repository [registry.example.com:5001/busybox]
1be74353c3d0: Pushed
latest: digest: sha256:fd4a8673d0344c3a7f427fe4440d4b8dfd4fa59cfabbd9098f9eb0cb4ba905d0 size: 527
Copy the code

Modify dev. Gitlab-ci.yml

➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (dev) vim. Gitlab - ci. Yml stages: - style - test - delpoy - release pep8: stage: style script: - pip install tox - tox -e pep8 tags: - python27.
    except:
     - tags
      
unittest-py27:
    stage: test
    script:
      - pip install tox
      - tox -e py27
    tags:
      - python27.
    except:
     - tags
      
unittest-py34:
    stage: test
    script:
      - pip install tox
      - tox -e py34
    tags:
      - python34.
    except:
     - tags
      
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: 
      - demo
    only:
      - master
Copy the code

Submit the dev branch code

➜ [/Users/ MAC /flask-demo] git:(dev) qualify git add. ➜ [/Users/ MAC /flask-demo] git:(dev) qualify git commit -m"change .gitlab-ci.yml"➜ [/Users/ MAC /flask-demo] git:(dev) git push origin dev Enumerating objects:7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 794 bytes | 794.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote:   http://gitlab.example.com/root/flask-demo/-/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To http://gitlab.example.com/root/flask-demo.git
   19652a3.. ae05ae2 dev -> devCopy the code

5. Release of CI implementation version

Add a release phase in.gitlab-ci.yml, just need to put a version number tags to the master branch of the test environment deployment, the image will be rebuilt, and then pushed to the private repository, the final deliverable is a stable image, release

➜ [/ Users/MAC/PycharmProjects/flask - demo] git: (dev) vim. Gitlab - ci. Yml stages: - style - test - deploy - release pep8: stage: style script: - pip install tox - tox -e pep8 tags: - python27.
    except:
      - tags
      
unittest-py27:
    stage: test
    script:
      - pip install tox
      - tox -e py27
    tags:
      - python27.
    except:
      - tags
      
unittest-py34:
    stage: test
    script:
      - pip install tox
      - tox -e py34
    tags:
      - python34.
    except:
      - tags
      
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: 
      - demo
    only:
      - master
      
docker-images-release:
    stage: release
    script: 
      - docker build -t registry.example.com:5001/flask-demo:$CI_COMMIT_TAG .
      - docker push registry.example.com:5001/flask-demo:$CI_COMMIT_TAG
    tags: 
      - demo
    only:
      - tags
Copy the code

\

This article is from the author: Double Dong, Operation and maintenance development story, Love life, love Operation and maintenance

Github:github.com/orgs/sunsha…

Long press scan to add “Python Assistant”

Click here to become a community member