Jenkins 新建配置Pipeline任务 三

发布于:2025-02-17 ⋅ 阅读:(145) ⋅ 点赞:(0)

Jenkins 新建配置Pipeline任务 三

一. 登录 Jenkins

网页输入 http://localhost:8080
在这里插入图片描述
输入账号、密码登录
在这里插入图片描述
一个没有创建任务的空 Jenkins

二. 创建 任务

图 NewItem
在这里插入图片描述
界面左上角 + New Item
图NewItemSelect
在这里插入图片描述
1.Enter an item name:输入任务名
2.Select an item type:选择 Pipeline:Pipeline 表示创建的是一个流水线任务
3.点击 OK
创建完成默认打开界面如下
图Configure
图Configure
修改参数、配置 之后点击 下面 Apply 按钮
需要退出编辑界面点击 Save 按钮
点击左上角 Dashboard-> TestProject1,回到项目基本信息界面
在这里插入图片描述
Stauts:项目状态,这是一个新任务,右侧只显示了 TestProject1 、Permalinks
Changes:记录变化信息,一般不需要关注
Build Now:点击这个按钮,开始执行构建
Configure:这个任务的所有配置在这里修改,点击Configure,回到 图Configure 界面
Delete Pipeline:删除这个任务
Stages:记录每次构建执行的阶段,一般不需要关注
Rename:重命名,点击进入修改任务名界面
Pipeline Syntax:流水线语法

三. 配置任务

点击 Configure 按钮,切换到配置界面
在这里插入图片描述
在这里插入图片描述
先看左上角 Configure
General:
Triggers:
Pipeline:
Advanced:
分别对应右侧不同模块
切换方式一:在左上角点击任意一个,就会切换到它对应的模块
切换方式二:在右侧区域,鼠标滚轮或者上下拖动网页,也可以看到所有的模块

四、General 模块

Description:项目描述
在这里插入图片描述
勾选 Discard old builds

  • Discard old builds:控制何时丢弃项目的构建记录
    • Days to keep builds :如果非空,例如填 5,则构建日期超过5天的记录将被删除
    • Max # of builds to keep :如果非空,例如填10,则当构建次数超过10次时,丢弃最旧的构建
      Days to keep builds 和 Max # of builds to keep 这两个选项可以同时激活,因此可以将构建保存7天,但最多只能保存10个构建。如果超过任何一个限制,那么超出该限制的任何构建都将被丢弃
      在 Advanced 部分,可以指定相同的选项

在这里插入图片描述

  • Do not allow concurrent builds:不允许并行构建,如果当前正在构建,执行新的构建,默认为等待当前的构建完成,再开始新的构建

    • Abort previous builds:勾选这个,如果当前正在构建,则立即停止构建,重新开始新的构建
  • Do not allow the pipeline to resume if the controller restarts:当Jenkins重启时,不恢复流水线

  • Throttle builds:控制每次构建的间隔

    • Number of builds:构建之间的间隔时长
    • Time period:时间类型控制 Number of builds:Year、Month、Week、Day、Hour、Minute、Second

在这里插入图片描述
勾选 This project is parameterized

五、Triggers

在这里插入图片描述
顾名思义就是触发器

Build after other projects are built:设置一个触发器,以便在其他项目完成构建时,为该项目安排一个新的构建
Build periodically:定期自动构建
在这里插入图片描述
如上配置,周一到周五每天早上 7:00 自动构建这个任务
GitHub hook trigger for GITSCM polling:跟 GitHub 推送 Hook 时,检测触发构建的机制,一般也用不到

六、Pipeline

在这里插入图片描述
在这里插入图片描述
在 Definition 处选择 Pipeline script from SCM
配置使用 SCM 源代码存储库中的 Pipeline 脚本
关于 Git 的配置看 Git仓库配置
在这里插入图片描述
Script Path:这个路径就是在 git 仓库中已经写好上传的 Pipeline 脚本,看下面 git 仓库中的文件
其中 testPipeline 就是 Pipeline 脚本,在跟目录
在这里插入图片描述
testPipeline 脚本内容如下

pipeline {
    agent any
    stages {
        stage('Print WorkSpace') {
            steps {
                script {
				    echo "Hello world"
                    echo "当前的工作目录: ${env.WORKSPACE}"
                }
            }
        }
    }
}

编辑完成之后点击 按钮 Apply -> 按钮 Save

七、构建任务

回到 TestProject1 配置界面
点 Build With parameters
在这里插入图片描述
在这里插入图片描述
构建信息会显示在 左侧下方的 Builds 区域,上图中显示的状态是 14:55 时刻的一个构建,状态显示未 绿色对勾,表示构建成功

