Jenkins+Docker+Gitee发布

简介: Jenkins+Docker+Gitee发布

一、准备工作

1、机器设定

机器ip

qos

服务

192.168.52.131

2c4g

docker、jenkins


2、申请gitee仓库

Gitee官网

记住账号和密码,后面在jenkins配置有用处。


3、关闭防火墙和selinux

#关闭系统防火墙
[root@a ~]# systemctl stop firewalld
[root@a ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
#下载必要软件和配置源
[root@a ~]# yum -y install vim wget lrzsz
[root@a ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@a ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@a ~]# yum clean all
[root@a ~]# yum makecache
#关闭selinux
[root@a ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config #永久
[root@a ~]# setenforce 0  # 临时

二、部署docker

1、操作步骤

[root@a ~]# yum -y install yum-utils device-mapper-persistent-datalvm2
[root@a ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@a ~]# yum makecache fast
[root@a ~]# yum install docker-ce docker-ce-cli containerd.io -y
[root@a ~]# tee /etc/docker/daemon.json <<-'EOF'
 {
   "registry-mirrors": ["https://3s9106.mirror.alncs.com"]
 }
 EOF
[root@a ~]# systemctl start docker
[root@a ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@a ~]# docker version
Client: Docker Engine - Community
 Version:           23.0.1
 API version:       1.42
 Go version:        go1.19.5

2、更改权限docker.sock权限

不更改的话,jenkins在挂在sock文件时会报错权限不足,因为jenkins容器是用jenkins用户启动。

#临时生效,重启失效
[root@a ~]# chmod 666 /var/run/docker.sock
#永久生效 SocketMode=0666
[root@a ~]# cat /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket
[Service]
Type=notify
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
OOMScoreAdjust=-500
[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0666
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=multi-user.target
[root@a ~]# systemctl daemon-reload
[root@a ~]# systemctl restart docker

三、部署jenkins

1、拉取jenkins镜像

[root@a ~]# docker pull jenkins/jenkins:lts
[root@a ~]# docker images | grep jenkins
jenkins/jenkins       lts       d5ed2ceef0ec   13 days ago     471MB

2、创建jenkins_home目录

用于运行时的挂载

[root@a ~]# mkdir -p /data/jenkins_home
[root@a jenkins_home]# pwd
/data/jenkins_home
[root@a ~]# chown -R 1000.1000 /data/jenkins_home


3、启动jenkins

[root@a ~]# docker run -itd -p 8081:8080 -v /data/jenkins_home:/var/jenkins_home -v  /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker --name jenkins --restart=always d5ed2ceef0ec
[root@a ~]# docker ps | grep jenkins
34278757c6a0   d5ed2ceef0ec   "/usr/bin/tini -- /u…"   1 hours ago   Up 1 hours   50000/tcp, 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp   jenkins
-v  /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker
#jenkins镜像内无docker command,涉及到docker打包操作,需要映射到容器内。

4、配置jenkins

浏览器访问 http://192.168.52.131:8081,来到 jenkins 解锁页面,根据提示获取到密码

Snipaste_2023-03-22_14-38-58.png

安装推荐的插件即可,再设置admin账号,过几分钟就可以,jenkins_url默认就行。


5、安装必须插件

Snipaste_2023-03-22_14-42-52.png

安装git、ssh、docker、docker-build-step插件。安装完成后重启jenkins容器。


6、配置ssh凭证、gitee凭证

一个为ssh docker宿主机,另一个拉取gitee代码凭证

Snipaste_2023-03-22_14-48-11.png


7、配置jenkins任务

Snipaste_2023-03-22_14-57-20.png

docker rm的操作放倒数第二步,免得打包过长,后端无法提供服务。

docker pull registry.cn-hangzhou.aliyuncs.com/liukuo/centos-py3:v1
docker tag registry.cn-hangzhou.aliyuncs.com/liukuo/centos-py3:v1 centos-py3:v1
workdir=${JENKINS_HOME}/workspace/appraved
cd ${workdir}
docker build -t approved:${BUILD_ID} .
docker rm -f approved
docker run -itd -p 9999:9990 --name approved approved:${BUILD_ID}


四、本地python代码提交

打开当前项目工程

Create Git Repository

Snipaste_2023-03-22_15-05-24.png

默认当前项目,点击确定


Git Remotes

Snipaste_2023-03-22_15-07-30.png

Snipaste_2023-03-22_15-09-20.png

git -->add-->Commenit Directory

Snipaste_2023-03-22_15-14-17.png

git -->push

如上图,操作push即可

push error
error:failed to push some refs to 'https://gitee....'
hint: updates were rejected because the remote contains work that you do
Done
hint: not have locally. this is usually caused by another repository pushing
hint: to the same ref. you may want to first integrate the remote changes
hint: (e.g.,'git pull ...') before pushing again.
hint: see the 'note about fast-forwards' in 'git push --help' for details.


解决:

Snipaste_2023-03-22_15-24-26.png

运行 git pull origin master --allow-unrelated-histories


最后重新push,如若还不行,检查下运行上面的命令后,是否有校验弹窗,填写gitee账密即可。


五、代码及dockerfile

1、demo

主函数main.py

from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
    return {"message": "Hello World"}
@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}
if __name__=="__main__":
    uvicorn.run(app="main:app",host="0.0.0.0",port=9990,reload=True)


2、Teiminal生成requirements.txt

PS D:\pycharm_project\approved> pip freeze > requirements.txt

3、dockerfile

EXPOSE端口要跟port的值一致

FROM centos-py3:v1
WORKDIR /app
ADD ./requirements.txt /app
ADD ./main.py /app
RUN pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
EXPOSE 9990
CMD ["python3", "main.py"]


六、启动构建

1、查看jenkins_home代码

[root@a appraved]# ll
总用量 24
-rw-r--r-- 1 1000 1000  162 3月  21 18:56 Dockerfile
-rw-r--r-- 1 1000 1000  326 3月  21 17:29 main.py
-rw-r--r-- 1 1000 1000  952 3月  21 17:29 README.en.md
-rw-r--r-- 1 1000 1000 1313 3月  21 17:29 README.md
drwxr-xr-x 2 root root    6 3月  21 18:38 reports
-rw-r--r-- 1 1000 1000  548 3月  21 17:29 requirements.txt
-rw-r--r-- 1 1000 1000  156 3月  21 17:29 test_main.http
[root@a appraved]# pwd
/data/jenkins_home/workspace/appraved

2、构建日志

Started by user xinyi
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/appraved
The recommended git tool is: NONE
using credential 2bac1267-d600-4018-b9c3-e6423355b3ca
 > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/appraved/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://gitee.com/liu_kuo/approved.git # timeout=10
Fetching upstream changes from https://gitee.com/liu_kuo/approved.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
using GIT_ASKPASS to set credentials gitee pwd
 > git fetch --tags --force --progress -- https://gitee.com/liu_kuo/approved.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision ef577e563242802de780d2c429434cd42dd1ccf2 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f ef577e563242802de780d2c429434cd42dd1ccf2 # timeout=10
Commit message: "update README.md."
 > git rev-list --no-walk 8630421130cde2d657e135e8084ee12a7add475b # timeout=10
[appraved] $ /bin/sh -xe /tmp/jenkins6805498392986962970.sh
+ docker rm -f approved
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/jenkins_home/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
+ docker pull registry.cn-hangzhou.aliyuncs.com/liukuo/centos-py3:v1
v1: Pulling from liukuo/centos-py3
Digest: sha256:0920481436f6a57d6940793bb2cf8ad844c7d2801890ece315b77d8031fe73f0
Status: Image is up to date for registry.cn-hangzhou.aliyuncs.com/liukuo/centos-py3:v1
registry.cn-hangzhou.aliyuncs.com/liukuo/centos-py3:v1
+ docker tag registry.cn-hangzhou.aliyuncs.com/liukuo/centos-py3:v1 centos-py3:v1
+ workdir=/var/jenkins_home/workspace/appraved
+ cd /var/jenkins_home/workspace/appraved
+ docker build -t approved:15 .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
           https://docs.docker.com/go/buildx/
Sending build context to Docker daemon  162.8kB
Step 1/6 : FROM centos-py3:v1
 ---> 08b1090028eb
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> 69bf560badf2
Step 3/6 : ADD . /app
 ---> fbf716d13454
Step 4/6 : RUN pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
 ---> Running in 960a6db55599
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting anyio==3.6.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/77/2b/b4c0b7a3f3d61adb1a1e0b78f90a94e2b6162a043880704b7437ef297cad/anyio-3.6.2-py3-none-any.whl (80 kB)
Collecting click==8.1.3
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c2/f1/df59e28c642d583f7dacffb1e0965d0e00b218e0186d7858ac5233dce840/click-8.1.3-py3-none-any.whl (96 kB)
Collecting colorama==0.4.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Collecting fastapi==0.95.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/3f/91/5412a4c845d1b88cfded182b0e5553e3498a38e5a65a8e9b02e3aaf47dd5/fastapi-0.95.0-py3-none-any.whl (57 kB)
Collecting h11==0.14.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/95/04/ff642e65ad6b90db43e668d70ffb6736436c7ce41fcc549f4e9472234127/h11-0.14.0-py3-none-any.whl (58 kB)
Collecting httptools==0.5.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/79/81/895467fb9dfaca61b6b8349b5ea49921e639b993f1e5f0c9c89270cd5d7e/httptools-0.5.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (417 kB)
Collecting idna==3.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fc/34/3030de6f1370931b9dbb4dad48f6ab1015ab1d32447850b9fc94e60097be/idna-3.4-py3-none-any.whl (61 kB)
Collecting pydantic==1.10.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/37/3b/2589fddb22425a1c29894501947da0bd094105e9a22f09d44bddb3121728/pydantic-1.10.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB)
Collecting python-dotenv==1.0.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/44/2f/62ea1c8b593f4e093cc1a7768f0d46112107e790c3e478532329e434f00b/python_dotenv-1.0.0-py3-none-any.whl (19 kB)
Collecting PyYAML==6.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/12/fc/a4d5a7554e0067677823f7265cb3ae22aed8a238560b5133b58cda252dad/PyYAML-6.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (661 kB)
Collecting sniffio==1.3.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c3/a0/5dba8ed157b0136607c7f2151db695885606968d1fae123dc3391e0cfdbf/sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting starlette==0.26.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/12/48/f9c1ec6bee313aba264fbc2483d9070f4e4526f2538e2b55b1e4a391d938/starlette-0.26.1-py3-none-any.whl (66 kB)
Collecting typing_extensions==4.5.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/31/25/5abcd82372d3d4a3932e1fa8c3dbf9efac10cc7c0d16e78467460571b404/typing_extensions-4.5.0-py3-none-any.whl (27 kB)
Collecting uvicorn==0.21.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8c/f1/7c45fe2a09133e103dcf0621831545c268cd3f7a5d58dc7e470be91b2cd0/uvicorn-0.21.1-py3-none-any.whl (57 kB)
Collecting watchfiles==0.18.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/08/d3/2ceeaba2ec53c166e6e2035ba68f24354b53241d7373dfb12d534bf7c737/watchfiles-0.18.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)
Collecting websockets==10.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d7/f9/f64ec37da654351b212e5534b0e31703ed80d2a6acb6b8c1b1373fafa876/websockets-10.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (106 kB)
Installing collected packages: idna, sniffio, anyio, click, colorama, typing-extensions, pydantic, starlette, fastapi, h11, httptools, python-dotenv, PyYAML, uvicorn, watchfiles, websockets
Successfully installed PyYAML-6.0 anyio-3.6.2 click-8.1.3 colorama-0.4.6 fastapi-0.95.0 h11-0.14.0 httptools-0.5.0 idna-3.4 pydantic-1.10.6 python-dotenv-1.0.0 sniffio-1.3.0 starlette-0.26.1 typing-extensions-4.5.0 uvicorn-0.21.1 watchfiles-0.18.1 websockets-10.4
WARNING: You are using pip version 20.2.3; however, version 23.0.1 is available.
You should consider upgrading via the '/usr/local/python3/bin/python3.9 -m pip install --upgrade pip' command.
Removing intermediate container 960a6db55599
 ---> 0b539ed1a32c
