Auto Build for multiple Docker containers

We have some projects which have multiple Dockerfiles — for example, a database container to be preloaded with test data and the application which will use that database. Obviously we can either build those manually using CI steps or split them into separate projects but it feels like there should be an easy way to say “Run Auto Build in directory1 and directory2”.

The documentation doesn’t appear to cover this and some of the past attempts I’ve seen were relatively old and involved enough copying from the Auto Build template that it’d be easier to just do the build manually.

Hi, as far as I can see, the Documentation Stages of Auto DevOps | GitLab doesn’t cover your scenario. It looks like Auto Build will not pick up multiple Dockerfiles from Subdirectories.

If a project’s repository contains a Dockerfile at its root, Auto Build uses docker build to create a Docker image.

I guess, there is no way to do Auto Build for such a scenario - I personally would recommend implementing the Steps manually.

I ended up doing something like this, where I have a main app container that Auto-build picks up, and then nginx and varnish containers built by their own jobs. I made an abstract job that inherits from Auto-build and copies from its script. I added a variable there called CONTAINER_NAME so multiple containers would get pushed with different names. Then I extended that into two jobs with different variables. The result is three different container names in the same registry.

include:
  - template: Auto-DevOps.gitlab-ci.yml

.build container:
  stage: build
  extends: build
  variables:
#    DOCKERFILE_PATH: nginx/Dockerfile
  script:
    - |
      if [[ -z "$CI_COMMIT_TAG" ]]; then
        export CI_APPLICATION_REPOSITORY=${CI_APPLICATION_REPOSITORY:-$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/$CONTAINER_NAME}
        export CI_APPLICATION_TAG=${CI_APPLICATION_TAG:-$CI_COMMIT_SHA}
      else
        export CI_APPLICATION_REPOSITORY=${CI_APPLICATION_REPOSITORY:-$CI_REGISTRY_IMAGE/$CONTAINER_NAME}
        export CI_APPLICATION_TAG=${CI_APPLICATION_TAG:-$CI_COMMIT_TAG}
      fi
    - /build/build.sh

build nginx:
  extends: .build container
  variables:
    DOCKERFILE_PATH: nginx/Dockerfile
    CONTAINER_NAME: nginx

build varnish:
  extends: .build container
  variables:
    DOCKERFILE_PATH: varnish/Dockerfile
    CONTAINER_NAME: varnish
4 Likes

Can you share what is in /build/build.sh?

That line is coming from lib/gitlab/ci/templates/Jobs/Build.gitlab-ci.yml · master · GitLab.org / GitLab · GitLab but I can not seem to find the file it is referencing right now. I assume it’s in one of the Gitlab repos.

I tried using your .gitlab-ci.yml but I have a little problem, instead of only two builds, it makes a third build with no argument. Is there a way to have only the two build nginx and varnish without the default one ?

In my case I wanted the three jobs, where the default one built the application container.

I think you could disable the default build job by overriding it in your .gitlab-ci.yml file and setting the rules to never run.

this looks like addressing not only build but other steps as well