技術(tech)

Want to change build path for Orb(aws-ecr) of CircleCI

As noted above, I was using CircleCI’s Orb (aws-ecr) and was putting the build materials in a different directory, not the root directory. So I wanted to change the path specification for the build materials.

I wanted to pass the build-path to Orb, but I got stuck, so I put it in the article.

 

Assumption

 

  • circleci 2.1
  • circleci/aws-ecr@8.2.1

 

What we want to do

 

I would like to change the path specification of the build material to the specified directory instead of the root directory during the Orb (aws-ecr) process (build-and-push-image) in CircleCI.

What is Orb?

Orbs are reusable packages of parameterizable configuration that can be used in any project. They are made up of reusable configuration elements, for example, jobscommands, and executors. Orbs are available for many languages, platforms, services, and tools. Visit the Orbs Registry to search for orbs to help simplify your configuration.

https://circleci.com/docs/ja/orb-intro/

You can follow the AWS ECR documentation and write your own commands.
If you don’t care about requirements, you can easily implement Build/Push process by simply following CircleCI’s Orb.

For example, you don’t have to write docker build or docker push commands, you can just pass arguments and execute the method.

version: 2.1

orbs:
  aws-ecr: circleci/aws-ecr@8.2.1

workflows:
  build-and-deploy:
    jobs:
    - aws-ecr/build-and-push-image:
        context:
          - CIRCLECI_SAMPLE_CONTEXT
        repo: "${AWS_RESOURCE_NAME_PREFIX}"
        tag: "${CIRCLE_SHA1}"
        dockerfile: "Dockerfile"

The case at issue here

This is a pattern where Build materials are not located in the root directory, but in a subdirectory.

~/D/l/c/sample ❯❯❯ tree
.
├── hogehoge.txt
└── side-app
    ├── Dockerfile
    ├── node_modules
    └── yarn.lock

 

In this case, you need to pass the path of the Dockerfile and the path of the Build material as parameters.

Looking at the aws-ecr documentation, it seems that the following parameters can be passed.

  • path: Path to the directory containing your Dockerfile and build context.
  • build-path: Path to the directory containing your build context.
version: 2.1

orbs:
  aws-ecr: circleci/aws-ecr@8.2.1

workflows:
  build-and-deploy:
    jobs:
    - aws-ecr/build-and-push-image:
        context:
          - CIRCLECI_SAMPLE_CONTEXT
        repo: "${AWS_RESOURCE_NAME_PREFIX}"
        tag: "${CIRCLE_SHA1}"
        dockerfile: "Dockerfile"
        path: "./side-app"
        build-path: "./side-app"

 

Syntax check…

~/D/l/c/sample ❯❯❯ circleci config validate
Error: Error calling workflow: 'build-and-deploy'
Error calling job: 'aws-ecr/build-and-push-image'
Unexpected argument(s): build-path

Oh, I can’t pass build-path…?

Upon closer inspection of the documentation.
Jobs > build-and-push-image has no build-path parameter.
Commands > build-and-push-image has a build-path parameter.

In other words, when calling as a job from workflow, build-path cannot be specified.
So, when calling from workflow as a job, build-path cannot be specified, and the job must be called from job as a command.

Solution

Invoke Orb’s build-and-push-image command from job.

version: 2.1

orbs:
  aws-ecr: circleci/aws-ecr@8.2.1

jobs:
  aws-build-and-push:
    docker:
      - image: cimg/base:current
    steps:
      - setup_remote_docker:
          version: 20.10.2
          docker_layer_caching: true
      - aws-ecr/build-and-push-image:
          repo: "${AWS_RESOURCE_NAME_PREFIX}"
          tag: "${CIRCLE_SHA1}"
          dockerfile: "Dockerfile"
          path: "./side-app"
          build-path: "./side-app"

workflows:
  build-and-deploy:
    jobs:
    - aws-build-and-push:
        context:
          - CIRCLECI_SAMPLE_CONTEXT

 

So this is OK.

It took me half a day to figure this out.

Conclusion

 

The ability to pass build-path has only recently been available (since v8.2.0).

The release notes state.

PRはこちら:https://github.com/CircleCI-Public/aws-ecr-orb/pull/251

This PR adds the build-path parameter to the docker-build-and-push-image command and job along with the build-image command. This enables users to specify the path of the build directory in the case that it differs from the path to the Dockerfile.

 

>This PR adds the build-path parameter to the docker-build-and-push-image command and job

Has it been added to Job?
It doesn’t seem to be working…?
I’ll have to create Issue.

Thank you for reading this far.