将 Git 分支名称传递给 Concourse 任务

时间:2021-01-27 23:24:44

标签: concourse

我正在寻找一种方法来获取要在脚本中使用的 git 分支名称。 head_name 为我提供了我正在寻找的分支的名称,但我不确定如何将该参数传递给任务。

enter image description here

1 个答案:

答案 0 :(得分:0)

您无法直接访问 get 容器中的元数据。

git 资源在容器中执行 git clone,因此您可以访问数据。如果您的任务映像具有 git CLI,我将只运行 git branch --show-current(来自 https://stackoverflow.com/a/6245587/4462517)。

如果您的任务映像没有 git 并且您不能/不想安装它,您可以在当前任务之前添加这样的任务:

- task: get-git-branch
  config:
    platform: linux
    image_resource:
      type: registry-image
      source:
        repository: concourse/git-resource
    inputs:
    - name: source # or whatever your git resource is named
    outputs:
    - name: branch-info
    run:
      path: bash
      args:
      - -c
      - |
        cd source
        git branch --show-current > ../branch-info/current-branch

从那里,您可以将 branch-info 输出添加为任务的输入,并读取 branch-info/current-branch 的内容以使用它。