Stephen Paulin
Stephen Paulin

Reputation: 101

Gitlab working directory not clean when using cache with CLONE_STRATEGY: none

I have a GitLab pipeline setup that has a package step to do a maven build during the tag event and a release to upload the jar to the GitLab generic package registry using curl and GitLab-release cli.

What I'm expecting to happen is a cache of the .m2 to be loaded into the package step to allow the mvn clean package to do its thing. Then archive the created jar and test results only.

The release step should begin clean with no git clone, no cache and only the jar and test results.

Instead the 'find .' shows the release step contains everything including

From the cache documentation (https://docs.gitlab.com/ee/ci/caching/) on GitLab it states

Why is GitLab putting so much content into the release job? The release job fails at times because its finding multiple Jar files from previous tags (IE the clean and the archiving are holding past version).

gitlab-ci.yml

variables:
  MAVEN_CLI_OPTS: "-s $CI_PROJECT_DIR/.m2/settings.xml"
  MAVEN_VERSION_PLUGIN_VERSION: 2.11.0
  MAVEN_ARTIFACT_NAME: test-component
  GIT_CLEAN_FLAGS: -ffd
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${MAVEN_ARTIFACT_NAME}"

cache:
  key: primary
    paths:
      - .m2/repository

stages:
   - package
   - release

package:
  stage: package
  image: maven:latest
  script:
    - mvn ${MAVEN_CLI_OPTS} clean package
  artifacts:
    paths:
      - target/*.jar
      - target/surefire-reports
  only:
    - tags
    - merge_requests
    - branches
  except:
    - main

release:
  stage: release
  image: alpine:latest
  cache: []
  variables:
    GIT_STRATEGY: none
  dependencies:
    - package
  script:
    - |
      apk add curl gitlab-release-cli
      find .
      JAR_NAME=`basename target/${MAVEN_ARTIFACT_NAME}-${CI_COMMIT_TAG}.jar`
      'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file target/${JAR_NAME} ${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/${JAR_NAME}'
      release-cli create --name "Release $CI_COMMIT_TAG" --description "$TAG_MESSAGE" --tag-name ${CI_COMMIT_TAG} --assets-link "{\"name\":\"jar\",\"url\":\"${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/${JAR_NAME}\"}"
  only:
    - tags

Upvotes: 0

Views: 1461

Answers (1)

Michael Delgado
Michael Delgado

Reputation: 15452

See the GitLab docs on GIT_STRATEGY:

A Git strategy of none also re-uses the local working copy, but skips all Git operations normally done by GitLab. GitLab Runner pre-clone scripts are also skipped, if present. This strategy could mean you need to add fetch and checkout commands to your .gitlab-ci.yml script.

It can be used for jobs that operate exclusively on artifacts, like a deployment job. Git repository data may be present, but it’s likely out of date. You should only rely on files brought into the local working copy from cache or artifacts.

So GitLab documentation is pretty clear that you should always expect the git repository to be present. When you want to work exclusively with artifacts, I you can create a new temporary directory and reference the path to the artifacts explicitly rather than relying on a totally clean working directory.

Upvotes: 0

Related Questions