How to setup GitHub Docker CI to run for all submodules

Viewed 356

I am a beginner at Docker. I have a Maven multi module project, each submodule is a SpringBoot microservice (so each submodule is executable). I have added Dockerfiles in each of the submodule and I want to run the Docker CI but it fails saying:

unable to prepare context: path "/discovery-service" not found

Discovery service is a maven submodule name. My docker.yaml is

name: Docker Image CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Build the Docker image
      run: |
        docker build /discovery-service --file Dockerfile --tag my-image-name:$(date +%s)
        docker build /config-server --file Dockerfile --tag my-image-name:$(date +%s)
        docker build /gateway-service --file Dockerfile --tag my-image-name:$(date +%s)
        docker build /business-owner-service --file Dockerfile --tag my-image-name:$(date +%s)
© 2020 GitHub, Inc.

How can I get the Dockerfiles to get created? I would like to use Github to deploy directly to Google Cloud Kubernetes

2 Answers

I got it to work and the following is my workflow:

steps:
    - uses: actions/checkout@v2
    - name: Build the Docker image
      run: |
        docker build comand-API/discovery-service --file comand-API/discovery-service/Dockerfile --tag my-image-name:$(date +%s)
        docker build comand-API/config-server --file comand-API/config-server/Dockerfile --tag my-image-name:$(date +%s)
        docker build comand-API/gateway-service --file comand-API/gateway-service/Dockerfile --tag my-image-name:$(date +%s)
        docker build comand-API/business-owner-service --file comand-API/business-owner-service/Dockerfile --tag my-image-name:$(date +%s)

Basically I just needed to lose the / on the folder target and update the file path

Disclaimer: This might be a little off-topic answer, but I bet it can help anyway and prodivde some useful information.

Solution: I rather suggest you to use a specific Maven plugin com.spotify:dockerfile-maven-pluginthat is able to create a Docker file and an image from it that might be used in the CI.

A breakdown:

  • goals: build and push goals build and pushes the Docker image to the local repository
  • repository: A repository name with the name of the image
  • dockerfile: A base Dockerfile (if used one from the root, otherwise navigate to the module directory)

Here is a sample:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.10</version>
    <executions>
        <execution>
            <id>default</id>
            <goals>
                <goal>build</goal>                                    
                <goal>push</goal>                                       
            </goals>
        </execution>
    </executions>
    <configuration>
        <repository>com.company/${project.artifactId}</repository>
        <tag>${project.version}</tag>
        <dockerfile>${project.parent.basedir}/Dockerfile</dockerfile>
        <contextDirectory>${project.parent.basedir}</contextDirectory>
        <pullNewerImage>false</pullNewerImage>
        <buildArgs>
            <BASE_DIRECTORY>${project.artifactId}</BASE_DIRECTORY>
        </buildArgs>
    </configuration>
</plugin>

This phases can be configured using Maven profiles. Thus this plugin might be optional for the CI usage only and skipped when you build on your machine.

Related