Jfrog Build a Docker image with a private base image from Artifactory

453 Views Asked by At

I am following the Jfrog Pipeline Example: Docker Build and Push from https://www.jfrog.com/confluence/display/JFROG/Pipeline+Example%3A+Docker+Build+and+Push

I would like to build a docker image based on an existing docker image(caching) from Artifactory.
I have checked the following configuration in the documentation: https://www.jfrog.com/confluence/display/JFROG/DockerBuild

To build a Docker image that relies on a private base image:
Define the base image as an Image resource, with autoPull set to true.
Specify the Image resource as one of the inputResources of the DockerBuild step.

Build a Docker image with a private base image
This example builds a Docker image that relies on a private base image stored in an 
Artifactory Docker repository.

resources:
# Application source repository
- name: gosvc_app
  type: GitRepo
  configuration:
    gitProvider: myGithub
    path: myuser/myrepo                   # replace with your repository name
    branches:
      include: master

 # Docker image in an Artifactory repository
 - name: base_image
  type: Image
  configuration:
    registry: myArtifactory
    sourceRepository: docker-local        # replace with your repository name
    imageName: docker.artprod.mycompany.com/baseimage
    imageTag: latest
    autoPull: true
pipelines:
  - name: demo_pipeline
  steps:
    - name: bld_image
      type: DockerBuild
      configuration:
        dockerFileLocation: .
        dockerFileName: Dockerfile
        dockerImageName: docker.artprod.mycompany.com/gosvc       
        dockerImageTag: ${run_number}
        inputResources:
          - name: gosvc_app
          - name: base_image
        integrations:
          - name: MyArtifactory

Running this pipeline throws the following error:
"pipeline demo_pipeline has cyclic connection involving step bld_image. A pipeline can not have cyclic connections.

If I configure DockerBuild with only the Image resource i am getting the following error.
Running this pipeline throws the following error:
"DockerBuild step bld_image must have exactly one input resource of type GitRepo"

So in one part of the documentation you can use 2 resources under 'inputResources' and while using this configuration throws the above error.
And in other part of the documentation it uses only the Image resource which throws the error of "...exactly one input resource of type GitRepo".

So how can i achieve building a docker image base on a docker image(caching) stored in Artifactory?

2

There are 2 best solutions below

0
On BEST ANSWER

Solution for using a remote cache docker image with buildx in the Artifactory:

      steps:
        - name: {{ .Values.DockerBuild.name | default "DockerBuild" }}
          type: DockerBuild
          execution:
            onStart:
              - docker buildx create --name jfrog-builder --use 
              - docker buildx install    
              - jf c add example --url="$int_{{ .Values.artifactoryIntegration }}_url" --user="$int_{{ .Values.artifactoryIntegration }}_user" --password="$int_{{ .Values.artifactoryIntegration }}_password"
              - echo $int_{{ .Values.artifactoryIntegration }}_apikey | docker login --username $int_{{ .Values.artifactoryIntegration }}_user --password-stdin {{ .Values.artifactoryUrl }}       
          configuration:
            dockerFileLocation: {{ .Values.DockerBuild.dockerFileLocation | quote }}
            dockerFileName: {{ .Values.DockerBuild.dockerFileName }}
            dockerImageName: {{ .Values.DockerBuild.dockerImageName }}
            dockerImageTag: {{ .Values.DockerBuild.dockerImageTag }}
            dockerOptions: "--cache-from type=registry,ref={{ .Values.DockerBuild.dockerImageName }}:{{ .Values.DockerBuild.dockerImageCacheTag }} --cache-to type=registry,ref={{ .Values.DockerBuild.dockerImageName }}:{{ .Values.DockerBuild.dockerImageCacheTag }},mode=max --load"
          inputResources:
            - name: {{ .Values.GitRepo.name | default "GitRepoRes"  }} 
1
On

The has cyclic connection error message usually means that you're using a resource as an input as well as an output somewhere in the same pipeline (not necessarily the same step).

For example if your pipeline had base_image -> bld_image -> push_image -> base_image, then that would be considered cyclic.

Since the error log is specifically mentioning the bld_image, can you make sure that this image resource is not used as an output later in your workflow?

If you want an image to reference what was pushed in a later step, I'd recommend creating a second image type resource. One resource represents the base image, and the other can represent the new image that you've built and pushed

JFrog Pipelines doesn't allow cyclic connections like this to help users avoid accidentally creating an infinitely looping pipeline.