From e07723d66df778cef5fef88a4c82f4d7ba550535 Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Thu, 9 Feb 2023 14:27:08 -0600 Subject: [PATCH] Add caching and log artifacts to GitHub actions Signed-off-by: Michael Carroll --- .github/actions/bazel-ci-jammy/Dockerfile | 10 +++ .github/actions/bazel-ci-jammy/entrypoint.sh | 2 +- .github/actions/bazel-ci-jammy/run.sh | 71 +++++++++++--------- .github/workflows/ci.yml | 29 +++++++- 4 files changed, 75 insertions(+), 37 deletions(-) diff --git a/.github/actions/bazel-ci-jammy/Dockerfile b/.github/actions/bazel-ci-jammy/Dockerfile index 25ba350..12b5e40 100644 --- a/.github/actions/bazel-ci-jammy/Dockerfile +++ b/.github/actions/bazel-ci-jammy/Dockerfile @@ -1,6 +1,16 @@ FROM ghcr.io/gazebo-tooling/gz-ubuntu:garden-jammy +ARG UID=1000 + +# Add a runner user and group to match github actions +# Additionally, use passwordless sudo +RUN groupadd -g 121 runner \ + && useradd -mr -d /home/runner -u 1001 -g 121 runner \ + && usermod -aG sudo runner \ + && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers COPY ["run.sh", "/run.sh"] COPY ["entrypoint.sh", "/entrypoint.sh"] +USER runner + ENTRYPOINT ["/entrypoint.sh"] diff --git a/.github/actions/bazel-ci-jammy/entrypoint.sh b/.github/actions/bazel-ci-jammy/entrypoint.sh index d43cecd..268fc7f 100755 --- a/.github/actions/bazel-ci-jammy/entrypoint.sh +++ b/.github/actions/bazel-ci-jammy/entrypoint.sh @@ -1,3 +1,3 @@ #!/bin/sh -l -sudo bash /run.sh $@ +bash /run.sh $@ diff --git a/.github/actions/bazel-ci-jammy/run.sh b/.github/actions/bazel-ci-jammy/run.sh index 57f12ae..a1fcd77 100755 --- a/.github/actions/bazel-ci-jammy/run.sh +++ b/.github/actions/bazel-ci-jammy/run.sh @@ -4,61 +4,66 @@ set -x set -e BAZEL_ARGS=$1 +WORKSPACE=~/gz +BAZEL_CACHE=$GITHUB_WORKSPACE/bazel_cache +BAZEL=$BAZEL_CACHE/bazelisk-linux-amd64 -echo ::group::Install tools: apt -apt update 2>&1 -apt -y install \ - build-essential \ - cppcheck \ - curl \ - git \ - gnupg \ - lsb-release \ - python3-pip \ - wget - -cd "$GITHUB_WORKSPACE" SYSTEM_VERSION=`lsb_release -cs` SOURCE_DEPENDENCIES="`pwd`/.github/ci/dependencies.yaml" SOURCE_DEPENDENCIES_VERSIONED="`pwd`/.github/ci-$SYSTEM_VERSION/dependencies.yaml" -echo ::endgroup:: -echo ::group::Install tools: pip -pip3 install -U pip vcstool colcon-common-extensions -echo ::endgroup:: +cd "$GITHUB_WORKSPACE" -# Install bazelisk -wget https://github.com/bazelbuild/bazelisk/releases/download/v1.16.0/bazelisk-linux-amd64 -mv ./bazelisk-linux-amd64 /usr/bin/bazel -chmod +x /usr/bin/bazel +# Restore cache +if [ ! -d "$BAZEL_CACHE" ]; then + mkdir -p ${BAZEL_CACHE} +fi +rm -rf ~/.cache +ln -sf ${BAZEL_CACHE} ~/.cache + +# Install baselisk if it is not already installed +if [ ! -f ${BAZEL} ]; then + wget https://github.com/bazelbuild/bazelisk/releases/download/v1.16.0/bazelisk-linux-amd64 -O ${BAZEL} +fi +chmod +x ${BAZEL} # Import repos -mkdir -p /gz -cd /gz -cp -R /github/workspace /gz/bazel -vcs import . < /github/workspace/example/bazel.repos +mkdir -p ${WORKSPACE}/bazel +cd ${WORKSPACE} +shopt -s extglob +# Copy relevant bazel files into the build space +cp -R ${GITHUB_WORKSPACE}/!(gz|.git|.github|bazel_cache) ${WORKSPACE}/bazel +vcs import . < ${WORKSPACE}/bazel/example/bazel.repos echo ::group::Install dependencies from binaries EXCLUDE_APT="libignition|libgz|libsdformat|libogre|dart" UBUNTU_VERSION=`lsb_release -cs` ALL_PACKAGES=$( \ sort -u $(find . -iname 'packages-'$UBUNTU_VERSION'.apt' -o -iname 'packages.apt') | grep -Ev $EXCLUDE_APT | tr '\n' ' ') -apt-get install --no-install-recommends --quiet --yes $ALL_PACKAGES +sudo apt install --no-install-recommends --quiet --yes $ALL_PACKAGES echo ::endgroup:: -ln -sf /gz/bazel/example/WORKSPACE.example /gz/WORKSPACE -ln -sf /gz/bazel/example/BUILD.bazel.example /gz/BUILD.bazel -ln -sf /gz/bazel/example/bazelrc.example /gz/.bazelrc -ln -sf /gz/bazel/example/bazelproject.example /gz/.bazelproject +ln -sf ${WORKSPACE}/bazel/example/WORKSPACE.example ${WORKSPACE}/WORKSPACE +ln -sf ${WORKSPACE}/bazel/example/BUILD.bazel.example ${WORKSPACE}/BUILD.bazel +ln -sf ${WORKSPACE}/bazel/example/bazelrc.example ${WORKSPACE}/.bazelrc +ln -sf ${WORKSPACE}/bazel/example/bazelproject.example ${WORKSPACE}/.bazelproject echo ::group::Bazel query -bazel query $BAZEL_ARGS +${BAZEL} query $BAZEL_ARGS echo ::endgroup:: echo ::group::Bazel build -bazel build $BAZEL_ARGS +${BAZEL} build $BAZEL_ARGS echo ::endgroup:: echo ::group::Bazel test -bazel test $BAZEL_ARGS +${BAZEL} test $BAZEL_ARGS echo ::endgroup:: + +echo ::group::Bazel test +ls -la ${WORKSPACE} +ls -la ${WORKSPACE}/bazel-testlogs +ls -la ${BAZEL_CACHE} +echo ::endgroup:: + +cp -RL ${WORKSPACE}/bazel-testlogs ${GITHUB_WORKSPACE}/bazel-testlogs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 937ee33..210fa6c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,13 +7,36 @@ jobs: runs-on: ubuntu-latest name: Ubuntu Jammy CI steps: + - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - - name: Login to GHCR - run: echo ${{ secrets.CR_PAT }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin + - name: Cache bazel + uses: actions/cache@v3 + env: + cache-name: bazel-cache-3 + with: + path: | + ${{ github.workspace }}/bazel_cache + key: cache-bazel-${{ hashFiles('example/bazel.repos') }} + restore-keys: | + cache-bazel- - name: Compile and test uses: ./.github/actions/bazel-ci-jammy with: bazel-args: //... + + - name: 'Upload Test Logs' + uses: actions/upload-artifact@v3 + with: + name: bazel-testlogs + path: ${{ github.workspace }}/bazel-testlogs + retention-days: 5