19

I am looking for an easy and clean way to publish artefacts build with GitLab CI onto Artifactory.

I was able to spot https://gitlab.com/gitlab-org/omnibus/blob/af8af9552966348a15dc1bf488efb29a8ca27111/lib/omnibus/publishers/artifactory_publisher.rb but I wasnt able to find any documentation regarding how I am supposed to configure it to make it work.

Note: I am looking for a gitlab_ci.yaml approach, not as in implementing it externally.

sorin
  • 137,198
  • 150
  • 472
  • 707

3 Answers3

10

At a basic level, this can be done with the JFrog CLI tools. Unless you want to embed configuration in your .gitlab-ci.yml (I don't) you will first need to run (on your runner):

jfrog rt c

This will prompt for your Artifactory URL and an API key by default. After entering these items, you'll find ~/.jfrog/jfrog-cli.conf containing JSON like so:

    {
      "artifactory": {
        "url": "http://artifactory.localdomain:8081/artifactory/",
        "apiKey": "AKCp2V77EgrbwK8NB8z3LdvCkeBPq2axeF3MeVK1GFYhbeN5cfaWf8xJXLKkuqTCs5obpzxzu"
      }
    }

You can copy this file to the GitLab runner's home directory - in my case, /home/gitlab-runner/.jfrog/jfrog-cli.conf

Once that is done, the runner will authenticate with Artifactory using that configuration. There are a bunch of other possibilities for authentication if you don't want to use API keys - check the JFrog CLI docs.

Before moving on, make sure the 'jfrog' executable is in a known location, with execute permissions for the gitlab-runner user. From here you can call the utility within your .gitlab-ci.yml - here is a minimal example for a node.js app that will pass the Git tag as the artifact version:

stages:
  - build-package
build-package:
  stage: build-package
  script:
    - npm install
    - tar -czf test-project.tar.gz *
    - /usr/local/bin/jfrog rt u --build-name="Test Project" --build-number="${CI_BUILD_TAG}" test-project.tar.gz test-repo
Michael Lihs
  • 5,664
  • 12
  • 40
  • 67
Andrew Snell
  • 669
  • 6
  • 8
4

If you're building with maven this is how I managed to do mine:

Note: you need to have your artifactory credentials (user and pass) ready.

  1. Create a master password and generate an encrypted password from it. The procedure on how to create a masterpassword can be found here

  2. In your pipeline settings in gitlab, create 2 secret variables, one for the username and the other for your encrypted password.

  3. Update or create a settings.xml file in .m2 directory for maven builds. Your settings.xml should look like this:

    <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <servers>
        <server>
          <id>central</id>
          <username>${env.ARTIFACTORY_USER}</username>
          <password>${env.ENCRYPTED_PASS}</password>
        </server>
      </servers>
    </settings>
    
  4. In your .gitlab-ci.yml file, you need to use this settings.xml like this:

    image: maven:latest
    
    variables:
      MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
      MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
    
    cache:
      paths:
        - .m2/repository/
        - target/
    
    build:
      stage: build
      script:
        - mvn $MAVEN_CLI_OPTS compile
    

and that's it. This should work. You can visit here for more about how to use artifactory with maven

Michael Lihs
  • 5,664
  • 12
  • 40
  • 67
Buba Conteh
  • 174
  • 1
  • 9
  • Am trying to follow your solution, first of all, how do I create an artifacotry credential can you point me to a site? I keep seeing jfrog and it isn't free. – DaviesTobi alex Apr 03 '19 at 08:09
  • 1
    @alexdavies you need jfrog cli which is free https://jfrog.com/getcli/ if you want to create your artifactory credentials for gitlab-ci. Please follow the steps here under the `Example` section https://www.jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory, you'll have an idea of creating your artifactory credentials for your gitlab-ci. – Buba Conteh Apr 04 '19 at 11:39
  • yea thanks, I already figured that out and got it working. Thanks again. – DaviesTobi alex Apr 04 '19 at 11:41
1

I know this doesn't exactly answer your question, but I got to this question from a related search, so I thought it might be relevant to others too:

I ended up using an mvn deploy job that was bound to the deploy stage for gitlab.

Here is the relevant job portion:

deploy:jdk8:
  stage: test
  script:
    - 'mvn $MAVEN_CLI_OPTS deploy site site:stage'
  only:
    - master
  # Archive up the built documentation site.
  artifacts:
    paths:
    - target/staging
  image: maven:3.3.9-jdk-8
gsaslis
  • 2,526
  • 2
  • 23
  • 30