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, 2019 in Jenkins by anonymous
• 360 points
7,692 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, 2019 by Sirajul
• 54,260 points

selected Aug 16, 2019 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
1 answer

How to trigger a build on commit to branch in declarative pipeline

Hi@Madhura, You can use triggers keyword in your ...READ MORE

answered 5 days ago in Jenkins by MD
• 34,440 points
28 views
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, 2019 in Jenkins by Sirajul
• 54,260 points
6,532 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
• 18,060 points
15,756 views
0 votes
2 answers
+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,420 points
555 views
0 votes
1 answer
+2 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, 2019 in Jenkins by Sirajul
• 54,260 points
22,619 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, 2019 in Jenkins by Petr Doležal
6,324 views