1

I'm trying to set up some CI tests using GitHub's Actions and its support for Docker containers. Specifically:

When a pull request is made, I want the GitHub action to build a docker container and use it build the code from the branch the pull request is being made from. I have tried passing the branch name using $GITHUB_REF as an input. However, all the entrypoint.sh script ever gets is literally "$GITHUB_REF" and never the resolved branch name.

Here are the relevant files:

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 "------------------------"
David L.
  • 209
  • 2
  • 11

2 Answers2

0

When you use another action, you basically run a different program. So there is no shell, so nothing can parse $GITHUB_REF env variable and resolve it to a branch name.

In with key you are limited to use ${{ ... }} to access context information and evaluate expressions.

The github context contains the ref you want to pass to faustus123/DockerAction-JANA2@alpha action as the input for branch parameter, so the following might work:

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

The reference given by @banyan gave similar information as given in @zCHIP 's answer which is what was needed to get at the parameter. Basically, to pass ${{ github.ref }}. This does not contain the actual branch name, but a reference like "refs/pull/61/head". My entrypoint.sh script then needed to fetch this into a branch (locally) that could then be checked out.

Going through this though, I noticed that when the docker container is run, GitHub does pass in the GITHUB_REF environment variable which actually means I don't have to go to the trouble of passing it through as a parameter myself. I cleaned up the files a bit and here are my final version which seem to work in case someone else is trying to do this.

This is the action script ccpp-docker.yml in my actual project that I want to implement CI for:

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

Here is the action.yml script in my custom action that is hosted in the dedicated faustus123/DockerAction-JANA2 repository.

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

Here is the entrypoint.sh script that is hosted in the dedicated faustus123/DockerAction-JANA2 repository.

#!/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 ---------------------------------------"
David L.
  • 209
  • 2
  • 11