【DevOps】Jenkins使用Pipline发布Web项目

使用Pipline发布Web项目

文章目录

资源列表

操作系统 主机名 配置 IP
CentOS 7.9 jenkins 2C4G 192.168.93.101
CentOS 7.9 gitlab 2C4G 192.168.93.102
CentOS 7.9 apache 2C4G 192.168.93.103

基础环境

  • 关闭防火墙
bash 复制代码
systemctl stop firewalld
systemctl disable firewalld
  • 关闭内核安全机制
bash 复制代码
setenforce 0
sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
  • 修改主机名
bash 复制代码
hostnamectl set-hostname jenkins
hostnamectl set-hostname gitlab
hostnamectl set-hostname apache

一、准备Gitlab

1.1、部署Gitlab

  • 这里省略步骤

1.2、创建demo项目

1.3、提交代码

  • 本次就在jenkins主机上进行测试了
bash 复制代码
# 下载Git命令
[root@jenkins ~]# yum -y install git


# 解压源代码
[root@jenkins ~]# tar -zxvf BlueLight.git.tar.gz


# 拉取代码仓库
[root@jenkins ~]# git clone http://192.168.93.102/root/demo.git


# 复制源代码到仓代码库
[root@jenkins ~]# mv -f BlueLight/* demo/


# 往main分支进行第一次推送
[root@jenkins ~]# cd demo/
[root@jenkins demo]# git config --global user.email "you@example.com"
[root@jenkins demo]# git config --global user.name "Your Name"
[root@jenkins demo]# git add .
[root@jenkins demo]# git commit -m "first commit"
[root@jenkins demo]# git push -u origin main

二、准备Jenkins

  • 这里省略

三、准备Apache

bash 复制代码
[root@apache ~]# yum -y install httpd
[root@apache ~]# systemctl start httpd

四、Jenkins的配置

4.1、Jenkins配置SSH主机

  • 点击"系统管理(System)"------>"系统配置"------>最下边"SSH Server下的新增"

4.2、Jenkins配置gitlab的密码

  • 点击"系统管理(System)------>"凭据管理(Credentials)"------>"全局"------>"Add Credentials"

4.3、Jenkins创建demo项目

4.4、配置流水线

  • 可以使用流水线语法,生产Pipeline的代码然后粘贴过来
  • 把生成的Pipeline命令替换进去,stage给本阶段起名叫拉取代码
  • 在生成一个流水线语法,
bash 复制代码
# 构建代码如下,需要进行修改
pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                git branch: 'main', credentialsId: '5592dcbd-ca4d-4d75-8793-5f834c11014b', url: 'http://192.168.93.102/root/demo.git'
            }    
        }        
        stage('部署网站') {
            steps {
            sshPublisher(publishers: [sshPublisherDesc(configName: 'web', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'chmod -R 755 /var/www/html', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/*')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
}

4.5、访问测试

4.6、将Pipeline放在gitlab仓库

  • 创建一个文件"Jenkins",将刚才的pipline代码填写到此文件

  • 注意:Jenkins首字母要大写(J)

4.7、重新配置Jenkins的流水线项目

  • 在定义中选择"Pipline script from SCM",SCM使用"Git",填写号仓库URL,然后保存
  • 备注:SCM叫做源代码控制

4.8、构建项目


相关推荐
li35741 天前
将已有 Vue 项目通过 Electron 打包为桌面客户端的完整步骤
前端·vue.js·electron
Icoolkj1 天前
VuePress 与 VitePress 深度对比:特性、差异与选型指南
前端·javascript·vue.js
excel1 天前
CNN 分层详解:卷积、池化到全连接的作用与原理
前端
excel1 天前
CNN 多层设计详解:从边缘到高级特征的逐层学习
前端
西陵1 天前
Nx带来极致的前端开发体验——任务编排
前端·javascript·架构
大前端helloworld1 天前
从初中级如何迈入中高级-其实技术只是“入门卷”
前端·面试
苦逼IT运维1 天前
Jenkins 监控方案:Prometheus + Grafana 实践
jenkins·grafana·prometheus
东风西巷1 天前
Balabolka:免费高效的文字转语音软件
前端·人工智能·学习·语音识别·软件需求
萌萌哒草头将军1 天前
10个 ES2025 新特性速览!🚀🚀🚀
前端·javascript·vue.js