codeship

Is it possible to use variables in a codeship-steps.yml file?


We currently use Codeship Pro to push Docker images to a private registry on AWS, as well as to deploy those images to an ECS cluster.

However, the codeship-steps.yml file includes a hard-coded region name for which AWS region I'm pushing to. For example:

- name: push_production
  service: app
  type: push
  image_name: 123456789012.dkr.ecr.us-east-1.amazonaws.com/project/app-name
  image_tag: "{{.Timestamp}}"
  tag: master
  registry: https://123456789012.dkr.ecr.us-east-1.amazonaws.com
  dockercfg_service: aws_generator

I would like to be able to fairly easily switch this to deploy to a different AWS region. Thus the question:

Is it possible to use variables in a codeship-steps.yml file?

I know some of the properties can use a handful of built-in variables provided by Codeship (such as the {{.Timestamp}} value used for the image_tag property), but I don't know if, for example, values from an env_file can be used in the image_name, registry, and/or command properties of a step.

I'm imagining something like this...

codeship-steps.yml:

- name: push_production
  service: app
  type: push
  image_name: "123456789012.dkr.ecr.{{.AWS_REGION}}.amazonaws.com/project/app-name"
  image_tag: "{{.Timestamp}}"
  tag: master
  registry: "https://123456789012.dkr.ecr.{{.AWS_REGION}}.amazonaws.com"
  dockercfg_service: aws_generator

... but that results in an "error parsing image name during push step: invalid reference format" on the push step.

I've tried simply not specifying the registry in the image_name...

  image_name: project/app-name

... but I get a "Build Error: no basic auth credentials" on the push step. At this point, I'm running out of ideas.


Solution

  • Is it possible to use [environment] variables in a codeship-steps.yml file?

    While the image_tag can take advantage of Go templates, the same is not the case for image_name, registry, or anything else. This is a separate set of templating variables that are accessible only to the image_tag generation.

    As for environment variables in general (CI environment variables or those defined in the service configs), these values can be used in codeship-steps.yml on the command step when passed through a shell command. For example:

    - service: app
      command: echo The branch name is: $CI_BRANCH
    

    Results in:

    The branch name is: $CI_BRANCH
    

    - service: app
      command: /bin/sh -c 'echo The branch name is: $CI_BRANCH'
    

    Results in:

    The branch name is: master
    

    As for your 'no basic auth credentials' error message, it's possible that there's an issue with how you are retrieving the basic auth credentials for access to your image registry. If you are on a MacOS device, I would recommend that you review our documentation on how to generate Docker credentials.