首页 > 其他分享 >jenkins结合远程仓库

jenkins结合远程仓库

时间:2023-12-18 16:22:05浏览次数:30  
标签:git 仓库 root flask master ssh jenkins my 远程

 

既然是持续集成,对代码进行构建,我们得获取代码仓库的内容,这里选择我们搭建的gitlab服务器

1.开发工程师的机器

1. 在window上生成ssh-key

$ ssh-keygen.exe -t rsa -C 'www.yuchaoit.cn'


2.添加到代码仓库github/gitlab都玩一玩,公司用这俩居多

 

gitlab上添加该机器的ssh-key允许上传代码,咱这里就不区分多个账户,多个权限了,都先基于root账户,实现部署流程,理解jenkins是怎么工作的。

 

2.gitlab新建项目

 

3.开发提交代码

1.准备好代码

这里利用python程序,flask代码做实验

# coding:utf-8

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>Hello 超哥带你学linux www.yuchaoit.cn</h1>'

if __name__ == '__main__':
    app.run()

2.克隆gitlab代码仓库到本地

1.先设置git身份
Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop
$ git config --global user.name "laoliu"

Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop
$ git config --global user.email "[email protected]"





2.克隆代码

Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop
$ git clone [email protected]:root/my_flask.git
Cloning into 'my_flask'...
The authenticity of host '10.0.0.99 (10.0.0.99)' can't be established.
ED25519 key fingerprint is SHA256:fsFB+VUXvu9atyktELhNhs0zzRdli9XbqehOn2We9yo.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.99' (ED25519) to the list of known hosts.
warning: You appear to have cloned an empty repository.

3.开发代码,进行提交,推送到代码仓库

Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop/my_flask (master)
$ ls
my_app.py

Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop/my_flask (master)
$ cat my_app.py

# coding:utf-8

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>Hello 超哥带你学linux www.yuchaoit.cn</h1>'

if __name__ == '__main__':
    app.run(host="0.0.0.0")


4.版本提交,推送gitlab
$ git add .

$ git commit -m 'my_app.py 首次开发'
[master (root-commit) 3e973ff] my_app.py 首次开发
 1 file changed, 11 insertions(+)
 create mode 100644 my_app.py


Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop/my_flask (master)
$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 20 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 402 bytes | 402.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 10.0.0.99:root/my_flask.git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.

4.检查代gitlab码仓库

 

5.配置jenkins的job获取代码仓库

 

给jenkins服务器安装git
[root@jenkins-100 ~]#yum install git -y

 

添加jenkins服务器的ssh-key到gitlab服务器
[root@jenkins-100 ~]#ssh-keygen -t rsa -C "www.yuchaoit.cn"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WRnTFIqkwh7IXB2gWyFkrkE1BLwx9bYHtD9YRYFdbgk www.yuchaoit.cn
The key's randomart image is:
+---[RSA 2048]----+
|.+O=++..=E=+o.   |
|.O *+.o+.oo=o    |
|. X ==... ++     |
| + +.o*  o.      |
|. . .o +S        |
|      . .        |
|                 |
|                 |
|                 |
+----[SHA256]-----+


[root@jenkins-100 ~]#cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9C597NnGpdyRYkDtF4zQmTa+bRxXqll3XX7LJDjLfsgfUZbfolj0KwkmdIvpQjecDrKff33bOIhGQQ64okmQlKPyp+iISO6sRCH1p2VhZNFEWOeBRtzA+TFrLX4WeVFJFg2IuOE1cFuKGESBC7pqZZf4H12QaNCunLwWLTrqoUGvfW0+rXOBGaXPW1yNpTMevnPkN81ZKiqhONtUE+suYwwYi8zgi54CXZZBNEcyXhZH2gLLser/hy+16vqYZ65enGBcfPYBNSHt35DcNs/Qs6nLpT/UBxblQwFI5ktq7C6cm6igYVAuVpomDNdD+LCjvRhijQBCbxlvHwXcO9Tl www.yuchaoit.cn

 

添加jenkins的认证凭证,使用自己的私钥即可

 


 

至此,jenkins就可以下载gitlab的代码了。

试试看能下载什么,点击构建

 

6.开发脚本实现项目部署

注意是,jenkins > 目标机器(web-7测试)

# 注意免密登录了
ssh-copy-id [email protected]

部署代码

若是做更多考虑,可以先成脚本,然后远程去调用。



#!/bin/bash
# author: www.yuchaoit.cn

# 发送代码到目标机器
cd /var/lib/jenkins/workspace/my_flask  && scp my_app.py [email protected]:/opt/

# 给远程机器部署python3环境,代码运行环境
ssh [email protected] "yum install python3 python3-devel python3-pip"
ssh [email protected] "pip3 install flask"


# 远程启动进程,后台运行
# 重启进程
ssh [email protected] "pkill python3"

