ARTIFACTORY: Docker build + Gitlab integration with Artifactory and scan with JFrog Xray as part of a JFrog project

AuthorFullName__c
Swarnendu Kayal
articleNumber
000005368
FirstPublishedDate
2022-08-10T13:53:41Z
lastModifiedDate
2025-05-15

ARTIFACTORY: Docker build + Gitlab integration with Artifactory and scan with JFrog Xray as part of a JFrog project

The intention of this KB article is to kick start a docker build procedure using GitLab as a CI/CD tool and integrate it with Artifactory with a simple use case. We have used a SaaS version of the JFrog platform. However, the use case stands valid for the self-hosted JFrog Platform as well.

Introduction

GitLab can be used as a CI/CD tool along with many other features that it has. In order to integrate the GitLab with JFrog Artifactory, there is no such out of the box plugin available. However, we can integrate it using our very own CLI tool called JFrog CLI. We can build with multiple package types using JFrog CLI. More details on JFrog CLI are mentioned in this page.

In this KB article, we will discuss a docker build. The docker build has four steps – 

  1. Create a GitLab project and upload all the required files to the GitLab repository.
  2. Pull a base image from Docker Hub.
  3. Build a docker image using the image that we have pulled. The step # 2 and # 3 will be done as part of a Dockerfile.
  4. Do a scan of the image before even pushing to the Artifactory. You can find more details about on-demand scanning here in this page. Please note that it will refer to the watch and policy and the policy has a rule to fail the build if the image contains any vulnerability.
  5. Push the image to the Artifactory and publish the build information.
  6. Scan that build using the JFrog Xray.
JFrog Platform Setup:

We have the below configurations in the JFrog platform. Please note that we will be pushing the build and scan it as part of a project. For more details on the project, kindly refer to this document.

 1. We have first created a project and it will look like below - 
User-added image

2. We have created a virtual, a remote and a local repository and we have clubbed the local and the remote repository to the virtual repository and we will use the virtual repository as an endpoint - 

User-added image

3. Xray configuration has to be done as below - 

A. Create a watch and policy before we run the pipeline. Otherwise the XRay scan will not succeed. Please follow the below links for creating the watch and policy –

Create the policy and the rules using this link – https://www.jfrog.com/confluence/display/JFROG/Creating+Xray+Policies+and+Rules 

Create the watch and add the repository, build, policies and rules using this link – https://www.jfrog.com/confluence/display/JFROG/Configuring+Xray+Watches

B. Make sure to enable the below option in the rules of the policy when it is created – 

