Gitlab-ci includes a file from another project that executes a script file

I have two different projects. project1 and project2. Inside project1, I have file_project1 file:

apply:
  stage: apply
  script:
    - bash folder/scripts/automation.sh
  rules:
    - if: $CI_PIPELINE_SOURCE == "push"
      when: always

In project2, I have created the .gitlab-ci.yml and I have included the project1 and the file_project1:

include:
  - project: 'namespace/project1'
    ref: main
    file: 'file_project1'

During the execution, project2 does not recognize the folder/scripts/automation.sh. I got the following error:

bash: folder/scripts/automation.sh: No such file or directory

Please, how can the pipeline inside project2 executes correctly the bash instruction defined in project1 ?

1 Like

Hi the file folder/scripts/automation.sh doesn’t exist in project2 and therefore doesn’t recognize it. When you include yaml , it doesn’t include other files in the project, just the yaml. You can try use Parent-child-pipeline so project2 will trigger the pipeline in project 1

1 Like

What you recommended for shared sh files between projects-components cicd ? (or no use sh files ??)
any full sample about it ?

o use sh functions only in YML ? How I build a Gitlab CI pipeline as a shared common library pipeline | by Nguyễn Hoàng Minh Quân | Medium

Currently you will need to have the sh file exist in the project you run the pipeline. With include (template components) you are merging a single file that is evaluated in the context of the pipeline’s project, not the component project.

CI/CD Steps is a new feature, currently “Experimental”, which will have the functionality you need. This is the docs link for CI/CD Steps CI/CD steps | GitLab

1 Like

I’m awaiting a feature like this, too, at the moment I have a workaround using the package registry. The general gist is:

  1. I create a ‘release’ in project1’s workflow which:
    1. Adds the extra files to an archive.
    2. Puts the archive in the package registry.
    3. Adds a link to the package to the release.
  2. I add a template in project1 that contains a job to download the package (in before_script), via the link provided in project1’s releases page.
  3. I use the template in project2’s workflow to download the package.

Then the script can be run from project2.

It sounds convoluted, that’s why I’m hoping that ‘steps’ are introduced soon.

See https://gitlab.com/stephematician/r-gitlab-ci for an example.

My troubleshooting:

shared library (YML files)

add or override steps when using include: (templates) and/or extends: and/or globals/default for before_script (after_script)

Using anchors and reference tags

Here is a video shows how to configure CI Components that can access other repository files by using CI Steps. https://youtu.be/qxTbeYXEQLM?feature=shared

You can use ci steps today to access local files and run them as scripts
Check our this new YouTube video https://youtu.be/qxTbeYXEQLM?si=uIWwwl4ttlALiKQ9

I’d prefer some written summary :slight_smile: instead of a video.