environment与options

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: environment与options

流水线变量

在pipeline中也是可以定义变量,使用变量的。使用关键字environment

1. 全局变量/局部变量

1.1 全局变量

流水线中也是有局部变量和全局变量之分的,他们都是使用关键字environment来定义

pipeline {
    agent {
        kubernetes {
            cloud 'kubernetes'
            showRawYaml true
            yaml """
"""
        }
    }
    environment {
        key = 'key01'
    }
    stages {
        stage('Hello') {
            steps {
                echo "${ key }"
            }
        }
    }
}

这里我们在stages外面定义了一个environment,里面有变量名key,他的值是key01

然后我们在stages里面输出了这个量,引用变量的格式是"${变量名}"

然后构建这个流水线

Running on hello-kuber-5-svx2v-pnhsc-dz2k7 in /home/jenkins/agent/workspace/hello-kuber
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
key01
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

看最后的输出,他就将变量的值key01给输出了

2. 局部变量

除了全局变量,还可以定义局部变量

pipeline {
    agent {
        kubernetes {
            cloud 'kubernetes'
            showRawYaml true
            yaml """
"""
        }
    }
    environment {
        key = 'key01'
    }
    stages {
        stage('Hello') {
            environment {
                key = 'key02'
            }
            steps {
                echo "${ key }"
            }
        }
    }
}

现在有全局变量key=key01,和一个局部变量key=key02,输出一下这个变量

[Pipeline] echo
key02
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

点击立即构建之后他输出的是key02,全局变量和局部变量冲突之后会采用局部变量,因为局部变量的优先级更高。当然局部变量只存在于这个stages中。

3. credentials插件

我们在流水线中需要使用某个凭证的时候,jenkins也是给我们提供了一个获取凭证的方式,就是使用credentials插件

pipeline {
    agent {
        kubernetes {
            cloud 'kubernetes'
            showRawYaml true
            yaml """
"""
        }
    }
    environment {
        key = 'key01'
        gitlab_user = credentials('test')
    }
    stages {
        stage('Hello') {
            environment {
                key = 'key02'
            }
            steps {
                echo "${ key }"
                echo "${gitlab_user_USR}"
                echo "${gitlab_user_PSW}"
            }
        }
    }
}

在变量里面我们多加了一行内容gitlab_user = credentials('test'),他这个是调用credentials插件来获取id为test这个凭证的内容然后复制给变量gitlab_user,然后我们在stages里面去输出这个凭证的用户名和密码

注意我们输出的方式,我们的变量名是gitlab_user,如果我们获取的凭据他是用户名和密码这种凭据的话,那么这个变量只是一个前缀,需要获取具体的用户名就是gitlab_user.USR

密码就是gitlab_user.PAS,我们来构建一下,看一下输出

[Pipeline] echo
test
[Pipeline] echo
Warning: A secret was passed to "echo" using Groovy String interpolation, which is insecure.
     Affected argument(s) used the following variable(s): [gitlab_user_PSW]
     See https://jenkins.io/redirect/groovy-string-interpolation for details.
****
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

可以看到用户名test已经被输出了,但是在输出密码的时候还是输出的****,这是因为他的保护机制,如果你是正常使用这个密码去连接的话是没有问题的

options

options用于定义pipeline运行时的约束,如是否在执行pipeline时打印时间戳,如果pipeline执行超过2小时,则强行终止并判定失败;禁止并行(这里的并行并不是指pipeline语法中的parallel,而是说一个任务不能有两个job同时运行)等等。

options的示例:

options {
    timestamps() //日志会有时间
    skipDefaultCheckout() //删除隐式checkout scm语句
    disableConcurrentBuilds() //禁止并行
    timeout(time: 2, unit: 'HOURS')
}

常用options说明如下:

  • buildDiscarder:为最近的流水线运行的特定数量保存组件和控制台输出
  • disableConcurrentBuilds:关闭并行执行构建,可被用来防止同时访问共享资源等
  • overrideIndexTriggers:允许覆盖分支索引触发器的默认处理
  • skipDefaultCheckout:在pipeline中,如果配置有代码仓库,则agent会默认检出代码,此选项用于跳过这个默认动作
  • skipStagesAfterUnstable:一旦构建状态变得unstable,则跳过该阶段
  • checkoutToSubdirectory:在工作空间的子目录中自动的执行源码检出
  • timeout:设置流水线运行的超时时间
