Better way to download GitLab Maven Artefacts from intern Gitlab Maven Repository

  docker, gitlab, gitlab-ce, gitlab-ci, maven

I have two stages in my gitlab ci.
First build maven + deploy to gitlab mvn repository secound build docker images + push. I use different images each stage. So I can’t move the binary from the deploy-maven stage to the correct locations and I build multiple docker images from the one maven job (multiple maven module project). I only need one specific artefact for each docker image (one big jar with added dependencies)

This is a simply fight version of my gitlab-ci.yml:

stages:   
  - build-artefakts   
  - build-image

variables:
 ... 

deploy-maven:
  image: maven:3.6-jdk-11
  stage: build-artefakts
  script:
    - 'mvn deploy -f pom.xml -s ~config/maven/ci_settings.xml -P prod'
  only:
    - master

deploy-docker-1:
  image: $CI_BASE_IMAGE
  stage: build-image
  before_script:
    - 'docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_TOKEN  $CI_REGISTRY'
  script:
    - 'cd docker'
    - 'mvn dependency:copy -Dartifact=$1_GROUP_ID:$1_ARTIFACT_ID:$PROJECT_VERSION:jar:$1_CLASSIFIER -s ../~config/maven/ci_settings.xml'
    - 'mv ./target/dependency/*.jar $1_JAR_NAME'
    - 'docker build . -t $1_IMAGE_NAME:$PROJECT_VERSION --build-arg JAR_FILE=$1_JAR_NAME'
    - 'docker tag $1_IMAGE_NAME:$PROJECT_VERSION $CI_REGISTRY/$CI_REGISTRY_NAMESPACE/$1_IMAGE_NAME:$PROJECT_VERSION'
    - 'docker push $CI_REGISTRY/$CI_REGISTRY_NAMESPACE/$1_IMAGE_NAME:$PROJECT_VERSION'

deploy-docker-2:
  image: $CI_BASE_IMAGE
  stage: build-image
  before_script:
    - 'docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_TOKEN  $CI_REGISTRY'
  script:
    - 'cd docker'
    - 'mvn dependency:copy -Dartifact=$2_GROUP_ID:$2_ARTIFACT_ID:$2_VERSION:jar:$2_CLASSIFIER -s ../~config/maven/ci_settings.xml'
    - 'mv ./target/dependency/*.jar $2_JAR_NAME'
    - 'docker build . -t $2_IMAGE_NAME:$PROJECT_VERSION --build-arg JAR_FILE=$FETCHER_JAR_NAME'
    - 'docker tag $2_IMAGE_NAME:$PROJECT_VERSION $CI_REGISTRY/$CI_REGISTRY_NAMESPACE/$2_IMAGE_NAME:$PROJECT_VERSION'
    - 'docker push $CI_REGISTRY/$CI_REGISTRY_NAMESPACE/$2_IMAGE_NAME:$PROJECT_VERSION'

And this line downloads the Artefact but unfortunately It downloads all dependencies, too. And this makes my stage to build docker images super slow. I only need one specific artefact. But mvn thinks that I need all artefacts to building this project.

 - 'mvn dependency:copy -Dartifact=$1_GROUP_ID:$1_ARTIFACT_ID:$PROJECT_VERSION:jar:$1_CLASSIFIER -s ../~config/maven/ci_settings.xml' 

I looked at the gitlab api and come up with this pseudocode:

Prepare:
$1_GROUP_ID_AS_PATH = $1_GROUP_ID // Replace . from Group Id with /
$1_PACKAGE_NAME = $1_GROUP_ID_AS_PATH/$2_ARTIFACT_ID

// Default Frist element is: The newest 
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5454/packages?package_name=$1_PACKAGE_NAME&package_type=maven 
// Pipe the result from the newst to grep the first id

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5454/packages/$NEWEST_ID"

// than grep the web_path and curl again

But this is i think a very complex solution. There has to be a better one.
My question is how to download artefacts without a complex setup from the GitLabs intern maven repostroy or can I pase somehow the artefacts from one stage to another ( this is kind of a cheat but I think its a better solution than mine)

Source: Docker Questions

LEAVE A COMMENT