0

I wrote a Shell script to replace text in the Jenkinsfiles of a large number of repos. I had used this to add on a parameter to a single line of text. However, now I need to insert a line of text before and after an existing command in the Jenkinsfiles. I don't have too much shell experience and could use some help.

Here is the Jenkinsfile text before:

sh "chmod +x increment.sh"
def result = sh returnstdout:true, script: "./increment.sh '${Version}' '${ReleaseVersion}' '${GitRepoURL}' '${CutRelease}' '${Branch}' '${JiraID}'"
//echo "$result"    

I need to add the following before the "def result" line:

sshagent(['gitssh']) {

and then add a closing curly bracket after the "def result" line:

}

I need the end result to be:

sh "chmod +x increment.sh"
sshagent(['gitssh']) {
    def result = sh returnstdout:true, script: "./increment.sh '${Version}' '${ReleaseVersion}' '${GitRepoURL}' '${CutRelease}' '${Branch}' '${JiraID}'"
}
//echo "$result"    

I actually don't care about keeping the commented out echo command if that makes it more difficult, but it is just to show what I have surrounding the "def result" line.

How can I accomplish this end result?

If it helps, I previously was adding new parameters at the end of the "def result" line with this code:

if [ -e Jenkinsfile ]
then
    sed -i -e "s/\${Branch}/\${Branch}\' \'\${JiraID}/g" Jenkinsfile
fi

Note: I am on a Mac.

Code so far:

file=repos_remaining.txt
while IFS="," read -r repoURL repoName; do
echo $repoURL
cd ..
echo $repoName
cd $(echo $repoName | tr -d '\r')
file=repos_remaining.txt    
if [ -e Jenkinsfile ]
then
    # sed -i -e $"s/def result/sshagent([\'gitssh\']) {\
    #   def result/g" Jenkinsfile
fi

# git add "Jenkinsfile"
# git commit -m "Added JiraID parameter to Jenkinsfile"
# git push origin master
done < "$file"

1 Answer 1

1

As with most cases where people want to automate the editing of a file, I suggest using ed:

ed -s Jenkinsfile <<'EOF'
/^def result/i
sshagent(['gitssh']) {
.
.+1a
}
.
w
EOF

The commands in the heredoc tell ed to move the cursor to the first line starting with def result, insert a line above it, append a line after it, and finally write the modified file back to disc.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Shawn, I was getting an unexpected end of file error with the line number at the end of my script, when I inserted your code inside my "then" clause. I've added my whole script to the question to provide clarity.
If I cannot get this to work, I can at least add it all in one line in the Jenkinsfile: sed -i -e "s/def result/sshagent([\'gitssh\']) {def result/g" Jenkinsfile; sed -i -e "s/\${JiraID}\'\"/\${Jira ID}\'\"}/g" Jenkinsfile;
@Coogie7 Bet you made the same mistake as in this question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.