Akshay Shah
Akshay Shah

Reputation: 734

How to exclude all files except one in gitlab-ci artifacts with

I have a folder structure like

foo/bar/lorem/a.txt
foo/bar/lorem/b.txt
foo/bar/lorem/c.ext
foo/bar/ipsum/p.txt
foo/bar/ipsum/q.ext

In GitLab CI's yml artifacts I want to include everything in foo/bar, exclude *.txt but include b.txt

The GitLab CI reference for artifacts says that:

Wildcards can be used that follow the glob patterns and golang's filepath.Match.

Try 1:

job1:
  artifacts:
    paths:
      - foo/bar/
      - foo/bar/lorem/b.txt
    exclude:
      - foo/bar/**/*.txt

Try 2:

job1:
  artifacts:
    paths:
      - foo/bar/
    exclude:
      - foo/bar/**/!(b).txt

Expected output:

foo/bar/lorem/b.txt
foo/bar/lorem/c.ext
foo/bar/ipsum/q.ext

What paths and exclude combination do I use to achieve this?

Upvotes: 8

Views: 9556

Answers (2)

SteeveDroz
SteeveDroz

Reputation: 6136

Although the previous answer is good and correct, it relies on a particular case where there are only two types of files. Having a whole hierarchy with multiple file types, that could possibly be different for each job, makes it very hard to apply to other cases.

Here is an answer, a bit more difficult to set up, but that covers more cases, if someone else is looking for an answer:


At the end of your script section, delete the files you don't want. For example:

job1:
  script:
    - # Perform the actions you already do
    - mkdir keep # Create a place to temporarly keep your files
    - mv foo/bar/lorem/b.txt keep/ # Move the file to the safe place
    - rm foo/bar/*.txt # Delete all files you want to delete
    - mv keep/b.txt foo/bar/lorem/ # Put back the file you want to keep
  artifacts:
    paths:
      - foo/bar/ # Save everything, the *.txt are already removed

Note for the anxious: CI jobs are run in a container (at least it's the default behavious), and the files you work on are a copy of your project. You can delete whatever you want, nothing will be lost outside the container.

Upvotes: 0

burtsevyg
burtsevyg

Reputation: 4076

You can do this:

job1:
  artifacts:
    paths:
      - foo/bar/lorem/b.txt
      - foo/bar/*/*.ext

Upvotes: 4

Related Questions