How to publish builds to Artifactory from GitLab CI?
Asked Answered
P

4

20

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.

Pax answered 7/9, 2015 at 16:10 Comment(1)
Have you made any progress on this?Songstress
S
12

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
Soapy answered 16/11, 2016 at 14:32 Comment(0)
B
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

Bin answered 24/8, 2017 at 7:13 Comment(3)
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.Heartbreaker
@alexdavies you need jfrog cli which is free jfrog.com/getcli if you want to create your artifactory credentials for gitlab-ci. Please follow the steps here under the Example section jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory, you'll have an idea of creating your artifactory credentials for your gitlab-ci.Bin
yea thanks, I already figured that out and got it working. Thanks again.Heartbreaker
L
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
Langobardic answered 23/2, 2017 at 12:27 Comment(0)
W
0

JFrog documentation suggests this:

image: gradle:latest
 
before_script:
 # Download JFrog CLI
 -  curl -fL https://install-cli.jfrog.io | sh
 # configure artifactory server
 - jf config add $JFROG_PLATFORM_SERVER_ID--url=$JFROG_PLATFORM_URL --user=$JFROG_PLATFORM_USER --password=$JFROG_PLATFORM_PASS
 - jf config show
 
 # configure gradle, set upload and download repos
 - jf gradlec --repo-resolve $JFROG_PLATFORM_REPO --repo-deploy $JFROG_PLATFORM_REPO
 
gitlab-ci-gradle-build: #This job name will be used as the build info repository name in artifactory
 stage: build
 script:
   # upload the built artifact to artifactory
   - jf gradle clean artifactoryPublish --build-name=$CI_JOB_NAME --build-number=$CI_JOB_ID
   # Collect environment variables for the build
   - jf rt bce $CI_JOB_NAME $CI_JOB_ID
   # Collect VCS details from git and add them to the build
   - jf rt bag $CI_JOB_NAME $CI_JOB_ID
   # Publish build info
   - jf rt bp $CI_JOB_NAME $CI_JOB_ID
   # Scan with xray
   - jf bs $CI_JOB_NAME $CI_JOB_ID

The variables JFROG_* need to be defined in the CI/CD section of the Gitlab repository.

If this is gonna be shared among a set of projects, I would bake a container image with JFrog defaults and leave only the API Key to be set per project.

Waver answered 12/3 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.