Step 5/6 : EXPOSE 9990
 ---> Running in ab6e4efa3ccd
Removing intermediate container ab6e4efa3ccd
 ---> d4f55139cf45
Step 6/6 : CMD ["python3", "main.py"]
 ---> Running in 2562039eb902
Removing intermediate container 2562039eb902
 ---> 840e3aff83a9
Successfully built 840e3aff83a9
Successfully tagged approved:15
+ docker run -itd -p 9999:9990 --name approved approved:15
fc960fb01c4a427828a853fd4380803891290289fe82ced431c622b092f312cf
Finished: SUCCESS

3、验证

[root@a appraved]# docker ps | grep jenkins
34278757c6a0   d5ed2ceef0ec   "/usr/bin/tini -- /u…"   22 hours ago   Up 22 hours   50000/tcp, 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp   jenkins
[root@a appraved]# docker ps | grep approved
159c8ccb2454   approved:14    "python3 main.py"         5 hours ago    Up 5 hours    0.0.0.0:9999->9990/tcp, :::9999->9990/tcp              approved
[root@a appraved]# docker ps | grep approved
[root@a appraved]# docker ps | grep approved
[root@a appraved]# docker ps | grep approved
fc960fb01c4a   approved:15    "python3 main.py"         6 seconds ago   Up 5 seconds   0.0.0.0:9999->9990/tcp, :::9999->9990/tcp              approved

