You can use that pattern, it’s arguably better than what I use.
However my builds can look like, the environment really depicts how you can
execute things. I have a node labelled “default"
def runAndReturn(script){
toReturn = sh(script:script, returnStdout: true)
toReturn = toReturn[0..-2]
println toReturn
toReturn
}
node("default"){
stage('shhhh'){
x = runAndReturn("echo 134")
println(x)
}
}
This outputs:
[Pipeline] stage
[Pipeline] { (shhhh)
[Pipeline] sh
[test] Running shell script
+ echo 134
[Pipeline] echo
134
[Pipeline] echo
134
[Pipeline] }
[Pipeline] // stage
> On May 23, 2018, at 12:41 PM, JB <[email protected]> wrote:
>
> OK,
>
> because I'm a beginner, wich pattern are you using?
>
> On Wednesday, May 23, 2018 at 1:27:28 PM UTC-4, Edward Bond wrote:
> JB,
>
> Sorry, you are using the declarative syntax. I run my builds with a
> different pattern.
>
> Something like this might work for you,
>
>
>
> def runAndReturn(script){
> toReturn = sh(script:script, returnStdout: true)
> toReturn = toReturn[0..-2]
> println toReturn
> toReturn
> }
>
>
> pipeline {
> agent any
>
> stages {
> stage('Build project') {
> steps {
> git credentialsId: '*****', url: 'https://******'
> sh 'dotnet build
> WebApplication/WebApplication1/WebApplication1.csproj'
> }
> }
>
> stage('Build image') {
> steps {
> script {
> tagBefore = runAndReturn("git describe --candidate=1
> --tags")
> tag = runAndReturn("echo ${tagBefore} | cut -d\'-\' -f 1")
> webImageName =
> "${env.ACR_LOGINSERVER}/my-project-1499882073260/test:${tag}"
> sh "sudo docker build -t ${webImageName} -f
> WebApplication/WebApplication1/Dockerfile WebApplication/."
> }
>
> }
> }
> }
> }
>
>
>
>> On May 23, 2018, at 12:20 PM, JB <[email protected] <javascript:>> wrote:
>>
>> Hello Edward,
>>
>> This is my script at all
>> I think I did a mistake...
>>
>>
>> pipeline {
>> agent any
>>
>> stages {
>> stage('Build project') {
>> steps {
>> git credentialsId: '*****', url: 'https://******'
>> sh 'dotnet build
>> WebApplication/WebApplication1/WebApplication1.csproj'
>> }
>> }
>>
>> stage('Build image') {
>> steps {
>> tagBefore = runAndReturn("git describe --candidate=1 --tags")
>> tag = runAndReturn("echo ${tagBefore} | cut -d\'-\' -f 1")
>> webImageName =
>> "${env.ACR_LOGINSERVER}/my-project-1499882073260/test:${tag}"
>> sh "sudo docker build -t ${webImageName} -f
>> WebApplication/WebApplication1/Dockerfile WebApplication/."
>> }
>> }
>> }
>> }
>>
>> def runAndReturn(script){
>> toReturn = sh(script:script, returnStdout: true)
>> toReturn = toReturn[0..-2]
>> println toReturn
>> toReturn
>> }
>>
>> Thanks to all people!
>>
>> Started by user demo
>> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
>> failed:
>> WorkflowScript: 14: Expected a step @ line 14, column 17.
>> tagBefore = runAndReturn("git describe --candidate=1
>> --tags")
>> ^
>> WorkflowScript: 15: Expected a step @ line 15, column 17.
>> tag = runAndReturn("echo ${tagBefore} | cut -d\'-\' -f 1")
>> ^
>> WorkflowScript: 16: Expected a step @ line 16, column 17.
>> webImageName =
>> "${env.ACR_LOGINSERVER}/my-project-1499882073260/test:${tag}"
>> ^
>> 3 errors
>> at
>> org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
>> at
>> org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
>> (...)
>> at hudson.model.ResourceController.execute(ResourceController.java:97)
>> at hudson.model.Executor.run(Executor.java:405)
>> Finished: FAILURE
>>
>>
>>
>> On Wednesday, May 23, 2018 at 1:11:15 PM UTC-4, Edward Bond wrote:
>> Don’t use sing quotes ‘, make sure you use “, that way the string can be
>> interpolated.
>>
>>
>> I also use a global helper that grabs the output of `sh` functions.
>>
>> def runAndReturn(script){
>> toReturn = sh(script:script, returnStdout: true)
>> toReturn = toReturn[0..-2]
>> println toReturn
>> toReturn
>> }
>>
>> stage('Build image') {
>> steps {
>> tagBefore = runAndReturn("git describe --candidate=1 --tags")
>> tag = runAndReturn("echo ${tagBefore} | cut -d\'-\' -f 1")
>> webImageName =
>> "${env.ACR_LOGINSERVER}/my-project-1499882073260/test:${tag}"
>> sh "sudo docker build -t ${webImageName} -f
>> WebApplication/WebApplication1/Dockerfile WebApplication/."
>> }
>> }
>>
>> or something like:
>>
>>
>> stage('Build image') {
>> steps {
>> sh """
>> export TAG=$(git describe --candidate=1 --tags)'
>> export TAG=$(echo $TAG | cut -d\'-\' -f 1)'
>> export
>> WEB_IMAGE_NAME=$ACR_LOGINSERVER/my-project-1499882073260/test:$TAG
>> sudo docker build -t $WEB_IMAGE_NAME -f
>> WebApplication/WebApplication1/Dockerfile WebApplication/.
>> """
>> }
>> }
>>
>> First example would allow you to go through and get access to strings to
>> have more control over the strings. Second is an example of how you could
>> use it to stay in bash.
>>
>>
>>> On May 23, 2018, at 7:40 AM, JB <[email protected] <>> wrote:
>>>
>>> Hello All,
>>>
>>> Anyone knows how to edit a variable from shell and recover the result
>>> across each next shells.
>>>
>>> I'm trying to get the git tag version and to push the value into an env
>>> variable.
>>> In next, I'd like to re use the value into the next cmd shell.
>>>
>>> It doesn't work!
>>>
>>> Anyone has an idea? I worked more than 2 days to trying to fix it.
>>>
>>> stage('Build image') {
>>> steps {
>>> sh 'TAG=$(git describe --candidate=1 --tags)'
>>> sh 'TAG=$(echo $TAG | cut -d\'-\' -f 1)'
>>> sh 'WEB_IMAGE_NAME=' + env['ACR_LOGINSERVER'] +
>>> '/my-project-1499882073260/test:' + env['TAG']
>>>
>>> sh 'sudo docker build -t ${WEB_IMAGE_NAME} -f
>>> WebApplication/WebApplication1/Dockerfile WebApplication/.'
>>> }
>>> }
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Jenkins Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to [email protected] <>.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/jenkinsci-users/8effa089-07c3-4918-afdd-858513ef13cc%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/jenkinsci-users/8effa089-07c3-4918-afdd-858513ef13cc%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>> For more options, visit https://groups.google.com/d/optout
>>> <https://groups.google.com/d/optout>.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Jenkins Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/jenkinsci-users/7b789802-5cac-4801-9d02-2f4cb732eefb%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/jenkinsci-users/7b789802-5cac-4801-9d02-2f4cb732eefb%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout
>> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-users/562025ac-3867-432b-a7fe-adb0d8901093%40googlegroups.com
>
> <https://groups.google.com/d/msgid/jenkinsci-users/562025ac-3867-432b-a7fe-adb0d8901093%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/3B5692C4-CBAC-4999-829D-E97CBF9017F5%40gmail.com.
For more options, visit https://groups.google.com/d/optout.