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

时间:2019-10-20 18:52:40

标签: docker github-actions

我正在尝试编写一个自定义的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" ]

3 个答案:

答案 0 :(得分:4)

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

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

我认为还有其他方法可以实现您想要的目标。您是否尝试过使用jobs.<job_id>.container语法?这样,您就可以指定要运行作业步骤的映像。但是,这要求您将映像发布到公共存储库。因此请注意不要包含任何秘密。

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

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

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

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

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"

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

答案 1 :(得分:1)

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

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 中。

答案 2 :(得分:0)

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

这有助于解决三件事:

  • 在测试操作的步骤中重复使用正在构建的自定义docker映像。用uses似乎不可能这样做,因为它首先尝试在作业的任何步骤之前发生的Setup 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