【问题标题】:How to use a variable docker image in github-actions?如何在 github-actions 中使用变量 docker 图像?
【发布时间】:2020-02-16 22:46:11
【问题描述】:

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


    【解决方案1】:

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

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

    我认为还有其他方法可以实现您想做的事情。您是否尝试过使用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

    或者,您也可以使用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"
    

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

    【讨论】:

      【解决方案2】:

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

      这有助于解决 3 件事:

      • 重用在测试操作的步骤中构建的自定义 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
      

      【讨论】:

        【解决方案3】:

        这是另一种方法。要使用的 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

        【讨论】:

          猜你喜欢
          • 2023-02-21
          • 2022-11-18
          • 2022-01-20
          • 2020-04-20
          • 2023-02-17
          • 2023-03-21
          • 2022-11-28
          • 2021-01-01
          • 2019-12-24
          相关资源
          最近更新 更多