How do I use `when` condition on a file's last modified time in Jenkins pipeline syntax
up vote
2
down vote
favorite
I am creating a Jenkins pipeline, I want certain stage to be triggered only when a particular log file's(log file is located in the server node where all the stages are going to run) last modified date is updated after the initiation of pipeline job, I understand we need to use "When" condition but not really sure how to implement it.
Tried referring some of the pipeline related portals but could not able to find an answer
Can some please help me through this?
Thanks in advance!
jenkins jenkins-pipeline
add a comment |
up vote
2
down vote
favorite
I am creating a Jenkins pipeline, I want certain stage to be triggered only when a particular log file's(log file is located in the server node where all the stages are going to run) last modified date is updated after the initiation of pipeline job, I understand we need to use "When" condition but not really sure how to implement it.
Tried referring some of the pipeline related portals but could not able to find an answer
Can some please help me through this?
Thanks in advance!
jenkins jenkins-pipeline
any success using my answer?
– Michael
Nov 19 at 14:03
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am creating a Jenkins pipeline, I want certain stage to be triggered only when a particular log file's(log file is located in the server node where all the stages are going to run) last modified date is updated after the initiation of pipeline job, I understand we need to use "When" condition but not really sure how to implement it.
Tried referring some of the pipeline related portals but could not able to find an answer
Can some please help me through this?
Thanks in advance!
jenkins jenkins-pipeline
I am creating a Jenkins pipeline, I want certain stage to be triggered only when a particular log file's(log file is located in the server node where all the stages are going to run) last modified date is updated after the initiation of pipeline job, I understand we need to use "When" condition but not really sure how to implement it.
Tried referring some of the pipeline related portals but could not able to find an answer
Can some please help me through this?
Thanks in advance!
jenkins jenkins-pipeline
jenkins jenkins-pipeline
edited Nov 18 at 0:01
Michael
1,0581712
1,0581712
asked Nov 17 at 14:54
bharaner
112
112
any success using my answer?
– Michael
Nov 19 at 14:03
add a comment |
any success using my answer?
– Michael
Nov 19 at 14:03
any success using my answer?
– Michael
Nov 19 at 14:03
any success using my answer?
– Michael
Nov 19 at 14:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
To get data about file is quite tricky in a Jenkins pipeline when using the Groovy sandbox since you're not allowed to do new File(...).lastModified
. However there is the findFiles
step, which basically returns a list of wrapped File
objects with a getter for last modified time in millis, so we can use findFiles(glob: "...")[0].lastModified
.
The returned array may be empty, so we should rather check on that (see full example below).
The current build start time in millis is accessible via currentBuild.currentBuild.startTimeInMillis
.
Now that we git both, we can use them in an expression
:
pipeline {
agent any
stages {
stage("create file") {
steps {
touch "testfile.log"
}
}
stage("when file") {
when {
expression {
def files = findFiles(glob: "testfile.log")
files && files[0].lastModified < currentBuild.startTimeInMillis
}
}
steps {
echo "i ran"
}
}
}
}
I'm not 100% sure this happens with the above syntax, but I experienced that with similar syntax (only difference is I usedws
blocks) caused Jenkins to create tem workspaces per stage(project-name@1
,project-name@2
). Might worth keep in mind if things can't be found.
– Praveen P
Nov 17 at 18:27
1
@PraveenP I tried it with my local master only Jenkins and it worked just fine. You might be right, though. You are definitely right about the file might not be found. I updated the code.
– Michael
Nov 19 at 5:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
To get data about file is quite tricky in a Jenkins pipeline when using the Groovy sandbox since you're not allowed to do new File(...).lastModified
. However there is the findFiles
step, which basically returns a list of wrapped File
objects with a getter for last modified time in millis, so we can use findFiles(glob: "...")[0].lastModified
.
The returned array may be empty, so we should rather check on that (see full example below).
The current build start time in millis is accessible via currentBuild.currentBuild.startTimeInMillis
.
Now that we git both, we can use them in an expression
:
pipeline {
agent any
stages {
stage("create file") {
steps {
touch "testfile.log"
}
}
stage("when file") {
when {
expression {
def files = findFiles(glob: "testfile.log")
files && files[0].lastModified < currentBuild.startTimeInMillis
}
}
steps {
echo "i ran"
}
}
}
}
I'm not 100% sure this happens with the above syntax, but I experienced that with similar syntax (only difference is I usedws
blocks) caused Jenkins to create tem workspaces per stage(project-name@1
,project-name@2
). Might worth keep in mind if things can't be found.
– Praveen P
Nov 17 at 18:27
1
@PraveenP I tried it with my local master only Jenkins and it worked just fine. You might be right, though. You are definitely right about the file might not be found. I updated the code.
– Michael
Nov 19 at 5:35
add a comment |
up vote
0
down vote
To get data about file is quite tricky in a Jenkins pipeline when using the Groovy sandbox since you're not allowed to do new File(...).lastModified
. However there is the findFiles
step, which basically returns a list of wrapped File
objects with a getter for last modified time in millis, so we can use findFiles(glob: "...")[0].lastModified
.
The returned array may be empty, so we should rather check on that (see full example below).
The current build start time in millis is accessible via currentBuild.currentBuild.startTimeInMillis
.
Now that we git both, we can use them in an expression
:
pipeline {
agent any
stages {
stage("create file") {
steps {
touch "testfile.log"
}
}
stage("when file") {
when {
expression {
def files = findFiles(glob: "testfile.log")
files && files[0].lastModified < currentBuild.startTimeInMillis
}
}
steps {
echo "i ran"
}
}
}
}
I'm not 100% sure this happens with the above syntax, but I experienced that with similar syntax (only difference is I usedws
blocks) caused Jenkins to create tem workspaces per stage(project-name@1
,project-name@2
). Might worth keep in mind if things can't be found.
– Praveen P
Nov 17 at 18:27
1
@PraveenP I tried it with my local master only Jenkins and it worked just fine. You might be right, though. You are definitely right about the file might not be found. I updated the code.
– Michael
Nov 19 at 5:35
add a comment |
up vote
0
down vote
up vote
0
down vote
To get data about file is quite tricky in a Jenkins pipeline when using the Groovy sandbox since you're not allowed to do new File(...).lastModified
. However there is the findFiles
step, which basically returns a list of wrapped File
objects with a getter for last modified time in millis, so we can use findFiles(glob: "...")[0].lastModified
.
The returned array may be empty, so we should rather check on that (see full example below).
The current build start time in millis is accessible via currentBuild.currentBuild.startTimeInMillis
.
Now that we git both, we can use them in an expression
:
pipeline {
agent any
stages {
stage("create file") {
steps {
touch "testfile.log"
}
}
stage("when file") {
when {
expression {
def files = findFiles(glob: "testfile.log")
files && files[0].lastModified < currentBuild.startTimeInMillis
}
}
steps {
echo "i ran"
}
}
}
}
To get data about file is quite tricky in a Jenkins pipeline when using the Groovy sandbox since you're not allowed to do new File(...).lastModified
. However there is the findFiles
step, which basically returns a list of wrapped File
objects with a getter for last modified time in millis, so we can use findFiles(glob: "...")[0].lastModified
.
The returned array may be empty, so we should rather check on that (see full example below).
The current build start time in millis is accessible via currentBuild.currentBuild.startTimeInMillis
.
Now that we git both, we can use them in an expression
:
pipeline {
agent any
stages {
stage("create file") {
steps {
touch "testfile.log"
}
}
stage("when file") {
when {
expression {
def files = findFiles(glob: "testfile.log")
files && files[0].lastModified < currentBuild.startTimeInMillis
}
}
steps {
echo "i ran"
}
}
}
}
edited Nov 19 at 7:23
answered Nov 17 at 17:26
Michael
1,0581712
1,0581712
I'm not 100% sure this happens with the above syntax, but I experienced that with similar syntax (only difference is I usedws
blocks) caused Jenkins to create tem workspaces per stage(project-name@1
,project-name@2
). Might worth keep in mind if things can't be found.
– Praveen P
Nov 17 at 18:27
1
@PraveenP I tried it with my local master only Jenkins and it worked just fine. You might be right, though. You are definitely right about the file might not be found. I updated the code.
– Michael
Nov 19 at 5:35
add a comment |
I'm not 100% sure this happens with the above syntax, but I experienced that with similar syntax (only difference is I usedws
blocks) caused Jenkins to create tem workspaces per stage(project-name@1
,project-name@2
). Might worth keep in mind if things can't be found.
– Praveen P
Nov 17 at 18:27
1
@PraveenP I tried it with my local master only Jenkins and it worked just fine. You might be right, though. You are definitely right about the file might not be found. I updated the code.
– Michael
Nov 19 at 5:35
I'm not 100% sure this happens with the above syntax, but I experienced that with similar syntax (only difference is I used
ws
blocks) caused Jenkins to create tem workspaces per stage(project-name@1
,project-name@2
). Might worth keep in mind if things can't be found.– Praveen P
Nov 17 at 18:27
I'm not 100% sure this happens with the above syntax, but I experienced that with similar syntax (only difference is I used
ws
blocks) caused Jenkins to create tem workspaces per stage(project-name@1
,project-name@2
). Might worth keep in mind if things can't be found.– Praveen P
Nov 17 at 18:27
1
1
@PraveenP I tried it with my local master only Jenkins and it worked just fine. You might be right, though. You are definitely right about the file might not be found. I updated the code.
– Michael
Nov 19 at 5:35
@PraveenP I tried it with my local master only Jenkins and it worked just fine. You might be right, though. You are definitely right about the file might not be found. I updated the code.
– Michael
Nov 19 at 5:35
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352355%2fhow-do-i-use-when-condition-on-a-files-last-modified-time-in-jenkins-pipeline%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
any success using my answer?
– Michael
Nov 19 at 14:03