How to share files to artifact from after_script?

I am trying to share files using artifacts.
For that, I have kept creation of a tarfile and copying it to deploy1 folder using in after_script section.
So that, it will run even if previous commands failed.
But, it is not showing in artifacts and thereby next stage failing.

Hi,

can you share the .gitlab-ci.yml configuration steps you’ve done thus far?

Cheers,
Michael

calling below one in after_script of a job.

And it is not available in the next stage of the job.

.forward_projectn_resources: &forward_projectn_resources

    - yum install -y tar #zip

    - mkdir dist1

    - tar -cvzf dist1/projectn.tar.gz --absolute-names $HOME/.project-n

    #- zip -r dist1/projectn.zip $HOME/.project-n

In your job definition you will also need something like:

artifacts:
    paths:
        - dist1/projectn.tar.gz
1 Like

I even shared the complete folder. But, not working. Will it work if I keep in after_script. Or should I keep only in script section? The file creation part

I have two steps in the script section.


  script:
    - *run_deploy
    - *forward_resources

The deploy1 folder will be created in forward_resourses.
But as the run_deploy failes, it is not going to next step.

So, I tried to keep this forward_resouces in after_script but it is failing to share artifact and I am getting file not found in next stage.

Even, I shared the complete dist1 folder, not just file

This is the potion of pipeline I have.

install-rpm:

  stage: deployrpm

  allow_failure: true

  extends:

   - .iftestawsdeploy

  image:

    name: amazon/aws-cli

    entrypoint: [""]

  variables:

      terraform_version: 1.1.7

      terragrunt_version: 0.36.6

      kubectl_version: 1.21.11

  before_script:

    - *install_dependencies

    - *install_projectn_rpm

  script:

    - *run_projectn_deploy

  after_script:

    - echo "Just to test after script creating artifacts or not even successful"

    - *forward_projectn_resources

    - ls dist1

  artifacts:

   paths:

    - ./dist1

  tags:

    - ec2runner

The above script block may fail sometimes. Irrespective of its result, after_script will run, so I am creating a tar.gz file there, which I’m adding the folder to artifacts, so available to next stages.

But, the artifact is being created only when the script block is success. If the script block is failed then the after_script content is not adding to artifacts even the tar file is created.

How to make the tar file available to artifacts even the script block is failed?

Please suggest.

Since you mentioned

it will run even if previous commands failed.

I thought you might be interested in artifacts:when because artifacts are by default not uploaded on job failure.

3 Likes