User-added image
C. Need to include the build in the Indexed Resources in the JFrog platform. However, when the build is first created with the Xray scan step, it will skip the Xray scan step as the build is not included in the Indexed Resources. That’s why it is recommended to include the builds using the Include or Exclude pattern. For this example, the Include pattern is “*/**”. This means the platform will add all the builds that are pushed to the Artifactory, to the Indexed Resources –

User-added image

For more details on the Index Resources, It is also recommended to go through this article

Docker File:

To do a docker build, we need to have a docker file created. The docker file contents is as below - 

FROM nginx:alpine
RUN rm /etc/nginx/conf.d/*
RUN echo "files are deleted"
COPY ./public/helloworld.conf /etc/nginx/conf.d/
COPY ./public/index.html /usr/share/nginx/html/
COPY ./public/arti-error.txt /usr/share/nginx/html/


The docker file is actually performing the below items - 

  1. Pull an Nginx image
  2. Removing all the contents of the folder “/etc/nginx/conf.d/”
  3. Putting a message that all the files are deleted
  4. Copying all the files that we have uploaded to the GitLab repository, copy it to a specific file location.
GitLab setup:

We need to have the below GitLab setup - 

1. First create a project in the GitLab. You can find the details on how to create the project using the below links - 

A. GitLab official link - https://docs.gitlab.com/ee/user/project/working_with_projects.html
B. Youtube link - https://www.youtube.com/watch?v=DGuMvGYZ7lY


2. Upload all the required files to GitLab repository as mentioned in the links in step # 1. It will look like this - 

User-added image

As you can see from the above screenshot that there is a “public” folder created. This folder contains all the files that I am copying to the specific folders as mentioned in the Docker file. It looks like this - 

User-added image

3. Click on the settings button and make sure that you have the CI/CD option enabled as shown in the screenshot below. Else, you will not see the CI/CD option where you have to check the pipeline run status - 

User-added image

4. Now create the variables in the settings as shown below. We will use these variables in the pipeline script - 

User-added image

The defined variables are as follows - 
ART_DOCKER_PASS: <Password to access Artifactory docker repository. Generally it is the same with which you login to the JFrog UI and view docker repositories>

ART_DOCKER_REG: test.jfrog.io

ART_DOCKER_USER: <Username to access Artifactory docker repository. Generally it is the same with which you login to the JFrog UI and view docker repositories>

ART_PLAT_PASS: <Password to login to JFrog UI>

ART_PLAT_URL: https://test.jfrog.io

ART_PLAT_USER: <Username to login to JFrog UI>

ARTIFACTORY_PROJECT_KEY: <Project Key defined in the project as shown in step # 1 under JFrog Platform Setup>

5. Now add a file called “.gitlab-ci.yml” in the GitLab project. The details on how to add the file is mentioned in this document - https://docs.gitlab.com/ee/ci/quick_start/
6. Now add the below content in the yaml file that is just added - 

docker-build:
  # Use the official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    - apk add --update curl && rm -rf /var/cache/apk/*
    # docker login to Artifactory
    - docker login -u "$ART_DOCKER_USER" -p "$ART_DOCKER_PASS" $ART_DOCKER_REG
    # Installing JFrog CLI
    - curl -fL https://install-cli.jfrog.io | sh
    - chmod +x /usr/local/bin/jf
    # Configure Artifactory instance with JFrog CLI
    - pwd
    - cd /usr/local/bin/
    - ./jf --version
    - ./jf c add  artifactory-server --url=$ART_PLAT_URL --user=$ART_PLAT_USER --password=$ART_PLAT_PASS
  script:
    # docker build  
    - docker build --pull -t "$ART_DOCKER_REG/dct-docker/test:$CI_JOB_ID" /builds/swarnendukayal/docker-jfrog
    - cd /usr/local/bin/
    # On-deman scanning
    - ./jf docker scan "$ART_DOCKER_REG/dct-docker/test:$CI_JOB_ID" --project=$ARTIFACTORY_PROJECT_KEY
    # Docker push
    - ./jf rt docker-push "$ART_DOCKER_REG/dct-docker/test:$CI_JOB_ID" dct-docker --build-name=docker-jfrog --build-number=$CI_JOB_ID --project=$ARTIFACTORY_PROJECT_KEY 
    # Collect the environment variables
    - ./jf rt bce docker-jfrog $CI_JOB_ID --project=$ARTIFACTORY_PROJECT_KEY
    # Pass the build information to Artifactory
    - ./jf rt bp docker-jfrog $CI_JOB_ID --project=$ARTIFACTORY_PROJECT_KEY
    # Scanning the docker build
    - ./jf bs docker-jfrog $CI_JOB_ID --project=$ARTIFACTORY_PROJECT_KEY
  # Run this job in a branch where a Dockerfile exists
  rules:
    - if: $CI_COMMIT_BRANCH
      exists:
        - Dockerfile


Please note that the location “/builds/swarnendukayal/docker-jfrog” is configured to define the GitLab repository path from where the Dockerfile will refer the files while doing the docker build. This location will change project to project and profile to profile. 

For example: as the URL of my sample project is - 
If the URL was as below - https://gitlab.com/abc/test-jfrog/-/blob/master/.gitlab-ci.yml, then the location would be “/builds/abc/test-jfrog”. Kindly change as per your project and repository location.

Once you save this file, it will immediately, trigger a build and you can find the build status as shown below - 

User-added image

The builds are showing failure as the Xray policy has been defined to fail the build if any vulnerabilities are caught. 

How does the build look in JFrog platform:

1. The build will appear in the builds section as below - 

User-added image

2. Once you click on the build, it will show you the build numbers as shown below - 

User-added image


As you can see that the Xray status is also showing as the builds are scanned and the violations are given. If you would like to check the XRay status specific to a build number, then click on the build number and then go to the “XRay Data” as below - 

User-added image


3. The scan that we have done of the image before we have pushed it to the Artifactory, can be seen here - 

User-added image

Please note that the steps that we are doing here, except the docker build steps, are basically the steps as mentioned in this blog – https://jfrog.com/blog/manage-your-docker-builds-with-jfrog-cli-in-5-easy-steps/. Kindly go through it as well for more clarity on how the JFrog CLI works with docker.