0

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

  • Git directory (.git)
  • Full checked out repository
  • .m2 cache
  • target (fully built as the Package step produced)

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

  • Archive: 'dependencies' keyword to control which job fetches the artifacts
  • Disable Cache uses the 'cache: []'

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
1
  • Found out the build failure wasn't because of the extra data. The failure was caused by - | for the script. The curl line fails with JAR_NAME not found. When switching back to - cmd for each line this works fine. Unsure why the multiline method fails. Still unclear on why content is making it into this job that shouldn't be. Commented Sep 8, 2022 at 15:46

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks...that explain the .git content and repo data. But why then is all the content generated by mvn (./target/*) being brought in too. Its not cached or achieved so its unclear what is bring it to another job or how to stop it.
do you mean things other than target/*.jar and target/surefire-reports? or are you just surprised that things you specified as artifacts are showing up in dependency builds?
I'm expecting the target dir to only have the *.jar and surefire-report. Instead I'm getting generate-source, class, test classes, etc... That stuff shouldn't be there since it isn't cached and isn't archived.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.