0

Is it possible to have a java source code repository in Git and have a Dockerfile in the root directory of the Git repository, so that after building the maven artifacts openshift will user the provided Docker file to create docker image ?

I got till building the Maven artifacts but after that it being handled by S2I it seems.

Update I'm using openshift Online, free version.

Code is multi-module maven project.

Properties files also there in target file.

1
  • It will help if you can share more information about your build config and pipeline. Commented Apr 14, 2020 at 13:23

2 Answers 2

1

This can absolutely be done by using chained builds.

Specifically, you would perform an S2I build from your GitHub repo

apiVersion: v1
kind: BuildConfig
metadata:
  name: artifact-build
spec:
  output:
    to:
      kind: ImageStreamTag
      name: artifact-image:latest
  source:
    git:
      uri: https://github.com/openshift/openshift-jee-sample.git
  strategy:
    sourceStrategy:
      from:
        kind: ImageStreamTag
        name: wildfly:10.1
        namespace: openshift

And then you would create a chained build that uses a Docker build

apiVersion: v1
kind: BuildConfig
metadata:
  name: image-build
spec:
  output:
    to:
      kind: ImageStreamTag
      name: image-build:latest
  source:
    dockerfile: |-
      FROM jee-runtime:latest
      COPY ROOT.war /deployments/ROOT.war
    images:
    - from: 
        kind: ImageStreamTag
        name: artifact-image:latest
      paths: 
      - sourcePath: /wildfly/standalone/deployments/ROOT.war
        destinationDir: "."
  strategy:
    dockerStrategy:
      from: 
        kind: ImageStreamTag
        name: jee-runtime:latest
  triggers:
  - imageChange: {}
    type: ImageChange
0

I found the root cause, also there were other things to consider here. Noting down bellow to make it clear.

  • In multi-module project target directory is not inside root directory, so OpenShift S2I doesn't see the jar file, it was the initial issue.

    Fix was to add the environment variable in the build config with target folder enter image description here

  • However if you need any properties files which are in the target directory to be copied along with the jar file you have to copy that in the pom file itself to /deployment directory. Add openshift profile as following and do the copying.

    <profiles>
    <profile>
        <!-- When built in OpenShift the 'openshift' profile will be used when 
            invoking mvn. -->
        <!-- Use this profile for any OpenShift specific customization your app 
            will need. -->
        <id>openshift</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
            <resources>
                <resource>
                    <filtering>true</filtering><!-- if it is neccessary -->
                    <directory>${project.basedir}/src/main/resources</directory><!-- from -->
                    <targetPath>/deployments</targetPath><!-- to adding ARTIFACT_DIR on openshift only finds the jar file but not the prop, so copy it manually like this-->
                    <includes><!-- what -->
                        <include>*.properties</include>
                        <include>Dockerfile</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
    

  • And final point to take is that as this is on Openshift free plan, Docker strategy is not allowed. It will give following message.

    Builds with docker strategy are prohibited on this cluster However if docker based way is required still it can be done with oc command line tool.

But concept of chained builds mentioned by @Will Gordon is really important. As per my case normal build is like 250MB, as it will have m2 also. But with chained build which copy artifact created from initial build to secondary deployable build I was able to reduce image size to 200MB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.