为复合GitHub Action中的Docker容器提供参数。

7
我正试图传递一些动态创建的参数到一个组合GitHub Action中。
然而,文档缺乏如何将参数传递给docker容器的示例。
请参阅此处我要实现的内容:https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsstepsuses
runs:
  using: 'composite'
  steps:
    - name: compose arguments
      id: compose-args
      shell: bash
      run: |
        encoded_github="$(echo '${{ inputs.github_context }}' | base64)"
        encoded_runner="$(echo '${{ inputs.runner_context }}' | base64)"

        args=('${{ inputs.command }}')
        args+=('${{ inputs.subcommand }}')
        args+=('--github-context')
        args+=("${encoded_github}")
        args+=('--runner-context')
        args+=("${encoded_runner}")
        args+=('${{ inputs.arguments }}')

        echo "::set-output name=provenance_args::$(echo "[$(printf "\"%s\"," ${args[*]})]" | sed 's/,]$/]/')"
    - name: Debug arguments
      shell: bash
      run: |
        echo Running slsa-provenance with following arguments
        echo ${{ steps.compose-args.outputs.provenance_args }}
    - uses: 'docker://ghcr.io/philips-labs/slsa-provenance:v0.5.0-draft'
      with:
        args: ${{ fromJSON(steps.compose-args.outputs.provenance_args) }}

fromJSON会从Bash参数的组合数组中返回一个JSON对象。我假定这个uses: 'docker://…部分应该像基于Docker的操作一样接收参数。

例如:

runs:
  using: 'docker'
  image: 'docker://ghcr.io/philips-labs/slsa-provenance:v0.4.0'
  args:
    - "generate"
    - '${{ inputs.subcommand }}'
    - "-artifact_path"
    - '${{ inputs.artifact_path }}'
    - "-output_path"
    - '${{ inputs.output_path }}'
    - "-github_context"
    - '${{ inputs.github_context }}'
    - "-runner_context"
    - '${{ inputs.runner_context }}'
    - "-tag_name"
    - '${{ inputs.tag_name }}'

很不幸,在GitHub Actions工作流程中,我遇到了以下错误。

The template is not valid. philips-labs/slsa-provenance-action/v0.5.0-draft/action.yaml (Line: 47, Col: 15): A sequence was not expected

请查看工作流程。 https://github.com/philips-labs/slsa-provenance-action/runs/4618706311?check_suite_focus=true

  • 我该如何解决这个错误?
  • 当前的方法可以解决吗?
  • 这是一个缺失的功能吗?
  • 是否有其他替代方案?

你有没有找到这个问题的答案?文档仍然没有说明如果提供 uses: docker://... 会发生什么。 - Alexis
2个回答

0

看一下这个 GitHub Action https://github.com/mr-smithers-excellent/docker-build-push

它有一个名为buildArgs的输入,因此它可以成为您的解决方案

例如:

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

  - uses: mr-smithers-excellent/docker-build-push@v5
    name: Build & push Docker image
    with:
      image: repo/image
      tags: v1, latest
      registry: registry-url.io
      dockerfile: ./your/path/Dockerfile
      buildArgs: Test=true

-2

我认为你可以通过bash命令来运行:

- name: pull image
  shell: bash
  run: |
    docker pull ghcr.io/philips-labs/slsa-provenance:v0.4.0

- name: run container
  shell: bash
  run: |
    docker run --rm -i \
    --workdir /github/workspace \
    -v "/var/run/docker.sock":"/var/run/docker.sock" \
    -v ${{ runner.temp }}/_github_home:"/github/home" \
    -v ${{ github.workflow }}:"/github/workflow" \
    -v ${{ runner.temp }}/_runner_file_commands:"/github/file_commands" \
    -v ${{ github.workspace }}:"/github/workspace" \
    ghcr.io/philips-labs/slsa-provenance:v0.4.0 \ # image
    generate \ # start pass the args here, after the image
    ${{ inputs.subcommand }} \
    -artifact_path ${{ inputs.artifact_path }} \
    -output_path ${{ inputs.output_path }} \
    -github_context ${{ inputs.github_context }} \
    -runner_context ${{ inputs.runner_context }} \
    -tag_name ${{ inputs.tag_name }} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接