如何将自定义脚本语句添加到 gitlab 作业模板?
How to add custom script statements to a gitlab job template?
我想创建一个通用的 .deploy
模板。
从该模板继承的每个作业都应该能够在继承模板的脚本之间添加一些自定义script
逻辑。但是怎么办?
示例:
.deploy
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
before_script:
- ...
script:
- touch .env
- echo "SOME_COMMON_KEY=VALUE" >> .env
- $$PSEUDOCODE / PLACE YOUR CUSTOM LOGIC HERE
- ssh -i $SSH_ -o "StrictHostKeyChecking=no" $USER@$SERVER
- scp -i $SSH docker-compose.yml .env $USER@$SERVER:~/test/
- ssh -i $SSH $USER@$SERVER "docker-compose up"...
deploy_test:
extends: .deploy
variables:
USER: test
SERVER: test
script:
#TODO how to add custom statements in place of the $$PSEUDOCODE ?
deploy_prod:
extends: .deploy
variables:
USER: prod
SERVER: prod
script:
#TODO how to add custom statements in place of the $$PSEUDOCODE ?
那么我如何添加一些自定义语句来代替 $$PSEUDOCODE
行?
您能否让您的 2 个作业共享相同的代码?如果是,您可以使用 YAML anchors for script
在你的例子中,它可以给出:
.pseudo_code: &pseudo_code
- echo "Hello $USER on $SERVER"
.deploy:
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
before_script:
- ...
script:
- touch .env
- echo "SOME_COMMON_KEY=VALUE" >> .env
- *pseudo_code
- ssh -i $SSH_ -o "StrictHostKeyChecking=no" $USER@$SERVER
- scp -i $SSH docker-compose.yml .env $USER@$SERVER:~/test/
- ssh -i $SSH $USER@$SERVER "docker-compose up"...
deploy_test:
extends: .deploy
variables:
USER: test
SERVER: test
deploy_prod:
extends: .deploy
variables:
USER: prod
SERVER: prod
您可以做的一件事是在环境变量上使用 eval
。
.deploy:
# ...
variables:
USER_SCRIPT:
value: ""
description: "The contents of this variable will be executed with `eval` during the job script."
script:
- touch .env
- echo "SOME_COMMON_KEY=VALUE" >> .env
- eval "${USER_SCRIPT}"
# ...
用户可以在 before_script
和 script:
之前或之后在扩展作业中插入逻辑的另一种方法是使用 !reference
标签。
例如,在before_script:
之后但在script:
之前插入逻辑
another_job:
extends: .deploy
before_script:
- echo "additional steps before before_script:"
- !reference [.deploy, before_script]
- echo "Additional step after before_script"
script:
- echo "Additional steps before script:"
- !reference [.deploy, script]
- echo "Additional steps after script:"
我想创建一个通用的 .deploy
模板。
从该模板继承的每个作业都应该能够在继承模板的脚本之间添加一些自定义script
逻辑。但是怎么办?
示例:
.deploy
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
before_script:
- ...
script:
- touch .env
- echo "SOME_COMMON_KEY=VALUE" >> .env
- $$PSEUDOCODE / PLACE YOUR CUSTOM LOGIC HERE
- ssh -i $SSH_ -o "StrictHostKeyChecking=no" $USER@$SERVER
- scp -i $SSH docker-compose.yml .env $USER@$SERVER:~/test/
- ssh -i $SSH $USER@$SERVER "docker-compose up"...
deploy_test:
extends: .deploy
variables:
USER: test
SERVER: test
script:
#TODO how to add custom statements in place of the $$PSEUDOCODE ?
deploy_prod:
extends: .deploy
variables:
USER: prod
SERVER: prod
script:
#TODO how to add custom statements in place of the $$PSEUDOCODE ?
那么我如何添加一些自定义语句来代替 $$PSEUDOCODE
行?
您能否让您的 2 个作业共享相同的代码?如果是,您可以使用 YAML anchors for script
在你的例子中,它可以给出:
.pseudo_code: &pseudo_code
- echo "Hello $USER on $SERVER"
.deploy:
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
before_script:
- ...
script:
- touch .env
- echo "SOME_COMMON_KEY=VALUE" >> .env
- *pseudo_code
- ssh -i $SSH_ -o "StrictHostKeyChecking=no" $USER@$SERVER
- scp -i $SSH docker-compose.yml .env $USER@$SERVER:~/test/
- ssh -i $SSH $USER@$SERVER "docker-compose up"...
deploy_test:
extends: .deploy
variables:
USER: test
SERVER: test
deploy_prod:
extends: .deploy
variables:
USER: prod
SERVER: prod
您可以做的一件事是在环境变量上使用 eval
。
.deploy:
# ...
variables:
USER_SCRIPT:
value: ""
description: "The contents of this variable will be executed with `eval` during the job script."
script:
- touch .env
- echo "SOME_COMMON_KEY=VALUE" >> .env
- eval "${USER_SCRIPT}"
# ...
用户可以在 before_script
和 script:
之前或之后在扩展作业中插入逻辑的另一种方法是使用 !reference
标签。
例如,在before_script:
之后但在script:
another_job:
extends: .deploy
before_script:
- echo "additional steps before before_script:"
- !reference [.deploy, before_script]
- echo "Additional step after before_script"
script:
- echo "Additional steps before script:"
- !reference [.deploy, script]
- echo "Additional steps after script:"