From 8824ecb28836891c155b9f792e763eb698a592f6 Mon Sep 17 00:00:00 2001
From: Michael Carroll <michael@openrobotics.org>
Date: Thu, 9 Feb 2023 14:27:08 -0600
Subject: [PATCH] Bump checkout and add caching

Signed-off-by: Michael Carroll <michael@openrobotics.org>
---
 .github/actions/bazel-ci-jammy/Dockerfile    | 10 ++++
 .github/actions/bazel-ci-jammy/entrypoint.sh |  2 +-
 .github/actions/bazel-ci-jammy/run.sh        | 48 ++++++++++++--------
 .github/workflows/ci.yml                     | 19 +++++++-
 4 files changed, 59 insertions(+), 20 deletions(-)

diff --git a/.github/actions/bazel-ci-jammy/Dockerfile b/.github/actions/bazel-ci-jammy/Dockerfile
index 25ba350..1975445 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
 
 COPY ["run.sh", "/run.sh"]
 COPY ["entrypoint.sh", "/entrypoint.sh"]
 
+# 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
+
+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..71da610 100755
--- a/.github/actions/bazel-ci-jammy/run.sh
+++ b/.github/actions/bazel-ci-jammy/run.sh
@@ -4,10 +4,13 @@ set -x
 set -e
 
 BAZEL_ARGS=$1
+WORKSPACE=$GITHUB_WORKSPACE/gz
+BAZEL_CACHE=$GITHUB_WORKSPACE/bazel_cache
+BAZEL=~/.cache/bazelisk-linux-amd64
 
 echo ::group::Install tools: apt
-apt update 2>&1
-apt -y install \
+sudo apt update 2>&1
+sudo apt -y install \
   build-essential \
   cppcheck \
   curl \
@@ -27,38 +30,47 @@ echo ::group::Install tools: pip
 pip3 install -U pip vcstool colcon-common-extensions
 echo ::endgroup::
 
-# 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
+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) ${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::
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 937ee33..14abe40 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -7,13 +7,30 @@ jobs:
     runs-on: ubuntu-latest
     name: Ubuntu Jammy CI
     steps:
+
       - name: Checkout
-        uses: actions/checkout@v2
+        uses: actions/checkout@v3
 
       - 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-1
+        with:
+          path: |
+            ${{ github.workspace }}/bazel_cache
+          key: ${{ runner.os }}-${{ env.cache-name }}
+
       - 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 }}/gz/bazel-testlogs
+          retention-days: 5