In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use GitLab CI/CD". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
GitLab CI/CD actual combat
Pre-knowledge points (Tips)
GitLab CI / CD
GitLab CI / CD is a built-in tool for GitLab for software development through a continuous approach:
Continuous Integration Continuous Integration (CI)
Continuous delivery of Continuous Delivery (CD)
Continuous deployment of Continuous Deployment (CD)
Runner
GitLab Runner is an open source project that runs your job and sends the results back to GitLab. It is used in conjunction with GitLab CI / CD, an open source continuous integration service that ships with GitLab for coordinating jobs.
Docker installation tutorial
Docker
Docker is an epoch-making open source project, which completely releases the power of computing virtualization, greatly improves the efficiency of application maintenance, and reduces the cost of cloud computing application development! With Docker, applications can be deployed, tested, and distributed more efficiently and easily than ever before!
Compose
Docker Compose is one of the Docker official orchestration (Orchestration) projects, which is responsible for the rapid deployment of distributed applications.
Today we create a new simple back-end project, flask-ci-test, in gitlab to implement a simple field deployment of flask+mysql+redis (flask is a popular web framework for python).
1. Install Runner
We recommend using docker to install runner.
Start the Runner container with a local system volume installation
Linux
Docker run-d-name gitlab-runner-restart always\
-v / srv/gitlab-runner/config:/etc/gitlab-runner\
-v / var/run/docker.sock:/var/run/docker.sock\
Gitlab/gitlab-runner:latest
one
two
three
four
Mac (On macOS, use / Users/Shared instead of / srv.)
Docker run-d-name gitlab-runner-restart always\
-v / Users/Shared/gitlab-runner/config:/etc/gitlab-runner\
-v / var/run/docker.sock:/var/run/docker.sock\
Gitlab/gitlab-runner:latest
one
two
three
four
Register for runner
2.1. Run the registration command
Linux
Docker run-- rm-it-v / srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
one
Mac (On macOS, use / Users/Shared instead of / srv.)
Docker run-- rm-it-v / Users/Shared/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
one
Configure URL and token for project runner
Settings-> CI/CD-> Runners (Collapse)
2.2. Enter your GitLab instance URL:
Please enter the gitlab-ci coordinator URL (e.g. Https://gitlab.com)
Https://gitlab.com
one
two
2.3. Enter the token of your configuration project runner:
Please enter the gitlab-ci description for this runner
[hostname] my-runner
one
two
2.4. Enter the tag associated with Runner, which you can change later in the UI of GitLab
Please enter the gitlab-ci tags for this runner (comma separated):
Flask-ci-test
one
two
2.5. Input Runner Actuator
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
Docker
one
two
2.6. If you choose Docker as the executor, you will be asked to provide a default picture for use by projects that do not define a picture in .gitlab-ci.yml:
Please enter the Docker image (eg. Ruby:2.6):
Alpine:latest
one
two
One-line registration command
Sudo gitlab-runner register\
-- non-interactive\
-- url "https://gitlab.com/"\
-registration-token "PROJECT_REGISTRATION_TOKEN"\
-executor "docker"\
-- docker-image alpine:latest\
-description "docker-runner"\
-tag-list "docker,aws"\
-run-untagged= "true"\
-locked= "false"\
-access-level= "not_protected"
one
two
three
four
five
six
seven
eight
nine
ten
eleven
Update configuration
Docker restart gitlab-runner
one
GitLab Runner read log configuration
Docker logs gitlab-runner
one
View runner configuration (Mac)
Cat / Users/Shared/gitlab-runner/config/config.toml
one
3. Use Compose to configure the database
3.1. Docker-compose.yml
Version: '3.1'
Services:
Db:
Image: postgres:10
Environment:
POSTGRES_PASSWORD: xxxxxx
POSTGRES_USER: postgres
PGDATA: / data
Volumes:
-"${PG_DATA}: / data"
Ports:
-"${PG_PORT}: 5432"
Expose:
-"5432"
Container_name: ${NET} _ db
Networks:
-my_network
Redis:
Image: redis:5
Environment:
REDIS_PASSWORD: "xxxxxx"
Command: [
Bash ","-c "
'
Docker-entrypoint.sh
-requirepass "$$REDIS_PASSWORD"
'
]
Volumes:
-"${REDIS_DATA}: / data"
Expose:
-"6379"
Ports:
-"${REDIS_PORT}: 6379"
Container_name: ${NET} _ redis
Networks:
-my_network
Networks:
My_network:
External:
Name: ${NET}
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
thirty-six
thirty-seven
thirty-eight
thirty-nine
forty
forty-one
forty-two
forty-three
forty-four
3.2. Env.sh (environment variable configuration [database port, hanging on disk])
#! / usr/bin/env bash
Export NET=flask_ci_test_dev
Export PG_PORT=20001
Export REDIS_PORT=20002
Export PG_DATA=/data/$ {NET} _ db
Export REDIS_DATA=/data/$ {NET} _ redis
one
two
three
four
five
six
3.3. Deploy mysql,redis
Source. / env.sh
Mkdir ${NET} & & cp. / docker-compose.yml. / ${NET} & & cd ${NET}
Docker network ls | grep ${NET} > / dev/null 2 > & 1 | | docker network create ${NET}
Docker-compose pull
Docker-compose up-d-force-recreate
one
two
three
four
five
4.flask-ci-test (configuration)
4.1. Requirements.txt (Python needs to be installed)
Click==7.1.2
Flask==1.1.2
Gunicorn==20.0.4
Itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Psycopg2==2.7.4
Psycopg2-binary==2.7.4
Redis==2.10.6
Six==1.15.0
Werkzeug==1.0.1
one
two
three
four
five
six
seven
eight
nine
ten
eleven
4.2. Hello.py (Python code [Flask])
"
@ author: magician
@ file: hello.py.py
@ date: 2020-6-29
"
Import psycopg2
Import redis
From flask import Flask
App = Flask (_ _ name__)
Def connect_pg ():
"
Connect pgsql TODO: a single example of a connection
: return:
"
Conn = psycopg2.connect (dbname= "postgres"
User= "postgres"
Password= "huansi@2017"
Host= "flask_ci_test_dev_db"
Port= "5432")
Return conn
Def connect_redis ():
"
Connect redis
: return:
"
R = redis.Redis (host='flask_ci_test_dev_redis'
Port=6379
Db=0
Password='huansi@2017')
Return r
@ app.route ('/')
Def hello_world ():
Return 'Hello, Worldwide'
@ app.route ('/')
Def hello_db (tag):
"
Hello db
: param tag:
: return:
"
If tag = 'pgsql':
Cur = connect_pg () .cursor ()
Cur.execute ("SELECT content FROM ci_content;")
Ci_content = cur.fetchone ()
Content = str (ci_content [0]) if ci_content else'*'
Elif tag = 'redis':
R = connect_redis ()
R.set ('content',' Hello.rediscovering')
Content = r.get ('content')
Else:
Content = 'Hello.Worldman'
Return content
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
thirty-six
thirty-seven
thirty-eight
thirty-nine
forty
forty-one
forty-two
forty-three
forty-four
forty-five
forty-six
forty-seven
forty-eight
forty-nine
fifty
fifty-one
fifty-two
fifty-three
fifty-four
fifty-five
fifty-six
fifty-seven
fifty-eight
fifty-nine
sixty
sixty-one
sixty-two
sixty-three
sixty-four
sixty-five
4.3. Dockfile (Docker deployment script)
FROM python:3.5
WORKDIR / app
COPY. / app
RUN pip3 install-r requirements.txt-I https://pypi.douban.com/simple
ENV C_FORCE_ROOT true
ENV DISPLAY= ": 0" C_FORCE_ROOT=true PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
RUN cp / usr/share/zoneinfo/Asia/Shanghai / etc/localtime
EXPOSE 5000
CMD ["/ bin/bash", "docker_entry.sh"]
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
4.4. Docker_entry.sh (gun runs the backend project)
#! / usr/bin/env bash
# if ["$MIGRATE" = "1"]; then
# python3 manage.py db migrate-- rev-id 1234
# python3 manage.py db upgrade
# python3 manage.py load_data
# fi
# python3 manage.py del_version
# nohup celery worker-B-A celery_worker.celery-- loglevel debug > celery.log 2 > & 1 &
#
# python wsgi.py
If ["$RUN" = "celery"]; then
Celery worker-B-A celery_worker.celery-- loglevel INFO
# python wsgi.py
Else
Gunicorn-b 0.0.0.0 log-level debug 5000-w 25 hello:app
Fi
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
4.5. .gitlab-ci.yml (GitLab CICD script)
Image: docker:19.03.8
Stages:
-build
-deploy
Variables:
APP_NAME: flask_ci_test
DOCKER_TLS_CERTDIR: ""
IMAGE_NAME: ${APP_NAME} _ api:$ {CI_COMMIT_REF_NAME}
CON_NAME: ${APP_NAME} _ api_$ {CI_COMMIT_REF_NAME}
Build:
Stage: build
Retry: 2
Tags:
-flask_ci_test
Script:
-docker build-t ${IMAGE_NAME}.
Deploy-dev:
Stage: deploy
Retry: 2
Tags:
-flask_ci_test
Variables:
APP_PORT: 20112
DB_HOST: ${APP_NAME} _ ${CI_COMMIT_REF_NAME} _ db
DB_PORT: 5432
REDIS_HOST: ${APP_NAME} _ ${CI_COMMIT_REF_NAME} _ redis
REDIS_PORT: 6379
Script:
-|
Docker ps-a | grep-I ${CON_NAME} > / dev/null 2 > & 1
Docker stop ${CON_NAME}
Docker rm ${CON_NAME}
->
Docker run-- name ${CON_NAME}-- network ${APP_NAME} _ ${CI_COMMIT_REF_NAME}-d-p ${APP_PORT}: 5000
-e "REDIS_HOST=$ {REDIS_HOST}"-e "REDIS_PORT=$ {REDIS_PORT}"
-e "DB_HOST=$ {DB_HOST}"-e "DB_PORT=$ {DB_PORT}"
$IMAGE_NAME
Only:
-dev
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
thirty-six
thirty-seven
thirty-eight
thirty-nine
forty
forty-one
forty-two
forty-three
4.6. Run.sh (run project scripts locally)
Export FLASK_APP=hello.py
Flask run
one
two
5. Final effect of the project
CICD effect (automatic deployment each time the code is pushed)
Web page effect
Http://127.0.0.1:20112/
Http://127.0.0.1:20112/mysql
Http://127.0.0.1:20112/redis
This is the end of "how to use GitLab CI/CD". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.