saw303
saw303

Reputation: 9072

Can a CircleCI workflow create a Docker image in a first step and then test that Docker image in a second step?

I'm new to CircleCI workflows and I would like to create a workflow with two steps.

  1. The first step (build job) will build and push a Docker image to a Docker registry with a certain tag. After successful termination of the first step it will trigger a second workflow step and handover the tag of the Docker image.
  2. The seconds step will then download the image by handed over by step 1 (e.g. using an ENV variable). Something like
version: 2
    jobs:
      build:
        docker:
          - image: docker.repo.ch/image:${TAG}

Is there some mechanism to hand over a value as a paramter from one build job to another one in within a CircleCI workflow?

Upvotes: 1

Views: 436

Answers (1)

Efrat Levitan
Efrat Levitan

Reputation: 5612

Since every run step is a new shell, environment variables are not shared across steps. If you need an environment variable to be accessible in more than one step, export the value using BASH_ENV.

https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-step

Export a var to the next job:

workflows:
  version: 2
  pass_tag:
    jobs:
    - create_tag
    - use_tag:
        requires:
        - create_tag

executors:
  node:
    docker:
      - image: circleci/node:4.8.2

jobs:
  create_tag:
    executor: node
    steps:
    - run: echo "export MY_TAG=$CIRCLE_SHA1" >> $BASH_ENV
  use_tag:
    executor: node
    steps:
    - run: echo $MY_TAG

Push & Use docker images:

use CircleCI orbs (like a library) to build and push the image:

version: 2.1
orbs:
    aws-ecr: circleci/[email protected]

workflows:
  version: 2
  example:
   jobs:
      - aws-ecr/build-and-push-image:
          account-url: DEV_AWS_ECR_ACCOUNT_URL
          aws-access-key-id: DEV_ACCESS_KEY_ID
          aws-secret-access-key: DEV_SECRET_ACCESS_KEY
          create-repo: true
          repo: '${CIRCLE_PROJECT_REPONAME}'
          region: AWS_DEFAULT_REGION
          tag: '${CIRCLE_SHA1}'
      -  pull_image:
          requires:
              - aws-ecr/build-and-push-image

jobs:
    pull_image:
        *** pull and use your image here ***

Upvotes: 4

Related Questions