Skip to content

Commit

Permalink
workflows/e2e: Use local envtest github action
Browse files Browse the repository at this point in the history
Use local actions/envtest to setup envtest.

The envtest action downloads the envtest binaries for a given version
inside a container and moves them to the host. The binaries also
include the setup-envtest binary. The env var KUBEBUILDER_ASSETS is
also set to the directory containing the envtest binaries.

The cache in /github/home/.cache populated when installing setup-envtest
causes the builds to fail in `make test` target due to the cached go
build files, maybe due to some incompatibilities. Deleting them at the
end of the action helps avoid affecting other build steps in the
workflow.

Since the run-test action runs in a container, the host $PATH is not
passed to the test container. The ENV directive in Dockerfile is used to
append the PATH with envtest binary path. This helps avoid redownload of
the envtest binaries when it's not found in the path via the Makefile
targets.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Jul 31, 2021
1 parent 4ce7c0d commit c975d37
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .github/actions/envtest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM golang:1.16-buster

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
16 changes: 16 additions & 0 deletions .github/actions/envtest/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Setup envtest
description: A GitHub Action for setting up controller-runtime envtest
author: Flux authors
branding:
color: blue
icon: command
inputs:
version:
description: 'Kubernetes version'
required: false
default: "latest"
runs:
using: docker
image: Dockerfile
args:
- ${{ inputs.version }}
25 changes: 25 additions & 0 deletions .github/actions/envtest/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -eu

# envtest kubernetes version.
VERSION=${1:-latest}

go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
setup-envtest use $VERSION

ENVTEST_DIR=$(setup-envtest use -i $VERSION -p path)
SETUP_ENVTEST_PATH=$(which setup-envtest)

mkdir -p $GITHUB_WORKSPACE/envtest
mv $ENVTEST_DIR/* $GITHUB_WORKSPACE/envtest
mv $SETUP_ENVTEST_PATH $GITHUB_WORKSPACE/envtest
ls -lh $GITHUB_WORKSPACE/envtest

echo "$GITHUB_WORKSPACE/envtest" >> $GITHUB_PATH
echo "$RUNNER_WORKSPACE/$(basename $GITHUB_REPOSITORY)/envtest" >> $GITHUB_PATH
echo "KUBEBUILDER_ASSETS=$GITHUB_WORKSPACE/envtest" >> $GITHUB_ENV

# Cleanup cache to avoid affecting other builds. Some go builds may get
# affected by this cache populated in the above operations.
rm -rf /github/home/.cache
3 changes: 3 additions & 0 deletions .github/actions/run-tests/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ RUN set -eux; \
RUN groupadd -g 116 test && \
useradd -u 1001 --gid test --shell /bin/sh --create-home test

# Set path to envtest binaries.
ENV PATH="/github/workspace/envtest:${PATH}"

# Run as test user
USER test

Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ jobs:
image: kindest/node:v1.21.1@sha256:69860bda5563ac81e3c0057d654b5253219618a22ec3a346306239bba8cfa1a6
- name: Setup Kustomize
uses: fluxcd/pkg/actions/kustomize@main
- name: Setup Kubebuilder
uses: fluxcd/pkg/actions/kubebuilder@main
- name: Setup envtest
uses: ./.github/actions/envtest
with:
version: "1.19.2"
- name: Setup Helm
uses: fluxcd/pkg/actions/helm@main
- name: Run tests
uses: ./.github/actions/run-tests
env:
GOPATH: /github/home/go
KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin
- name: Check if working tree is dirty
run: |
if [[ $(git diff --stat) != '' ]]; then
Expand All @@ -44,14 +45,10 @@ jobs:
fi
- name: Build container image
run: make docker-build IMG=test/source-controller:latest
env:
KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin
- name: Load test image
run: kind load docker-image test/source-controller:latest
- name: Deploy controller
run: make dev-deploy IMG=test/source-controller:latest
env:
KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin
- name: Run smoke tests
run: |
kubectl -n source-system apply -f ./config/samples
Expand Down

0 comments on commit c975d37

Please sign in to comment.