GitLab与jekins结合构建持续集成(cl)环境(4)

pipeline流水线

Pipeline模式为管道模式,也称为流水线模式。通过预先设定好的一系列的阶段来处理输入的数据,每个阶段的输出即是下一个阶段的输入。

流水线格式

分为声明式流水线和脚本式流水线

#声明式流水线格式
pipeline {
	agent any
	stages {
		stage('Build') {
			steps {
				sh 'make'
			}
		}
		stage('Test') {
			steps {
				sh 'make check'
				junit 'reports/**/*.xml'
			}
		}
		stage('Deloy') {
			steps {
				sh 'make publish'
			}
		}
	}
}


agent any:在任何可用的代理上执行流水线或它的任何阶段,也就是执行流水线过程的位置,也可以指定到具体的节点
stage:定义流水线的执行过程(相当于一个阶段),比如下文所示的 Build、Test、Deploy, 但是这个名字是根据实际情况进行定义的,并非固定的名字
steps:执行某阶段具体的步骤
#脚本流水线
node{
	stage('Build'){
		checkout scm
		echo 'start build'
	}
	stage('Test'){
		echo 'start test'
	}
	stage('Deploy'){
		echo 'start deploy'
	}
}

Node(节点): 一个 Node 就是一个 Jenkins 节点,或者是 Master,或者是 Agent,是执行 Step 的具体运行环境,Pipeline 执行中的大部分工作都是在一个或多个声明 Node 步骤的上下文中完成的。如果不指定参数,则默认在master节点运行job
Stage(环节): 一个 Pipeline 可以从逻辑上划分为若干个 Stage,每个 Stage 代表一组操作,如:Build、Test、Deploy。注意,Stage 是一个逻辑分组的概念,可以跨多个 Node。即,一个stage语句块可以包含node,表示不同的节点执行响应的任务步骤。
Step(步骤): Step 是最基本的操作单元,小到执行一个 Shell 脚本,大到构建一个 Docker 镜像,由各类 Jenkins 插件提供,当插件扩展Pipeline DSL 时,通常意味着插件已经实现了一个新的步骤。

流水线方式发布静态页面

#先修改内容
git clone git@192.168.100.5:liuyuhao/test.git
cd test

#删除里面的内容
git rm -r test1/
git rm time.java

#写入新的内容
vim index.html
2023-4-20

#发布
git add .
git commit -m 'add time'
git push origin main

 将这一串复制到

pipeline {
	agent any
	stages {
		stage('拉代码') {
			steps {
				checkout scmGit(branches: [[name: '*/main']], browser: gitLab(repoUrl: 'http://192.168.100.5/liuyuhao/test.git', version: '15.9'), extensions: [], userRemoteConfigs: [[credentialsId: 'd2f7816c-b4c9-4a38-8335-155bc22fb017', url: 'git@192.168.100.5:liuyuhao/test.git']])
			}
		}
		stage('发布代码') {
			steps {
				sh 'scp /var/lib/jenkins/workspace/pipline/index.html  root@192.168.100.10:/var/www/html'
			}
		}
	}
}

 然后把发布代码写完,保存应用,构建 

前几次出了点小问题,第五次构建成功,之前没成功是因为/var/lib/jenkins/workspace/pipline/index.html没有修改路径,还用的之前的/var/lib/jenkins/workspace/test/index.html

浏览器访问

pipline参数化构建

 

 复制这串代码

pipeline {
	agent any
	parameters {
		choice choices: ['Deploy', 'Rollback'], description: '''Deploy:发布	Rollback:回滚''', name: 'status'
		string description: '''----------------------此项是回滚的版本号,如果您是发布,请忽律----------------------
		----------------------如果回滚请查询本项目在git中的commit_id----------------------''',name: 'version'
	}	
	stages {
		stage('拉取代码') {
			when {
				environment name: 'status', value: 'Deploy'
			}
			steps {
				checkout scmGit(branches: [[name: '*/main']], browser: gitLab(repoUrl: 'http://192.168.100.5/liuyuhao/test.git', version: '15.9'), extensions: [], userRemoteConfigs: [[credentialsId: 'd2f7816c-b4c9-4a38-8335-155bc22fb017', url: 'git@192.168.100.5:liuyuhao/test.git']])
			    sh 'scp /var/lib/jenkins/workspace/canshu/index.html  root@192.168.100.10:/var/www/html'
			}
		}
		
		stage('回滚代码') {
			when {
				environment name: 'status', value: 'Rollback'
			}
			
			steps {
				sh 'echo 本次操作为回滚'
				sh 'git reset --hard ${params.version}'
                sh 'scp /var/lib/jenkins/workspace/canshu/index.html  root@192.168.100.10:/var/www/html'
			}
		}
	}
}

构建第一次成功后,需要构建第二次才会弹出参数

 测试一下

[root@bogon ~]#cd test/
[root@bogon test]#ls
test1  time.java
[root@bogon test]#
[root@bogon test]#git rm time.java
rm 'time.java'
[root@bogon test]#git rm -r time.java
fatal: 路径 'time.java' 未匹配任何文件
[root@bogon test]#git rm -r test1/
rm 'test1/.classpath'
rm 'test1/.project'
rm 'test1/.settings/.jsdtscope'
rm 'test1/.settings/org.eclipse.jdt.core.prefs'
rm 'test1/.settings/org.eclipse.m2e.core.prefs'
rm 'test1/.settings/org.eclipse.wst.common.component'
rm 'test1/.settings/org.eclipse.wst.common.project.facet.core.xml'
rm 'test1/.settings/org.eclipse.wst.jsdt.ui.superType.container'
rm 'test1/.settings/org.eclipse.wst.jsdt.ui.superType.name'
rm 'test1/.settings/org.eclipse.wst.validation.prefs'
rm 'test1/pom.xml'
rm 'test1/src/main/resources/com/Time.java'
rm 'test1/src/main/webapp/WEB-INF/web.xml'
rm 'test1/src/main/webapp/index.jsp'
rm 'test1/target/classes/com/Time.class'
rm 'test1/target/classes/com/Time.java'
rm 'test1/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF'
rm 'test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.properties'
rm 'test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.xml'
[root@bogon test]#
[root@bogon test]#
[root@bogon test]#vim index.html
[root@bogon test]#git add .
[root@bogon test]#git commit -m "add ceshi"
[main a60cb7c] add ceshi
 21 files changed, 1 insertion(+), 262 deletions(-)
 create mode 100644 index.html
 delete mode 100644 test1/.classpath
 delete mode 100644 test1/.project
 delete mode 100644 test1/.settings/.jsdtscope
 delete mode 100644 test1/.settings/org.eclipse.jdt.core.prefs
 delete mode 100644 test1/.settings/org.eclipse.m2e.core.prefs
 delete mode 100644 test1/.settings/org.eclipse.wst.common.component
 delete mode 100644 test1/.settings/org.eclipse.wst.common.project.facet.core.xml
 delete mode 100644 test1/.settings/org.eclipse.wst.jsdt.ui.superType.container
 delete mode 100644 test1/.settings/org.eclipse.wst.jsdt.ui.superType.name
 delete mode 100644 test1/.settings/org.eclipse.wst.validation.prefs
 delete mode 100644 test1/pom.xml
 delete mode 100644 test1/src/main/resources/com/Time.java
 delete mode 100644 test1/src/main/webapp/WEB-INF/web.xml
 delete mode 100644 test1/src/main/webapp/index.jsp
 delete mode 100644 test1/target/classes/com/Time.class
 delete mode 100644 test1/target/classes/com/Time.java
 delete mode 100644 test1/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF
 delete mode 100644 test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.properties
 delete mode 100644 test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.xml
 delete mode 100644 time.java
[root@bogon test]#git push origin main
Counting objects: 4, done.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (3/3), 251 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.5:liuyuhao/test.git
   9e182b7..a60cb7c  main -> main
[root@bogon test]#

构建

 回滚

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值