浏览器访问:192.168.52.131:9999/hello/yes


结果如下:

[root@a appraved]# docker logs -f fc960fb01c4a
INFO:     Will watch for changes in these directories: ['/app']
INFO:     Uvicorn running on http://0.0.0.0:9990 (Press CTRL+C to quit)
INFO:     Started reloader process [1] using WatchFiles
INFO:     Started server process [9]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     192.168.52.1:57704 - "GET /hello/yes HTTP/1.1" 200 OK
INFO:     192.168.52.1:57704 - "GET /hello/yes HTTP/1.1" 200 OK
INFO:     192.168.52.1:57705 - "GET /hello/yes HTTP/1.1" 200

目录
相关文章
|
3月前
|
jenkins 持续交付 开发者
自动化部署:使用Jenkins和Docker实现持续集成与交付
【8月更文挑战第31天】本文旨在为读者揭示如何通过Jenkins和Docker实现自动化部署,从而加速软件开发流程。我们将从基础概念讲起,逐步深入到实际操作,确保即使是初学者也能跟上步伐。文章将提供详细的步骤说明和代码示例,帮助读者理解并应用这些工具来优化他们的工作流程。
|
28天前
|
监控 jenkins 持续交付
Docker和Jenkins有什么不同
【10月更文挑战第18天】Docker和Jenkins有什么不同
|
1月前
|
Java jenkins 持续交付
Centos7下docker的jenkins下载并配置jdk与maven
通过上述步骤,您将成功在CentOS 7上的Docker容器中部署了Jenkins,并配置好了JDK与Maven,为持续集成和自动化构建打下了坚实基础。
104 1
|
1月前
|
运维 jenkins 持续交付
自动化部署的魅力:如何用Jenkins和Docker简化运维工作
【10月更文挑战第7天】在现代软件开发周期中,快速且高效的部署是至关重要的。本文将引导你理解如何使用Jenkins和Docker实现自动化部署,从而简化运维流程。我们将从基础概念开始,逐步深入到实战操作,让你轻松掌握这一强大的工具组合。通过这篇文章,你将学会如何利用这些工具来提升你的工作效率,并减少人为错误的可能性。
|
1月前
|
jenkins Java 持续交付
Docker搭建jenkins环境
这篇文章详细介绍了如何利用Docker搭建Jenkins环境,包括拉取Jenkins镜像、配置端口映射及初始化设置的步骤。
145 0
Docker搭建jenkins环境
|
1月前
|
Ubuntu jenkins 持续交付
Ubuntu系统 用docker安装jenkins
Ubuntu系统 用docker安装jenkins
|
3月前
|
JavaScript jenkins 持续交付
自动化部署与持续集成:使用Jenkins和Docker优化开发流程
【8月更文挑战第31天】在软件开发的世界里,时间就是一切。本文将引导你通过Jenkins和Docker的强大组合,实现自动化部署和持续集成,让你的开发流程如丝般顺滑。我们将从基础设置开始,逐步深入到构建管道,最终实现一键部署的梦想。准备好让你的开发效率飞跃,一起探索这个令人兴奋的旅程吧!
|
3月前
|
jenkins Shell 持续交付
自动化部署:使用Jenkins和Docker实现CI/CD
【8月更文挑战第31天】 本文旨在引导读者了解如何通过Jenkins和Docker来实现持续集成和持续部署(CI/CD),从而优化开发流程,提升工作效率。文章将详细介绍配置Jenkins服务器、创建Docker镜像以及设置自动化构建和部署的步骤。通过实际操作案例,我们将展示如何将代码变更快速部署到测试或生产环境,确保软件质量与发布速度的双重保障。
|
3月前
|
jenkins 持续交付 开发工具
Jenkins 与 Docker 集成的最佳实践
【8月更文第31天】随着容器技术的兴起,越来越多的团队开始采用 Docker 来构建和部署应用。Docker 提供了一种轻量级的虚拟化方法,使得应用可以在任何地方以相同的方式运行,这极大地提高了开发效率和部署的一致性。与此同时,Jenkins 作为一种广泛使用的持续集成/持续交付(CI/CD)工具,可以帮助团队自动化构建、测试和部署流程。本文将探讨如何将 Docker 与 Jenkins 集成,以简化开发环境的搭建和维护。
343 0
|
19天前
|
jenkins Devops Java
DevOps实践:Jenkins在持续集成与持续部署中的价值
【10月更文挑战第27天】在快速发展的软件开发领域,DevOps实践日益重要。Jenkins作为一款流行的开源自动化服务器,在持续集成(CI)和持续部署(CD)中扮演关键角色。本文通过案例分析,探讨Jenkins在Java项目中的应用,展示其自动化构建、测试和部署的能力,提高开发效率和软件质量。
41 2