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!










share|improve this question
























  • any success using my answer?
    – Michael
    Nov 19 at 14:03















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!










share|improve this question
























  • any success using my answer?
    – Michael
    Nov 19 at 14:03













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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"
}
}
}
}





share|improve this answer























  • 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




    @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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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"
}
}
}
}





share|improve this answer























  • 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




    @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

















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"
}
}
}
}





share|improve this answer























  • 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




    @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















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"
}
}
}
}





share|improve this answer














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"
}
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








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 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




    @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






  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga