Use shell variables in global hook

Hello, developers.

I need a help. I used a variable in my pipeline script like this.

# this shell is pipeline shell script

export container_id=`docker run -d MY_CONTAINER`

then regardless of the success of result, I want to remove my container using container_id, like this.

# this shell is global exit hook

docker rm ${docker stop $container_id}

but it do not work. What can I do for solve this problem?

Hi @renynim,

Yeah, this is a bit of a gotcha; the command itself can’t export environment variables to other parts of the lifecycle, as it’s possible for the script to not be a shell script.

A workaround would be to move the docker run portion to an environment or pre-command hook, and from there it can submit those environment variables for later use. Another option, depending on what you’re trying to accomplish with Docker, would be to use our Docker plugin, which handles much of this lifecycle stuff for you.

Hope that helps!