How to extract a substring of a string in a declarative pipeline code?

+1 vote
I have a file with the name as <JIRA ID>_<Filename>(For ex: ad-123_file1.txt) that has been checked in in the same location as that of my jenkinsfile in bitbucket. As part of the logic that I write in the jenkinsfile which is written in declarative pipeline syntax, I would like to fetch the JIRA ID and store it in a variable for future use. Here, I want to use '_' as the delimiter. How can I write this logic in the Jenkinsfile?
Aug 14 in Jenkins by anonymous
• 320 points
616 views

1 answer to this question.

+3 votes
Best answer

This would have been more easier using a groovy script (ie) Scripted pipeline. 

However there is a still a way where you could include it in your declarative pipeline syntax. 

You could probably add the "script" step in your Declarative pipeline code in the jenkins file.

The script step takes a block of scripted pipeline and executes that in the Declarative Pipeline.

Jenkinsfile (Declarative Pipeline)

pipeline {
    agent any
    stages {
        stage('-----') {
            steps {
                echo '---'
                      --------------
                script {
                    // Here write your groovy syntax that would extract the substring
                }
            }
        }
    }
}

You could try this syntax:

String stringParser(String inputString) {
    inputString.split("_")[0]
}

Try adding this snippet in the script section of your declarative jenkinsfile.

Hope this helps!

answered Aug 16 by Sirajul
• 38,820 points

selected Aug 16 by Abhilash
Thanks for your answer.

From the code snippet that you shared, I see that the inputString variable is the file according to my use case.

I am now interested to understand as to how I can assign this variable to the name of my file so that I'll eventually be able to use this to fetch the JIRA ID from the function. Can you please let me know how I can do this in the declarative pipeline?

Also, I would like the value returned by inputString.split("_")[0] be accessible throughout my Jenkins pipeline by another variable so that I can use it across the various stages. Can you please let me know as to how I can achieve this as well?

I am not so well versed with groovy still i tried rewriting the Script part.

script {
        def filename = "ad-123_file1.txt"    //filename will have the name of the file
        def JiraId = filename.split("_")     // JiraId will store the value that you could use at various stages  
        }

Give this a try!

Related Questions In Jenkins

0 votes
2 answers

How to auto build a job in jenkins if there is any change in code on Github repository?

To automatically run build on code commit, follow ...READ MORE

answered Jul 31 in Jenkins by Sirajul
• 38,820 points
283 views
+4 votes
7 answers

How to set the pipeline name and description for a Jenkinsfile?

You can rename pipeline jobs in jenkins ...READ MORE

answered Oct 11, 2018 in Jenkins by Hannah
• 16,630 points
8,402 views
0 votes
2 answers
0 votes
1 answer
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
216 views
0 votes
1 answer
0 votes
2 answers

How to trigger a Jenkins pipeline A in another Jenkins pipeline B?

If you are talking about executing another ...READ MORE

answered Jul 31 in Jenkins by Sirajul
• 38,820 points
6,603 views
0 votes
3 answers

How to get the Maven Version of a project in Jenkins?

Here is a snippet from our Jenkins ...READ MORE

answered Oct 4 in Jenkins by Petr Doležal
1,726 views