GitHub Docker动作拉取请求分支

时间:2020-03-13 13:54:45

标签: docker github-actions

我正在尝试使用GitHub的Actions及其对Docker容器的支持来设置一些CI测试。具体来说:

发出拉取请求时,我希望GitHub动作构建一个docker容器,并使用它从发出拉取请求的分支中构建代码。我尝试使用$ GITHUB_REF作为输入传递分支名称。但是,所有获得的entrypoint.sh脚本实际上都是“ $ GITHUB_REF”,而不是解析的分支名称。

以下是相关文件:

name: C/C++ CI docker

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  JTest_job:

    runs-on: ubuntu-latest
    name: Full build with tests

    # Build Docker container and run the entrypoint.sh script in it
    steps:
    - name: Build and run
      id: build_and_run
      uses: faustus123/DockerAction-JANA2@alpha
      with:
        branch: $GITHUB_REF

name: 'DockerAction-JANA2'
description: 'Build JANA2 and run JTest plugin'
inputs:
  branch:  # id of input
    description: 'branch name'
    required: false
    default: 'master'

# This specifies that docker will be used and the Dockerfile that the
# image should be built from. The args section specifies arguments that
# should be passed to the container when it is run (not when the image
# is being built).
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.branch }}
#!/bin/bash
#
# This is run from within the temporary janatest container
# that gets built by GitHub as part of a GitHub Action to test
# pull requests and commits to master.
#
# This builds JANA2 using the branch given as the only argument
# to this script. It also uses the CXX_STANDARD environment variable
# which should be set in the Dockerfile to be consistent with what
# the ROOT version used. (See Dockerfile for details.)
#
# n.b. The JANA software will be installed in /usr and the
# plugins in /plugins. This is in spite of setting the
# CMAKE_INSTALL_PREFIX below.
#

export BRANCH=$1
echo "--- Building JANA for branch $BRANCH --------------"
cd /opt/JANA2
git checkout $BRANCH
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/opt/JANA2/Linux -DCMAKE_CXX_STANDARD=$CXX_STANDARD
make -j8 install
echo "------------------------"


echo "--- JTest --------------"
export JANA_PLUGIN_PATH=/plugins
jana -PPLUGINS=JTest -Pjana:nevents=100
echo "------------------------"

echo "--- tests --------------"
export JANA_PLUGIN_PATH=/plugins
tests
echo "------------------------"

2 个答案:

答案 0 :(得分:0)

使用其他操作时,基本上可以运行其他程序。因此没有外壳程序,因此没有任何内容可以解析$GITHUB_REF env变量并将其解析为分支名称。

with键中,您只能使用${{ ... }}来访问context information and evaluate expressions

github上下文包含您要传递给faustus123/DockerAction-JANA2@alpha操作的ref作为分支参数的输入,因此以下操作可能有效:

...
  steps:
  - name: Build and run
    id: build_and_run
    uses: faustus123/DockerAction-JANA2@alpha
    with:
      branch: ${{ github.head_ref }}

答案 1 :(得分:0)

@banyan给出的引用提供了与@zCHIP答案中给出的类似信息,这是获取参数所需要的。基本上,传递$ {{{github.ref}}。它不包含实际的分支名称,而是一个引用,例如“ refs / pull / 61 / head”。然后,我的entrypoint.sh脚本需要将其提取到本地的一个分支中,然后可以将其检出。

尽管如此,但我注意到运行docker容器时,GitHub确实传入了GITHUB_REF环境变量,这实际上意味着我不必自己将其作为参数传递。我整理了一下文件,这是我的最终版本,在其他人尝试这样做的情况下,它似乎可以正常工作。

这是我要在其中实施CI的实际项目中的动作脚本ccpp-docker.yml:

name: C/C++ CI docker

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  JTest_job:

    runs-on: ubuntu-latest
    name: Full build with tests

    # Build Docker container and run the entrypoint.sh script in it
    steps:
    - name: Build and run
      id: build_and_run
      uses: faustus123/DockerAction-JANA2@alpha

这是我的自定义操作中的action.yml脚本,该脚本托管在专用的faustus123 / DockerAction-JANA2存储库中。

#
# This specifies a custom GitHub action that is used to
# build a Docker image as part of the JANA2 CI.
#


name: 'DockerAction-JANA2'
description: 'Build JANA2 and run JTest plugin'

# This specifies that docker will be used and the Dockerfile that the
# image should be built from.
runs:
  using: 'docker'
  image: 'Dockerfile'

这是专用于faustus123 / DockerAction-JANA2存储库中托管的entrypoint.sh脚本。

#!/bin/bash
#
# This is run from within the temporary janatest container
# that gets built by GitHub as part of a GitHub Action to test
# pull requests and commits to master.
#
# This builds JANA2 using the branch given by the GITHUB_REF
# environment variable (passed in by GitHub when container
# is run). What actually gets passed is not the branch
# name, but a repository reference from GITHUB_REF that looks
# something like "refs/pull/61/merge". This gets fetched from the
# origin into a branch named "CI" which is then checked out.
#
# This also uses the CXX_STANDARD environment variable
# which should be set in the Dockerfile to be consistent with
# whatever the ROOT version used. (See Dockerfile for details.)


echo "--- Checking out JANA for $GITHUB_REF --------------"
cd /opt/JANA2
git fetch origin ${GITHUB_REF}:CI
git checkout CI

echo "--- Building JANA ------------------------------"
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/opt/JANA2/Linux -DCMAKE_CXX_STANDARD=$CXX_STANDARD
make -j8 install

echo "--- Setting up JANA environment ----------------"
export PATH=/opt/JANA2/Linux/bin:${PATH}
export JANA_PLUGIN_PATH=/opt/JANA2/Linux/plugins
echo "PATH=${PATH}"
echo "JANA_PLUGIN_PATH=${JANA_PLUGIN_PATH}"

echo "--- Running JTest plugin -----------------------"
jana -PPLUGINS=JTest -Pjana:nevents=100

echo "--- Running janatests ------------------------------"
janatests

echo "--- Done ---------------------------------------"