0

Currently it creates a network name "denpal_default" and it does give this message:

[1BRemoving network denpal_default
Network denpal_default not found.
Network test-network is external, skipping

I haven't tested it yet, but I assume if it makes the denpal_default network and deletes it, it cannot run multiple builds at the same time.

I was thinking about a solution that would create a random COMPOSE_PROJECT_NAME="denpal-randomnumber" and build based on that.

But how do I use a variable set in the "Docker build"-stage in the "Verification"-stage later on?

stage('Docker Build') {
  steps {
    sh '''
    docker-compose config -q
    docker network prune -f && docker network inspect test-network >/dev/null || docker network create test-network
    COMPOSE_PROJECT_NAME=denpal docker-compose down
    COMPOSE_PROJECT_NAME=denpal docker-compose up -d --build "$@"
    '''
  }
}
stage('Verification') {
  steps {
    sh '''
    docker-compose exec -T cli curl http://nginx:8080 -v
    COMPOSE_PROJECT_NAME=denpal docker-compose down
    '''
  }
}
Dennis
  • 2,866
  • 7
  • 32
  • 49
  • https://stackoverflow.com/questions/44099851/how-do-i-pass-variables-between-stages-in-a-declarative-jenkins-pipeline -- This seems to be in similar to youe requirement – error404 Apr 08 '19 at 09:28
  • You can assign a variable in the first stage and utilize it in the second if you want. What might be a better option though is using Docker agents since it seems like what you really want is to run your jobs inside containers. Basically I think your problem could be solved most cleanly by re-architecting. – Matthew Schuchard Apr 08 '19 at 15:55

1 Answers1

0

you can use variables in sh commands in pipeline as basically it's a string, and leverage groovy gstring ( http://groovy-lang.org/syntax.html )

example for scripted pipeline, for declarative use env vars

def random = UUID.randomUUID().toString()
sh '''
   echo "hello ${random}"
'''

two common pitfalls, you must use double quotes ( gstring, single quote is regular string ), and "stage" is scoped, so define you're var's as global or in the same stage.

kbry
  • 133
  • 1
  • 7