Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

GitLab CI Templates - reference file/script local to the template

Say I have a repo ci-templates with all my reused gitlab-ci.yml templates. I am trying to add a template that generates Release Notes based on the commits between tags. I generate the release notes with a bash script that lives in the ci-templates repo. The release job uses image registry.gitlab.com/gitlab-org/release-cli which uses ash by default but I can install bash, git, and a few other tools at the start.

How can I add a bash script in the ci-templates repo that is pulled in and used during other other CI jobs when the templates is used via

include:
  - project: 'ci-templates'
    file: 'templates/release-notes.yml'

I tried writing the bash script into the yml file, but script: is not executed as bash. Would I have to manually call bash on every single line? Is there no cleaner way?

I also tried using $CI_JOB_TOKEN and the files api to download the file from the repo and then run it via bash script.sh but this fails since the token does not have api permission to use the file api.

At this point, trying to make this a generic template is more pain than its worth. I'd be more productive just copy pasting the script everywhere, but I'd like to avoid that for the sake of my team. Any ideas here? I think I understand the yaml important, but I have no idea how to reference anything that lives outside of the yaml.

question from:https://stackoverflow.com/questions/66057191/gitlab-ci-templates-reference-file-script-local-to-the-template

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the template-ci yaml move the bash script to a job in the release-notes.yml

generate-release-notes:
  script:
    - echo "do something"
    - echo "done"

Then when you include that template into your repo that job will be executed. If you need to customize the behavior of the script use the "variables" section of the job


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...