timeout(time: 30, unit: 'MINUTES')
  • retry:在失败时,指定重试次数
  • timestamps:在流水线输出时,打印时间

1. 定义options

如果在options里面加入了timestamps(),那么需要安装一个插件叫做 timestamper

如果没有按装直接定义这个options的话构建是会报错的

pipeline {
    agent {
        kubernetes {
            cloud 'kubernetes'
            showRawYaml true
            yaml """
"""
        }
    }
    options {
    timestamps() 
    skipDefaultCheckout() 
    disableConcurrentBuilds() 
    timeout(time: 2, unit: 'HOURS')
    }
    environment {
        key = 'key01'
        gitlab_user = credentials('test')
    }
    stages {
        stage('Hello') {
            environment {
                key = 'key02'
            }
            steps {
                echo "${ key }"
                echo "${gitlab_user_USR}"
            }
        }
    }
}

然后我们在输出里面就可以看到有时间了

16:29:08  key02
[Pipeline] echo
16:29:08  test
[Pipeline] echo
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

本文来自博客园,作者:FuShudi,转载请注明原文链接:https://www.cnblogs.com/fsdstudy/p/18263594

分类: Euler / HCIE / CICD

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
9天前
|
并行计算 PyTorch 算法框架/工具
options
这篇文章描述了在使用modelscope库下载模型时遇到的`TypeError: __init__() got an unexpected keyword argument ‘allowed_methods’`错误,并提供了通过升级`urllib3`库来解决该错误的方案。
options
|
2月前
|
Java Maven
The JAVA_HOME environment variable is not defined correctly,this environment variable is needed to
The JAVA_HOME environment variable is not defined correctly,this environment variable is needed to
ValidationError: Invalid options object. Dev Server has been initialized using an options object tha
ValidationError: Invalid options object. Dev Server has been initialized using an options object tha
|
4月前
|
设计模式 人工智能 Java
Error: JavaFX runtime components are missing, and are required to run this application with Gradle e
Error: JavaFX runtime components are missing, and are required to run this application with Gradle e
202 1
|
Java Maven
Maven配置报错The JAVA_HOME environment variable is not defined correctly
Maven配置报错The JAVA_HOME environment variable is not defined correctly
200 1
|
9月前
Error: Plugin/Preset files are not allowed to export objects, only functions……
Error: Plugin/Preset files are not allowed to export objects, only functions……
|
Java 应用服务中间件
Tomcat出现the jre_home environment variable is not defined correctly this environment variable is need
今天运行Tomcat点击startup.bat cmd直接一闪而过,直接闪退了,很难受,原因是前几天手贱不小心把环境全删了,好难受,只能重新安装了,现在官网下载jdk,我的是jdk-17.0.2,路径是`D:\javase\jdk-17.0.2` 记住这个路径(很重要),然后在环境变量里设置,在用户变量里添加JAVA_HOME与JRE_HOME把我们刚刚的路径复制进去。在系统变量里面的PATH新建的添加%JAVA_HOME%\bin 与%JRE_HOME%\bin 点击确定。环境配置好了。
649 0
Tomcat出现the jre_home environment variable is not defined correctly this environment variable is need
|
应用服务中间件
The JRE_HOME environment variable is not defined correctly This environment【tomcat闪退】
The JRE_HOME environment variable is not defined correctly This environment【tomcat闪退】
100 0
The JRE_HOME environment variable is not defined correctly This environment【tomcat闪退】
|
应用服务中间件
tomcat闪退[【the jre_home environment variable is not defined correctly this environment variabl】
tomcat闪退[【the jre_home environment variable is not defined correctly this environment variabl】
137 0
tomcat闪退[【the jre_home environment variable is not defined correctly this environment variabl】
|
Java Maven
No valid Maven installation found. Either set the home directory in the configuration dialog or set
No valid Maven installation found. Either set the home directory in the configuration dialog or set
817 0
No valid Maven installation found. Either set the home directory in the configuration dialog or set