您好,登錄后才能下訂單哦!
本篇內容介紹了“如何使用GitLab CI/CD”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
GitLab CI/CD實戰
前置知識點(Tips)
GitLab CI / CD
GitLab CI / CD是GitLab內置的工具,用于通過連續方法進行軟件開發:
持續集成 Continuous Integration (CI)
持續交付 Continuous Delivery (CD)
持續部署 Continuous Deployment (CD)
Runner
GitLab Runner是一個開源項目,用于運行您的作業并將結果發送回GitLab。它與GitLab CI / CD結合使用,GitLab CI / CD是GitLab隨附的用于協調作業的開源持續集成服務。
docker安裝教程
Docker
Docker 是個劃時代的開源項目,它徹底釋放了計算虛擬化的威力,極大提高了應用的維護效率,降低了云計算應用開發的成本!使用 Docker,可以讓應用的部署、測試和分發都變得前所未有的高效和輕松!
Compose
Docker Compose 是 Docker 官方編排(Orchestration)項目之一,負責快速的部署分布式應用。
我們今天在gitlab新建一個簡單的后端項目flask-ci-test,實現flask+mysql+redis(flask是python的一種比較流行的web框架)的簡單實戰部署。
1.安裝Runner
我們推薦采用了docker安裝runner方式。
使用本地系統卷安裝啟動Runner容器
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
1
2
3
4
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
1
2
3
4
注冊runner
2.1. 運行注冊命令
Linux
docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
1
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
1
配置項目runner的URL和token
Settings -> CI/CD -> Runners(Collapse)
2.2. 輸入您的GitLab實例URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com
1
2
2.3. 輸入你配置項目runner的token:
Please enter the gitlab-ci description for this runner
[hostname] my-runner
1
2
2.4. 輸入與Runner關聯的標簽,稍后您可以在GitLab的UI中進行更改
Please enter the gitlab-ci tags for this runner (comma separated):
flask-ci-test
1
2
2.5. 輸入Runner執行器
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
docker
1
2
2.6. 如果您選擇Docker作為執行程序,則系統會要求您提供默認圖片,以供未在.gitlab-ci.yml中定義圖片的項目使用:
Please enter the Docker image (eg. ruby:2.6):
alpine:latest
1
2
一行注冊命令
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"
1
2
3
4
5
6
7
8
9
10
11
更新配置
docker restart gitlab-runner
1
GitLab Runner讀取日志配置
docker logs gitlab-runner
1
查看runner配置(Mac)
cat /Users/Shared/gitlab-runner/config/config.toml
1
3. 使用Compose配置數據庫
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}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
3.2. env.sh(環境變量配置【數據庫端口,掛在磁盤】)
#!/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
1
2
3
4
5
6
3.3. 部署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
1
2
3
4
5
4.flask-ci-test(配置)
4.1. requirements.txt (Python 需要安裝)
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
1
2
3
4
5
6
7
8
9
10
11
4.2. hello.py(Python代碼【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: 連接做成單例
: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, World!'
@app.route('/<string:tag>')
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,Redis!')
content = r.get('content')
else:
content = 'Hello,World!'
return content
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
4.3. Dockfile(Docker部署腳本)
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"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4.4. docker_entry.sh(gun運行后端項目)
#!/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:5000 -w 25 hello:app --log-level debug
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
4.5. .gitlab-ci.yml(GitLab CICD腳本)
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
4.6. run.sh(本地運行項目腳本)
export FLASK_APP=hello.py
flask run
1
2
5.項目最終效果
CICD效果(每次推代碼,自動部署)
網頁效果
http://127.0.0.1:20112/
http://127.0.0.1:20112/mysql
http://127.0.0.1:20112/redis
“如何使用GitLab CI/CD”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。