ssh [email protected] "nohup /usr/bin/python3 /opt/my_app.py >/dev/null 2>&1 &"

 

7.测试访问web7的flask项目

 

8.完成项目更新(鼠标一点,自动更新,操心啥啊)

让开发去写代码就好了


Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop/my_flask (master)
$ vim my_app.py

Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop/my_flask (master)
$

Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop/my_flask (master)
$ git add .
warning: in the working copy of 'my_app.py', LF will be replaced by CRLF the next time Git touches it

Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop/my_flask (master)
$ git commit -m '更新代码'
[master 5ff3d74] 更新代码
 1 file changed, 1 insertion(+), 1 deletion(-)


推送代码

Sylar@DESKTOP-G6C412R MINGW64 ~/Desktop/my_flask (master)
$ git push -u origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 301 bytes | 301.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To 10.0.0.99:root/my_flask.git
   3604269..5ff3d74  master -> master
branch 'master' set up to track 'origin/master'.

9.鼠标一点,下班下班

 

标签:git,仓库,root,flask,master,ssh,jenkins,my,远程
From: https://www.cnblogs.com/sxy-blog/p/17911527.html

相关文章

  • CentOS中部署Jenkins
    1、jenkins安装包镜像地址https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable2、下载jenkins安装包wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.426.2-1.1.noarch.rpm3、安装jenkinsrpm-ivh jenkins-2.426.2-1.1.noarch.rpm......
  • CentOS中jenkins服务启动正常但是浏览器无法访问
    解决方法:1、检查下jenkins服务使用命令:systemctlstatusjenkins(检查状态是否存在报错信息)2、查看端口使用命令:ps-ef|grepjenkins(检查端口是否正常启用)3、检查防火墙使用命令:systemctlstatusfirewalld.service(检查防火墙状态是否正常)4、检查端口占用情况使用命令:nets......
  • Linux 安装 Harbor Docker镜像仓库&&远程访问
    Harbor是为企业用户设计的容器镜像仓库开源项目,包括了权限管理(RBAC)、LDAP、审计、安全漏洞扫描、镜像验真、管理界面、自我注册、HA等企业必需的功能,同时针对中国用户的特点,设计镜像复制和中文支持等功能。下面介绍如何在Linux安装并且使用cpolar内网穿透工具实现远程访问Har......
  • Git本地代码仓库关联远程仓库
    需求背景我在本地创建了一个项目,并且编写了一些代码后,通过使用Git初始化了本地仓库。此时,我又在GitHub上创建了一个仓库,并添加了一个README文件。现在,我想把本地的仓库与GitHub上的仓库关联,并将代码推送到远程仓库。这时候应该怎么办?操作步骤1、在本地项目的目录下初始化Git仓库git......
  • Harbor 本地docker镜像仓库远程连接(镜像管理零距离!!)
    简单几步,实现本地Harbordocker镜像仓库结合cpolar内网穿透实现远程推送和拉取镜像1.查看本地镜像进入本地Harbordocker仓库,我们可以看到我们的本地镜像,这边方便测试,提前上传了一个镜像  在左侧可以看到推送命令,这里局域网ip没有出现端口,说明默认使用80端口,知......
  • Docker 开启远程端口访问2375
    开启方法:1、修改/etc/default/docker下的配置cat/etc/default/dockerDOCKER_OPTS="-Htcp://0.0.0.0:2375"systemctlrestartdocker2、修改/usr/lib/systemd/system/docker.service配置cat/usr/lib/systemd/system/docker.service[Service]ExecStart=/usr/bin/dockerd-Htc......
  • docker-compose 部署 harbor 镜像仓库
    1、安装docker(这个就不写了,可以看)略......2、安装docker-compose[root@master2~]#curl-L"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname-s)-$(uname-m)"-o/usr/local/bin/docker-compose[root@master2~]#chmod+x/usr/loc......
  • SpringBoot开启注册发现并使用feign进行远程调用
    1、安装nacos服务端nacos下载地址https://github.com/alibaba/nacos/releases下载nacos之后解压并启动Nacos默认端口是88482、配置maven坐标信息2.1配置坐标管理<dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</......
  • docker从0安装Jenkins
    docker从0安装JenkinsUbuntu初始化sudoapt-getinstallopenssh-serversudovim/etc/ssh/sshd_config设置静态IPcd/etc/netplan···network:version:2renderer:NetworkManagerethernets:ens33:#网卡名称dhcp4:no#关闭dhcp......
  • Git本地代码仓库关联远程仓库
    需求背景我在本地创建了一个项目,并且编写了一些代码后,通过使用Git初始化了本地仓库。此时,我又在GitHub上创建了一个仓库,并添加了一个README文件。现在,我想把本地的仓库与GitHub上的仓库关联,并将代码推送到远程仓库。这时候应该怎么办?操作步骤1、在本地项目的目录下初始化Git仓库git......