GitHub Docker操作拉取请求分支



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

当提出pull请求时,我希望GitHub操作构建一个docker容器,并使用它从发出pull请求的分支构建代码。我已经尝试使用$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 "------------------------"

当您使用另一个操作时,基本上会运行不同的程序。所以没有shell,所以没有什么可以解析$GITHUB_REFenv变量并将其解析为分支名称。

with键中,您只能使用${{ ... }}来访问上下文信息和计算表达式。

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 }}

@banyan给出的参考给出了与@zCHIP的答案中给出的信息类似的信息,这是在参数处需要得到的信息。基本上,通过${{github.ref}}。这不包含实际的分支名称,而是一个类似"refs/pull/61/head"的引用。然后,我的entrypoint.sh脚本需要将其获取到一个分支(本地(中,然后可以签出该分支。

尽管如此,我还是注意到,当docker容器运行时,GitHub确实会传递GitHub_REF环境变量,这实际上意味着我不必麻烦自己将其作为参数传递。我清理了一些文件,这是我的最终版本,如果其他人试图这样做,它似乎可以工作。

这是我实际项目中的动作脚本ccpp-docker.yml,我想为其实现CI:

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脚本,该脚本托管在专用的faust123/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'

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

#!/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 ---------------------------------------"

最新更新