Currently I have a large Bash script in my GitLab CI YAML file. The example below is how I am grouping my functions so that I can use them during my CI process.
.test-functions: &test-functions |
function write_one() { echo "Function 1" }
function write_two() { echo "Function 2"}
function write_three() { echo "Function 3"}
.plugin-nuget:
tags:
- test-es
- kubernetes
image: mygitlab-url.com:4567/containers/dotnet-sdk-2.2:latest
script:
- *test-functions
- write_one
- write_two
- write_three
The example below shows how we can include a YAML file inside another one:
include:
- project: MyGroup/GitlabCiPlugins/Dotnet
file: plugin-test.yml
ref: JayCiTest
I would like to do the same thing with my script. Instead of having the script in the same file as my YAML, I would like to include the file, so that my YAML has access to my script's functions. I would also like to use PowerShell instead of Bash if possible.
How can I do this?
Currently I have a large Bash script in my GitLab CI YAML file. The example below is how I am grouping my functions so that I can use them during my CI process.
A file called .gitlab-ci.yml in the root of your repository, which contains the CI/CD configuration. In the .gitlab-ci.yml file, you can define: The scripts you want to run. Other configuration files and templates you want to include. Dependencies and caches. The commands you want to run in sequence and those you want to run in parallel.
In the .gitlab-ci.yml file, you can define: The scripts you want to run. Other configuration files and templates you want to include. Dependencies and caches.
Looking further down the screen we see an error message. As mentioned, .gitlab-ci.yml is the build file that stored within the repository itself. The commit has been successfully placed into source control, but because we enabled CI for this project, GitLab has also expected the build file to exist.
include
feature doesn't import YAML anchors.before_script
in one .gitlab-ci.yml file and then use it in before_script
in another. You can only rewrite it, not concat.Because of all of these problems you can't easily manage your scripts; split them, organize them and do another nice developer's decomposition stuff.
There are possible workarounds. You can store your scripts somewhere else; where a gitlab runner could reach them, and then inject them to your current job environment via source /ci-scripts/my-script.sh
in before_script
block.
Possible locations for storing ci scripts:
before_script
. Just in case, make sure nobody, except gitlab runner could access it.You can use powershell only if you installed your GitLab Runner on Windows. You can't use anything else in that case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With