如何在GitLab CI YAML文件中包含PowerShell脚本文件 [英] How to include a PowerShell script file in a GitLab CI YAML file
问题描述
当前,我的GitLab CI YAML文件中有一个大型Bash脚本.下面的示例说明了如何对函数进行分组,以便可以在CI流程中使用它们.
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
以下示例显示了如何在另一个文件中包含YAML文件:
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
我想对我的脚本做同样的事情.我不想将脚本与YAML放在同一个文件中,而是要包含该文件,以便我的YAML可以访问脚本的功能.如果可能的话,我也想使用PowerShell代替Bash.
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.
我该怎么做?
推荐答案
拆分shell脚本和GitLab CI语法
- GitLab CI不具有将文件内容包含到脚本块中"的功能.
- GitLab CI
include
功能不会导入YAML锚点. - GitLab CI无法连接数组.因此,您不能在一个.gitlab-ci.yml文件中编写
before_script
,然后在另一个.gitlab-ci.yml文件中使用它.您只能重写它,不能重写. - 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 inbefore_script
in another. You can only rewrite it, not concat. - 带有所有构建/测试/部署实用程序和ci脚本的特殊docker镜像
- 相同但专用的构建服务器
- 您可以部署包含脚本的简单网页,然后在
before_script
中下载并导入.为了以防万一,请确保除gitlabRunner之外的任何人都无法访问它. - Special docker image with all your build/test/deploy utils and ci scripts
- The same, but dedicated build server
- 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.
Split shell scripts and GitLab CI syntax
由于所有这些问题,您无法轻松地管理脚本;拆分它们,组织它们,然后做另一个不错的开发人员分解工作.
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.
有可能的解决方法.您可以将脚本存储在其他位置.gitlab跑步者可以到达的位置,然后通过 before_script
块中的 source/ci-scripts/my-script.sh
将它们注入到当前的工作环境中.
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.
可能存储ci脚本的位置:
Possible locations for storing ci scripts:
使用powershell
仅当在Windows上安装了GitLab Runner时,才能使用powershell.在这种情况下,您不能使用其他任何东西.
Using powershell
You can use powershell only if you installed your GitLab Runner on Windows. You can't use anything else in that case.
这篇关于如何在GitLab CI YAML文件中包含PowerShell脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!