【问题标题】:How to pass a docker image name to my GitHub action如何将 docker 图像名称传递给我的 GitHub 操作
【发布时间】:2021-09-04 04:19:03
【问题描述】:

我正在尝试在给定的 docker 容器中构建包。我有以下action.yaml 文件:

name: Build Package
description: Build Debian packages using Docker image

inputs:

  docker_image:
    description: Name of the docker image to use
    required: true

runs:
  using: 'composite'

  steps:
    - name: Check out the repository
      uses: actions/checkout@v2

    - name: Build `*.deb` packages
      uses: 'docker://${{inputs.docker_image}}'
      with:
        entrypoint: ./build.sh

在另一个存储库中我正在尝试使用它:

...
    steps:
      - uses: CMakeify-me/build-package-action@v1-beta
        with:
            docker_image: 'cmakeifyme/debian-9-deb-build:1.3'

很遗憾,我遇到了错误:

Error: CMakeify-me/build-package-action/v1-beta/action.yaml (Line: 19, Col: 13):
Error: CMakeify-me/build-package-action/v1-beta/action.yaml (Line: 19, Col: 13): Unrecognized named-value: 'inputs'. Located at position 1 within expression: inputs.docker_image
Error: GitHub.DistributedTask.ObjectTemplating.TemplateValidationException: The template is not valid. CMakeify-me/build-package-action/v1-beta/action.yaml (Line: 19, Col: 13): Unrecognized named-value: 'inputs'. Located at position 1 within expression: inputs.docker_image
   at GitHub.DistributedTask.ObjectTemplating.TemplateValidationErrors.Check()
   at GitHub.Runner.Worker.ActionManifestManager.ConvertRuns(IExecutionContext executionContext, TemplateContext templateContext, TemplateToken inputsToken, String fileRelativePath, MappingToken outputs)
   at GitHub.Runner.Worker.ActionManifestManager.Load(IExecutionContext executionContext, String manifestFile)
Error: Fail to load CMakeify-me/build-package-action/v1-beta/action.yaml

尝试仅打印 inputs 工作正常:

    - name: Spam
      run: echo '${{ inputs.docker_image }}'
      shell: bash

意思是inputs.docker_imageuses:的值内使用时会出现问题;-(

如何传递要在我的操作中使用的 docker 映像名称?

谢谢。

【问题讨论】:

  • 第二个仓库没有定义inputs.docker_image,对吧?这可能是它失败的原因?
  • 第二个 repo 尝试使用该操作并提供with.docker_image - 如果我理解正确,这是将参数传递给操作的方式...
  • 是的,你是对的。

标签: docker github-actions


【解决方案1】:

我认为您不能像这样使用 uses 的输入/变量。

文档中没有明确提及,但您可以在此处看到警告:https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions

  • 警告:在创建工作流和操作时,您应该始终考虑您的代码是否可能执行来自潜在攻击者的不受信任的输入。

如果您需要动态选择您的 docker 容器,您可能需要寻找替代方案。你可以避免uses,直接从shell运行docker,像这样......

调用您的操作,并通过with 传递输入:

      - uses: CMakeify-me/build-package-action@v1-beta
        with:
            docker_image: 'cmakeifyme/debian-9-deb-build:1.3'

然后您的操作可以通过run 输入,使用shell 调用docker:

    - name: run docker with dynamic image name
      run: 'docker run ${{ inputs.docker_image }}'
      shell: bash

【讨论】:

  • 是的,我已经考虑过这种方式......但看起来我必须在“手动”之前执行docker pull(也许是docker login)......它使我很难过 %( -- 如果我必须“手动”执行所有命令,那么 GH 操作中的 docker 支持有什么好处 %(
  • 我认为你别无选择。
猜你喜欢
  • 2021-10-03
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 2021-01-05
  • 2021-11-06
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多