Exporting a variable from a shell script

I have a pipeline with two stages, the first one pushes a docker image and the second one is supposed to write that image tag in a Kubernetes manifest and apply it. How do I pass the image tag from one stage to another? My pipeline looks like this

image: docker:latest

variables:
DOCKER_DRIVER: overlay

services:

  • docker:dind

stages:

  • publish_image
  • deploy_staging

publish_image:
stage: publish_image
only:

  • master
    script:
  • apk add --update git python3 make
  • docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
  • make canopy
  • ./deployment/version.sh

deploy_staging:
image: google/cloud-sdk
stage: deploy_staging
only:

  • master
    script:
  • cat ./deployment/canopy.yaml
  • echo “$GOOGLE_KEY” > key.json
  • gcloud auth activate-service-account --key-file key.json
  • gcloud config set project myproject
  • gcloud container clusters get-credentials staging-cluster --zone europe-west1-b
  • kubectl apply -f ./deployments/canopy.yaml
  • kubectl get po

The shell script computes a tag and pushes an image to gitlab docker registry

Apparently it’s not possible to share variables between jobs, but the top comment on this issue has an interesting workaround:

Basically it just exports the variable to a file/artifact, which is then copied back across to all the subsequent jobs in the pipeline. Each stage can then import the variable/variables from that file as needed.

Hope this works for you!

That helps. Thanks!

1 Like