0

I'm building several android apps in a docker image using gradle and a bash script. The script is triggered by jenkins, which runs the docker image. In the bash script I gather information about the successes of the builds. I want to pass that information to the groovy script of the jenkinsfile. I tried to create a txt file in the docker container, but the groovy script in the jenkinsfile can not find that file. This is the groovy script of my jenkinsfile:

script {
    try {
        sh script:'''
        #!/bin/bash
        ./jenkins.sh
        '''
    } catch(e){
        currentBuild.result = "FAILURE"
    } finally {
        String buildResults = null
        try {
            def pathToBuildResults="[...]/buildResults.txt"
            buildResults = readFile "${pathToBuildResults}"
        } catch(e) {
            buildResults = "error receiving build results. Error: " + e.toString()
        } 
    }
}

In my jenkins.sh bash script I do the following:

[...]
buildResults+=" $appName: Build Failed!" //this is done for several apps
echo "$buildResults" | cat > $pathToBuildResults //this works I checked, if the file is created
[...]

The file is created, but groovy cannot find it. I think the reason is, that the jenkins script does not run inside the docker container.

How can I access the string buildResults of the bash script in my groovy jenkins script?

3
  • are you getting the jenkinsfile from a git repo? or is the script hardcoded in the job configuration? Commented Oct 13, 2021 at 21:46
  • It's coming from a git repo Commented Oct 14, 2021 at 7:05
  • then you might have to checkout the sh and the text file together with the jenkinsfile. Keep them together in the git repo that is Commented Oct 14, 2021 at 14:30

1 Answer 1

1

One option that you have in order to avoid the need to read the results file is to modify your jenkins.sh script to print the results to the output instead of writing them to a file and then use the sh step to capture that output and use it instead of the file.
Something like:

script {
    try {
        String buildResults = sh returnStdout: true, script:'''
           #!/bin/bash
           ./jenkins.sh
           '''
        // You now have the output of jenkins.sh inside the buildResults parameter
    } catch(e){
        currentBuild.result = "FAILURE"
    }
}

This way you are avoiding the need to handle the output files and directly get the results you need, which you can then parse and use however you need.

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

1 Comment

It worked, thank you

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.