.gitlab-ci.yml以包含来自多个yml文件的多个shell函数
我有一个Gitlab mono存储库,其中包含一些后端Java和前端Node.js代码.为了创建CI,我正在研究一种共享方法来构建这两个应用程序.
I have a Gitlab mono repository with some backend Java and frontend Node.js code. To create a CI, I'm working on a shared approach to build both the applications.
在应用程序存储库中,我们将其称为"A",我有源代码以及如下的.gitlab-ci.yml文件,
In the application repository, let's call it "A", I have source code as well a .gitlab-ci.yml file as below,
A
├── .gitlab-ci.yml
├── backendapi
└── ui
.gitlab-ci.yml 文件
---
include:
- project: 'root/B'
ref: master
file: 'top-level.yml'
- project: 'root/B'
ref: master
file: 'maven.yml'
- project: 'root/B'
ref: master
file: 'node.yml'
我还有一个名为"B"的存储库,其中所有我的CI功能都存储在三个不同的文件中.
I have another repository called "B", where I have all my CI functionalities in three different files.
B
├── maven.yml
├── node.yml
└── top-level.yml
- top-level.yml 文件,其中包含我的构建阶段,
- top-level.yml file that has my build stage in it,
---
stages:
- build
variables:
GIT_SSL_NO_VERIFY: "1"
.build_script: &build_script
stage: build
tags:
- default
- docker
java_build:
<<: *build_script
image:
name: maven:latest
script:
- backend_build
node_build:
<<: *build_script
image:
name: node:slim
script:
- frontend_build
- maven.yml ,具有mvn构建功能,
- maven.yml, that has mvn build function,
.maven_build: &maven_build |-
function backend_build {
cd backendapi
mvn clean package -DskipTests
}
before_script:
- *maven_build
- node.yml ,其中具有节点功能,
- node.yml, with node function in it,
.node_build: &node_build |-
function frontend_build {
cd ui
npm install
npm build
}
before_script:
- *node_build
运行存储库"A"中的 .gitlab-ci.yml 文件时,它正在调用 top-level.yml , maven.yml和 node.yml 文件来自存储库"B",很好.
When the .gitlab-ci.yml file in repository "A" is run, it is calling the top-level.yml, maven.yml and node.yml files from the repository "B" which is good.
这里的问题是,当它运行 java_build 时,它无法从 maven.yml 中找到 backend_build 函数,似乎仅从 node.yml 文件加载 frontend_build 函数或从 maven.yml 文件覆盖 backend_build 函数. node_build 可以正常工作,因为它可以找到 frontend_build 函数.
The problem here is when it runs the java_build it is unable to find the backend_build function from maven.yml instead it seems like it only loading the frontend_build function from node.yml file or overwriting the backend_build function from maven.yml file. The node_build works as expected, cause it can find the frontend_build function.
the Skipping Git submodules setup
Authenticating with credentials from /root/.docker/config.json
Authenticating with credentials from /root/.docker/config.json
Authenticating with credentials from /root/.docker/config.json
$ function frontend_build { # collapsed multi-line command
$ backend_build
/bin/bash: line 90: backend_build: command not found
我知道我可以将所有功能复制到存储库"B"中的一个大yml文件中,并在存储库"A"中将.gitlab-ci.yml包括在内,但在这里我试图理解是否有可能尝试以上方法.
I know that I can copy all the functions into one big yml file in repository "B" and include the in .gitlab-ci.yml in the repository "A" but here I'm trying to understand is it even possible to try the above approach.
提前谢谢!
好吧,终于找到了一个破解方法,但并不是一个完整的答案,因为yaml文件无法像我在问题中所述那样起作用,但是我采用了另一种方法来解决问题.
Ok, Finally found a hack but not a complete answer as yaml files cannot act accordingly as I stated in my question, but I took a different approach to solve the problem.
嗯,不再有maven.yml或node.yml,存储库中只有四个文件 B
backend.yml
, frontend.yml
, hybrid.yml
和 top-level.yml
.
Well, there are no more maven.yml or node.yml, there are only four files in the repository B
backend.yml
, frontend.yml
, hybrid.yml
and top-level.yml
.
backend.yml具有所需的所有功能(build_app,lint_app,unit_test_app等),并且在frontend.yml之后的功能相同,但具有不同的命令.
The backend.yml has all the functions (build_app, lint_app, unit_test_app and so on..) that are required and the same follows the frontend.yml with different commands in the functions.
ex:在backend.yml build_app函数中,我将同时在 frontend.yml
build_app函数中使用maven命令,并使用nom命令.这里的build_app函数名称在frontend.yml和backend.yml中都是通用的,但是功能不同.
ex: In the backend.yml build_app function I will have the maven command at the same time in the frontend.yml
build_app function I will have the nom command. Here the build_app function name is common in both the frontend.yml and backend.yml but the functionality is different.
在top.yml阶段,我在脚本键中将通用函数名称指定为build_app.
In the top-level.yml stages, I specified the common function name as build_app in the script key.
stages:
- build
variables:
GIT_SSL_NO_VERIFY: "1"
.build_script: &build_script
stage: build
tags:
- default
- docker
build:
<<: *build_script
image: maven:latest
script:
- build_app
但是在.gitlab-ci.yml中,根据我需要做的构建,我包括了那个特定的yml文件.在下面的示例中,我要构建后端并包含backend.yml,同样适用于前端.
But in the .gitlab-ci.yml, depending on the build I need to do, I include that specific yml file. In the below example I want to build the backend and included the backend.yml same applies for the frontend.
include:
- project: 'root/B'
ref: master
file: 'top-level.yml'
- project: 'root/B'
ref: master
file: 'backend.yml'
如果必须同时构建后端和前端,则我将使用hybrid.yml,其功能名称与build_app相同,但同时包含maven和npm命令.我知道这不是正确的方法,但是我要解决的用例就足够了.
If I have to build both the backend and frontend, I will use a hybrid.yml with the same function name as build_app but include both the maven and npm command. I know this is not the right approach but I will suffice the use case I'm trying to solve.
谢谢您帮助我解决这个问题!
Thank you for helping me with the question!
快乐自动化:)