Search code examples
gitlabcontinuous-integrationgitlab-ci

Sharing an artifact between multiple gitlab-ci jobs


I'm working on a ci pipeline for a gitlab deployment. In the 'build' stage I compile and package up a java WAR archive. I then have two 'deploy' jobs that need to run in sequence. Each deploy job is tasked with taking the the WAR archive generated in the build stage and copying it to a remote server. The build and the first deploy job complete successfully. When it comes time for the second deploy job to run, it fails with an error saying that it was unable to locate the WAR.

I tried using 'needs' to define the dependencies as described here: Gitlab: How to use artifacts in subsequent jobs after build with no success. What am I doing wrong?

My deployment script looks like this:

stages:
  - build
  - deploy

build-dev:
  stage: build
  tags:
    - development
  artifacts:
      paths:
        - target/myapp-$VERSION.war
  script:
    - mvn clean package -Dbuild_version=$VERSION -Ddb_pwd=$db_pwd_$PROFILE -Demail_key=$email_key_$PROFILE -Dmodo_api_token=$modo_api_token_$PROFILE -Dmodo_auth_shared=$modo_auth_shared_$PROFILE
  when: manual
  allow_failure: false

deploy-dev-1:
  stage: deploy
  tags:
    - development
  script:
    - scp -i ~/.ssh/${KEY_NAME} -P $NODE1 target/myapp-$VERSION.war ${mcUser}@$HOST:/var/lib/tomcat9/webapps/myapp.war
  variables:
    NODE1: "99008"
    HOST: "myserver-$PROFILE"

deploy-dev-2:
  stage: deploy
  tags:
    - development
  needs:
    - job: deploy-dev-1
      artifacts: true
  script:
    - scp -i ~/.ssh/${KEY_NAME} -P $NODE1 target/myapp-$VERSION.war ${mcUser}@$HOST:/var/lib/tomcat9/webapps/myapp.war
  variables:
    NODE1: "99009"
    HOST: "myserver-$PROFILE"

Solution

  • Here is how you can do this. Expose the artifact from build stage and then use 'dependencies' to specific the build job in both your deploy jobs. This will allow both your deploy jobs to get the artifact exposed by build job

    Here is a sample generic structure for your scenario. Here I expose a test.txt from build to both deploy stages. The cat and ls in deploy stages show that the file was available to those stages

    stages:
      - build
      - deploy
    
    job_build:
      stage: build
      script:
        - echo "your build script goes here"
        - echo "test" > test.txt
      artifacts:
        paths:
          - test.txt  # Define the path to your build artifacts
    
    job_deploy1:
      stage: deploy
      script:
        - echo "your deploy1 script goes here"
        - cat test.txt
        - ls -l
      dependencies:
        - job_build
    
    job_deploy2:
      stage: deploy
      script:
        - echo "your deploy2 script goes here"
        - cat test.txt
        - ls -l
      dependencies:
        - job_build