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 15, 2019 in Jenkins by anonymous
• 380 points
45,956 views

1 answer to this question.

+4 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
• 59,230 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 Jul 9, 2020 in Jenkins by MD
• 95,440 points
2,933 views
+1 vote
3 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
• 59,230 points
76,083 views
0 votes
1 answer
+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,570 points
35,132 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,498 views
+2 votes
1 answer
+5 votes
3 answers

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

You can run the following code, its ...READ MORE

answered Jul 5, 2018 in Jenkins by Sophie may
• 10,610 points
117,435 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
22,074 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP