簡體   English   中英

如何在 github-actions 中使用變量 docker 圖像?

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

我正在嘗試編寫一個自定義 github 操作,該操作在 docker 容器中運行一些命令,但允許用戶使用 select 其中 docker 容器它們可以在不同版本的運行時運行 IDA4E6Z

我的直覺是將我的.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 ##[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" ]

有趣的方法! 我不確定是否可以在動作元數據的image字段中使用表達式。 我猜想唯一可以采用表達式而不是硬編碼字符串的字段是圖像的args ,以便可以傳遞inputs

作為參考,這是action.yml元數據的args部分。 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

參考: 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

除了@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

這是另一種方法。 要使用的 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中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM