[分享]如何在流水线模板文件使用input变量

在一些情况下,极狐GitLab的用户会自行定义流水线的模板文件,并在CI/CD中使用这些模板,但是当我们需要为模板文件输入变量时,通常只能通过全局变量来操作。

当我们不希望用全局变量时,可以使用input关键字为模板文件提供变量,该方式比全局变量更加灵活:

参考:Use CI/CD configuration from other files | GitLab

在项目中定义template文件:

# my-template.yml

spec:
  inputs:
    website:
    stage:
      default: deploy
---
deploy:
  stage: $[[ inputs.stage ]]
  script: echo "deploy $[[ inputs.website ]]"

使用input关键字定义变量:

# .gitlab-ci.yml

include:
  - local: my-template.yml
    inputs:
      stage: deploy
      website: my-website.example.com

stages:
 - test
 - deploy

test:
  stage: test
  script: echo "test"