那么这个构建做了什么?
执行的内容就是 Configure 模块中的 Pipeline 部分

1.在构建界面选择了:构建的分支 会赋值给参数 BRANCH_NAME
2.点 Build 会执行到 Pipeline script from SCM 模块的 Git,会执行 Git Checkout 分支的操作,把项目拉取到本地,切换到BRANCH_NAME分支
3.git 操作完成之后,调用 Script Path 路径配置的 Pipeline 脚本,也就是 我这里的 git 仓库目录的 Pipeline/testPipeline 脚本
这个脚本代码在上面已经有了,其实就做了两个输出

	 echo "Hello world"
     echo "当前的工作目录: ${env.WORKSPACE}"

关于 Pipeline 的语法,可以看到 Pipeline 模块最下方有一个 Pipeline Syntax 可以点击查看需要的各种功能应该怎么写

八、查看构建结果

在这里插入图片描述
选择最后一次构建,点击打开
在这里插入图片描述
1.Stauts:构建状态
在这里插入图片描述
2. **Console Output:**构建过程中的所有输出日志

Started by user admin
Lightweight checkout support not available, falling back to full checkout.
Checking out git https://github.com/LIQIANGEASTSUN/JenkinsTest.git into /Users/liqiang/.jenkins/workspace/TestProject1@script/a82089facc9f5b750180801ee6442a8ef978414d304ab5cc580f6728c566c0a8 to read testPipeline
The recommended git tool is: NONE
using credential xxx_credentials
 > git rev-parse --resolve-git-dir /Users/liqiang/.jenkins/workspace/TestProject1@script/a82089facc9f5b750180801ee6442a8ef978414d304ab5cc580f6728c566c0a8/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/LIQIANGEASTSUN/JenkinsTest.git # timeout=10
Fetching upstream changes from https://github.com/LIQIANGEASTSUN/JenkinsTest.git
 > git --version # timeout=10
 > git --version # 'git version 2.39.5 (Apple Git-154)'
using GIT_SSH to set credentials xxx project git credentials
Verifying host key using known hosts file
 > git fetch --tags --force --progress -- https://github.com/LIQIANGEASTSUN/JenkinsTest.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 9d2960eafc0c158d780d5849b638ebaf303cb96b (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 9d2960eafc0c158d780d5849b638ebaf303cb96b # timeout=10
Commit message: "ceshi"
First time build. Skipping changelog.
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/liqiang/.jenkins/workspace/TestProject1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
using credential xxx_credentials
 > git rev-parse --resolve-git-dir /Users/liqiang/.jenkins/workspace/TestProject1/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/LIQIANGEASTSUN/JenkinsTest.git # timeout=10
Fetching upstream changes from https://github.com/LIQIANGEASTSUN/JenkinsTest.git
 > git --version # timeout=10
 > git --version # 'git version 2.39.5 (Apple Git-154)'
using GIT_SSH to set credentials xxx project git credentials
Verifying host key using known hosts file
 > git fetch --tags --force --progress -- https://github.com/LIQIANGEASTSUN/JenkinsTest.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 9d2960eafc0c158d780d5849b638ebaf303cb96b (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 9d2960eafc0c158d780d5849b638ebaf303cb96b # timeout=10
Commit message: "ceshi"
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Print WorkSpace)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello world
[Pipeline] echo
当前的工作目录: /Users/liqiang/.jenkins/workspace/TestProject1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

点击 Console Output 右侧显示输出日志,可以看到日志中一些 git 操作命令
以及 Pipeline 执行的输出结果

# 可以看到 Commit message:这个是 构建的 git 分支,的提交记录
Commit message: "ceshi"
[Pipeline] echo
Hello world
[Pipeline] echo
当前的工作目录: /Users/liqiang/.jenkins/workspace/TestProject1
  1. Parameters
    在这里插入图片描述
    点击左侧的 Parameters 在右侧显示构建时,所有参数的值

  2. Pipeline Console:Pipeline 模块输出日志信息
    在这里插入图片描述

  3. Workspaces:上面执行 git 操作拉取 git 仓库,就是存储在这个目录了
    在这里插入图片描述
    可以看到一个目录路径,点击路径,可以将项目显示在 Jenkins 界面上
    在这里插入图片描述
    可以点击文件夹一层一层打开
    在任何一层 点击 下方的 all files in zip 就会将这个目录下的所有文件打包到一个 zip 中并且下载
    点击任何一个单独的文件,会自动下载这个文件

到此 Jenkins 新建配置 Pipeline 任务的基本操作已经完成了


网站公告

今日签到

点亮在社区的每一天
去签到