如何在github-actions中使用变量docker镜像?

How to use a variable docker image in github-actions?

提问人:gowerc 提问时间:10/21/2019 更新时间:1/6/2021 访问量:6340

问:

我正在尝试编写一个自定义的 github-action,它在 docker 容器中运行一些命令,但允许用户选择它们在哪个 docker 容器中运行(即,这样我就可以在不同版本的运行时环境中运行相同的构建指令)

我的直觉是把我的文件作为.github/actions/main/action.yml

name: 'Docker container command execution'
inputs:
  dockerfile:
    default: Dockerfile_r_latest
runs:
  using: 'docker' 
  image: '${{ inputs.dockerfile }}'
  args:
   - /scripts/commands.sh

但是,此错误包括:##[error](Line: 7, Col: 10): Unrecognized named-value: 'inputs'. Located at position 1 within expression: inputs.dockerfile

任何帮助将不胜感激!

文件引用

我的文件是:.github/workflow/build_and_test.yml

name: Test Package

on: 
  [push, pull_request]

jobs:

  R_latest:

    name: Test on latest
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
        name: Checkout project

      - uses: ./.github/actions/main
        name: Build and test
        with:
          dockerfile: Dockerfile_r_latest

我的 Dockerfile 是:.github/actions/main/Dockerfile_r_latest

FROM rocker/verse:latest
ADD scripts /scripts
ENTRYPOINT [ "bash", "-c" ]
docker github-actions

评论


答:

5赞 peterevans 10/21/2019 #1

有趣的方法!我不确定是否可以在操作元数据字段中使用表达式。我猜唯一可以采用表达式而不是硬编码字符串的字段是图像,以便可以传递。imageargsinputs

作为参考,这是元数据的部分。https://help.github.com/en/articles/metadata-syntax-for-github-actions#argsargsaction.yml

我认为还有其他方法可以实现你想做的事情。你试过使用语法吗?这允许您指定作业步骤将在其中运行的映像。但是,这需要您将映像发布到公共存储库。所以要注意不要包含任何秘密。jobs.<job_id>.container

例如,如果在工作流中将映像发布到 Docker Hub,则可能如下所示:gowerc/r-latest

name: Test Package

on: 
  [push, pull_request]

jobs:

  R_latest:

    name: Test on latest
    runs-on: ubuntu-latest
    container: gowerc/r-latest
    steps:
      - uses: actions/checkout@master
        name: Checkout project

      - name: Build and test
        run: ./scripts/commands.sh

编号: https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idcontainer

或者,您也可以在步骤级别使用 指定图像。然后,您可以传递命令 via 来执行脚本。usesargs

name: my workflow
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Check container
        uses: docker://alpine:3.8
        with:
          args: /bin/sh -c "cat /etc/alpine-release"

编号: https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#example-using-a-docker-hub-action

2赞 mehdio 7/12/2020 #2

除了@peterevans答案之外,我还要补充第三个选项,您可以在其中使用简单的命令并传递您定义的任何命令。docker runenv

这有助于解决 3 件事:

  • 在测试操作的步骤中重复使用正在构建的自定义 docker 映像。似乎不可能这样做,因为它首先尝试在作业的任何步骤之前发生的步骤中提取尚不存在的图像。usesSetup job
  • 此特定映像也可以存储在私有 docker 注册表中
  • 能够将变量用于 docker 映像

我的工作流程如下所示:

name: Build-Test-Push
on:
push:
    branches:
    - master
env:
    AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
    AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    ECR_REGISTRY: ${{ secrets.AWS_ECR_REGISTRY }}
    ECR_REPOSITORY: myproject/myimage
    IMAGE_TAG: ${{ github.sha }}

jobs:

build-and-push:
    runs-on: ubuntu-latest
    steps:
    - name: Checking out
    uses: actions/checkout@v2
    with:
        ref: master

    - name: Login to AWS ECR
    id: login-ecr
    uses: aws-actions/amazon-ecr-login@v1

    - name: Build
    run: |
        docker pull $ECR_REGISTRY/$ECR_REPOSITORY || true
        docker build . -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest

    - name: Test
    run: |
        docker run $ECR_REGISTRY/$ECR_REPOSITORY:latest /bin/bash -c "make test"

    - name: Push
    run: |
        docker push $ECR_REGISTRY/$ECR_REPOSITORY
1赞 Stephen G Tuggy 1/6/2021 #3

这是另一种方法。要使用的 Docker 映像将传递给负责拉取正确映像的 shell 脚本。cibuild

GitHub 工作流文件:

name: 'GH Actions CI'

on:
  push:
    branches: ['*master', '*0.[0-9]?.x']
  pull_request:
    # The branches below must be a subset of the branches above
    branches: ['*master', '*0.[0-9]?.x']

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest

    strategy:
      fail-fast: true
      matrix:
        include:
          - FROM:     'ubuntu:focal'
          - FROM:     'ubuntu:bionic'
          - FROM:     'ubuntu:xenial'
          - FROM:     'debian:buster'
          - FROM:     'debian:stretch'
          - FROM:     'opensuse/leap'
          - FROM:     'fedora:33'
          - FROM:     'fedora:32'
          - FROM:     'centos:8'

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2
      with:
        # We must fetch at least the immediate parents so that if this is
        # a pull request then we can checkout the head.
        fetch-depth: 2

    # If this run was triggered by a pull request event, then checkout
    # the head of the pull request instead of the merge commit.
    - run: git checkout HEAD^2
      if: ${{ github.event_name == 'pull_request' }}

    - name: Run CI
      env:
        FROM: ${{ matrix.FROM }}
      run: script/cibuild

Bash 脚本:script/cibuild

#!/bin/bash

set -e

docker run --name my-docker-container $FROM script/custom-script.sh
docker cp my-docker-container:/usr/src/my-workdir/my-outputs .
docker rm my-docker-container

echo "cibuild Done!"

将自定义命令放入 中。script/custom-script.sh