GitLab CI and Artifactory: On Your Mark, Get Set, Build!

GitLab CI and Artifactory

Integrating JFrog Artifactory into your GitLab CI workflow makes JFrog’s ‘Release Fast or Die’ mantra, a living reality.
This blog post describes how to integrate GitLab CI with Artifactory not only to resolve and deploy binaries but also to benefit from Artifactory’s 
Build Integration feature.

Publish and manage your packages

GitLab CI supports creating multiple builds and assesses how each commit passes your tests and affects your products. During the build process, a mass of binaries is generated, that if not managed correctly on a large scale, can lead to binary havoc. To overcome this, Artifactory is seamlessly integrated into your build process in order to publish and manage these binaries. Via JFrog CLI, GitLab CI fetches, caches, and publishes your dependencies, created packages, and build information to Artifactory.

Artifactory’s Build Integration automatically links each generated package to its relevant build, dependencies, information, and collects a new set of metadata that you might not have benefited from until now. Once Artifactory is integrated with GitLab CI, you can store and view the information such as:

  • Build information and published modules
  • Used Dependencies
  • Environment Variables
  • License Summary
  • Links to your issue-tracker
  • Diffs between builds

The data is stored in the Artifactory database allows you to retrieve it also using the Artifactory Query Language.

Embed Artifactory into your GitLab CI workflow

The following step-by-step example demonstrates how to embed Artifactory into your GitLab CI workflow. In this example, we’ll use Maven. This and additional package type examples are available in GitHub.

  1. Create a new project in GitLab.
  2. Clone this Maven sample project into your GitLab repository.
  3. Configure the following Artifactory credentials in your Maven project, under CI/CD Settings > Secret variables:
    ARTIFACTORY_URL: https://artifactory.mycompany.com/artifactory
    ARTIFACTORY_USER: admin
    ARTIFACTORY_PASS: password
    MAVEN_REPO_KEY: Name of the target repository in Artifactory
    GitLab and Artifactory
  4. Commit the following .gitlab-ci.yml configuration file to the root directory of your project. This file is a customized template that can be edited according to your needs.
    .gitlab-ci.yml sample

    # This file is a template, and might need editing before it works on your project.
    image: maven:latest
    before_script:
      # Install JFrog CLI
      -  curl -fL https://getcli.jfrog.io | sh
      # Configure Artifactory instance with JFrog CLI
      - ./jfrog rt config --url=$ARTIFACTORY_URL --user=$ARTIFACTORY_USER --password=$ARTIFACTORY_PASS
      - ./jfrog rt c show
      # Set the M2_HOME environment variable
      - export M2_HOME=/usr/share/maven
      # Replace the repository name in the configuration.yml to the correct one.
      - sed -i 's,MAVEN_REPO_KEY,'"$MAVEN_REPO_KEY"',g' configuration.yml
    build:
      script:
        # Run the MVN command
        - ./jfrog rt mvn "clean install" configuration.yml --build-name=gitlabci-maven-artifactory --build-number=$CI_JOB_ID
        # Collect the environment variables
        - ./jfrog rt bce gitlabci-maven-artifactory $CI_JOB_ID
        # Pass the build information to Artifactory
        - ./jfrog rt bp gitlabci-maven-artifactory $CI_JOB_ID
      only:
        - master
    

    An instant build is automatically triggered after you commit the configuration file and Artifactory is populated with your Maven packages and cached dependencies.

  5. Navigate to the job in the GitLab CI UI to view the build process and logs.GitLab Job View
  6. Log into Artifactory and navigate to the Artifact Repository Browser to view your published artifacts in Artifactory.
    GitLab and Artifactory
  7. Navigate to the Build Browser to view the build information.
    GitLab and Artifactory
    That’s it! You’re done.