Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a PowerShell script file in a GitLab CI YAML file

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?

like image 977
Zorthgo Avatar asked Jan 17 '20 21:01

Zorthgo


People also ask

Is there a bash script in GitLab CI YAML file?

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.

How to run GitLab CI/CD scripts in parallel?

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.

What can I include in GitLab-CI?

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.

What is GitLab-CI-yml?

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.


1 Answers

Split shell scripts and GitLab CI syntax

  • GitLab CI has no feature "include file content to script block".
  • GitLab CI include feature doesn't import YAML anchors.
  • GitLab CI can't concat arrays. So you cant write 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:

  1. Special docker image with all your build/test/deploy utils and ci scripts
  2. The same, but dedicated build server
  3. You can deploy simple web page containing your scripts and download and import then in before_script. Just in case, make sure nobody, except gitlab runner could access it.

Using powershell

You can use powershell only if you installed your GitLab Runner on Windows. You can't use anything else in that case.

like image 145
heyzling Avatar answered Sep 30 '22 05:09

heyzling