【问题标题】:GitHub Docker action pull request branchGitHub Docker 操作拉取请求分支
【发布时间】:2020-06-25 13:04:37
【问题描述】:

我正在尝试使用 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 "------------------------"

【问题讨论】:

标签: docker github-actions


【解决方案1】:

当你使用另一个动作时,你基本上是在运行一个不同的程序。所以没有shell,所以什么都不能解析$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 }}

【讨论】:

    【解决方案2】:

    @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
    

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

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

    【讨论】:

      猜你喜欢
      • 2020-01-30
      • 2021-09-24
      • 2022-08-13
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      相关资源
      最近更新 更多