Vid*_*sha 0 jenkins jenkins-groovy jenkins-pipeline
我需要在我的詹金斯管道之一中使用昨天的日期。
有没有办法在詹金斯管道脚本中打印昨天的日期。
def date = new date()
println date // this is printing the current date
def date1 = date - 1 or date.minus(1)
println date 1 // This is also printing the current date.
Run Code Online (Sandbox Code Playgroud)
有没有办法在詹金斯管道脚本中获取昨天的日期。
这会起作用的。
def today = new Date()
def yesterday = today - 1
println today.format("MM/dd/yyyy")
println yesterday.format("MM/dd/yyyy")
Run Code Online (Sandbox Code Playgroud)
输出:
03/25/2020 -- Today's date
03/24/2020 -- Yesterday's date
Run Code Online (Sandbox Code Playgroud)
同时如果您的代码如下,
def date = new Date()
println date
def date1 = date - 1
println date1
Run Code Online (Sandbox Code Playgroud)
如果没有格式,它会像这样打印。
Wed Mar 25 09:21:57 GMT 2020
Tue Mar 24 09:21:57 GMT 2020
Run Code Online (Sandbox Code Playgroud)
Jenkins 声明式管道示例:
#! groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
def today = new Date()
def yesterday = today - 1
def daybeforeyesterday = yesterday.previous()
println "Today: " + today.format("MM/dd/yyyy") + " && Yesterday: " +
yesterday.format("MM/dd/yyyy") + " && The Day before yesterday: " +
daybeforeyesterday.format("MM/dd/yyyy")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on agent-j2sxm in /home/jenkins/workspace/
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Today: 03/26/2020 && Yesterday: 03/25/2020 && The Day before yesterday: 03/24/2020
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7303 次 |
| 最近记录: |