Copy a file into a remote GitLab repository

Quite new to GitLab CI/CD. I’m using self-hosted GL.
I’ve got a Python script that extracts data from a text file (hosts) and generates a second text file (config).
I would need to copy the second file into a remote GL repository.

This is what I’ve done so far in my gitlab-ci.yml file:

stages:
  - build
  - copy

build-job:
  stage: build
  only:
    - main
  script:
    - python3 --version
    - python3 inventory2ssh.py
    - cat config
 
copy-job:
  stage: copy
  only:
    - main
  script:
    - cat config
    - git clone "https://user:$PROJECT_TOKEN@remote_repo_url" ssh-config
    - cp config ssh-config/ssh/
    - cd ssh-config/ssh
    - git config user.name "my_user"
    - git config user.email "my_email"
    - git add config
    - git commit -m "Adding config file"
    - git push origin master

The problem is that only the first job succeeds, the second one throws this error:

fatal: pathspec 'config' did not match any files

Hey,

One question: What type of GitLab Runner Executor are you using?

If the answer is Docker - you need to know that every job runs in a separate container (your project is cloned, script is executed…) and when done, container is deleted - and everything with it if you didn’t push it somewhere, or specify artifacts.

So, I think (hope), the solution should be quite simple - define artifacts in your first job:

build-job:
  stage: build
  only:
    - main
  script:
    - python3 --version
    - python3 inventory2ssh.py
    - cat config
  artifacts:
    paths:
      - config

GitLab Runner will then upload this artifact to GitLab, and by default, all jobs in the next stage will download this artifact and be able to use it :slight_smile:

Hope this helps!

1 Like

Hallo Paula,
Worked like a charm!

Danke schön

It was a Shell executor by the way! :slightly_smiling_face:

1 Like