Skip to content

Commit

Permalink
No longer wrap and run ct in Docker container (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
unguiculus authored Nov 3, 2020
1 parent c4c1963 commit 7800dec
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 203 deletions.
59 changes: 35 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
# *chart-testing* Action

A GitHub Action to lint and test Helm charts, using the [helm/chart-testing](https://github.com/helm/chart-testing) CLI tool.

`master` supports Helm 3 only.
Support for Helm 2 is on branch `dev-v2`.
A GitHub Action for installing the [helm/chart-testing](https://github.com/helm/chart-testing) CLI tool.

## Usage

### Pre-requisites

1. A GitHub repo containing a directory with your Helm charts (eg: `/charts`)
1. Optional: if you want to override the defaults, a [chart-testing config file](https://github.com/helm/chart-testing#configuration) in your GitHub repo (eg. `/ct.yaml`)
1. A workflow YAML file in your `.github/workflows` directory. An [example workflow](#example-workflow) is available below.
1. A GitHub repo containing a directory with your Helm charts (e.g: `charts`)
1. A workflow YAML file in your `.github/workflows` directory.
An [example workflow](#example-workflow) is available below.
For more information, reference the GitHub Help Documentation for [Creating a workflow file](https://help.github.com/en/articles/configuring-a-workflow#creating-a-workflow-file)

### Inputs

For more information on inputs, see the [API Documentation](https://developer.github.com/v3/repos/releases/#input)

- `image`: The chart-testing Docker image to use (default: `quay.io/helmpack/chart-testing:v3.2.0`)
- `config`: The path to the config file
- `command`: The chart-testing command to run
- `kubeconfig`: The path to the kube config file
- `docker_args`: Additional arguments which should be passed to docker when starting the ct container
- `version`: The chart-testing version to install (default: `v3.2.0`)

### Example Workflow

Create a workflow (eg: `.github/workflows/lint-test.yaml`):

Note that Python must be installed as shown below because the action also installs [Yamale](https://github.com/23andMe/Yamale) and [yamllint](https://github.com/adrienverge/yamllint) which require Python.

```yaml
name: Lint and Test Charts

Expand All @@ -39,28 +34,44 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- uses: actions/setup-python@v2
with:
python-version: 3.7

- name: Fetch history
run: git fetch --prune --unshallow
- name: Set up chart-testing
uses: helm/[email protected]

- name: Run chart-testing (list-changed)
id: list-changed
run: |
changed=$(ct list-changed)
if [[ -n "$changed" ]]; then
echo "::set-output name=changed::true"
fi
- name: Run chart-testing (lint)
id: lint
uses: helm/[email protected]
with:
command: lint
run: ct lint

- name: Create kind cluster
uses: helm/[email protected]
# Only build a kind cluster if there are chart changes to test.
if: steps.lint.outputs.changed == 'true'
if: steps.list-changed.outputs.changed == 'true'

- name: Run chart-testing (install)
uses: helm/[email protected]
with:
command: install
run: ct install
```
This uses [`@helm/kind-action`](https://www.github.com/helm/kind-action) GitHub Action to spin up a [kind](https://kind.sigs.k8s.io/) Kubernetes cluster, and [`@helm/chart-testing-action`](https://www.github.com/helm/chart-testing-action) to lint and test your charts on every Pull Request.
This uses [`helm/kind-action`](https://www.github.com/helm/kind-action) GitHub Action to spin up a [kind](https://kind.sigs.k8s.io/) Kubernetes cluster,
and [`helm/chart-testing`](https://www.github.com/helm/chart-testing) to lint and test your charts on every pull request.

## Upgrading from v1.x.x

v2.0.0 is a major release with breaking changes.
The action no longer wraps the chart-testing tool but simply installs it.
It is no longer run in a Docker container.
All `ct` options are now directly available without the additional abstraction layer.

## Code of conduct

Expand Down
25 changes: 8 additions & 17 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
name: "Helm Chart Testing"
description: "Run the Helm chart-testing tool"
description: "Install the Helm chart-testing tool"
author: "The Helm authors"
branding:
color: blue
icon: anchor
inputs:
image:
description: "The chart-testing Docker image to use (default: quay.io/helmpack/chart-testing:v3.2.0)"
config:
description: "The relative path to the chart-testing config file"
command:
description: "The chart-testing command to run"
required: true
docker_args:
description: "Additional arguments which should be passed to docker when starting the ct container"
kubeconfig:
description: "The path to the kube config file"
outputs:
changed:
description: "Whether or not charts have changed (true/false)"
version:
description: "The chart-testing version to install (default: v3.2.0)"
default: v3.2.0
runs:
using: "node12"
main: "main.js"
using: composite
steps:
- run: "$GITHUB_ACTION_PATH/ct.sh --version ${{ inputs.version }}"
shell: bash
154 changes: 42 additions & 112 deletions ct.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,23 @@ set -o errexit
set -o nounset
set -o pipefail

DEFAULT_IMAGE=quay.io/helmpack/chart-testing:v3.2.0
DEFAULT_CHART_TESTING_VERSION=v3.2.0

show_help() {
cat << EOF
Usage: $(basename "$0") <options>
-h, --help Display help
-i, --image The chart-testing Docker image to use (default: ${DEFAULT_IMAGE})
-c, --command The chart-testing command to run
--config The path to the chart-testing config file
--kubeconfig The path to the kube config file
--docker-args Additional arguments which should be passed to docker when starting the ct container
-v, --version The chart-testing version to use (default: $DEFAULT_CHART_TESTING_VERSION)"
EOF
}

main() {
local image="$DEFAULT_IMAGE"
local config=
local command=
local kubeconfig="$HOME/.kube/config"
local docker_args=()
local version="$DEFAULT_CHART_TESTING_VERSION"

parse_command_line "$@"

if [[ -z "$command" ]]; then
echo "ERROR: '-c|--command' is required." >&2
show_help
exit 1
fi

run_ct_container
trap cleanup EXIT

local changed
changed=$(docker_exec ct list-changed)
if [[ -z "$changed" ]]; then
echo 'No chart changes detected.'
echo "::set-output name=changed::false"
return
fi

# Convenience output for other actions to make use of ct config to check if
# charts changed.
echo "::set-output name=changed::true"

# All other ct commands require a cluster to be created in a previous step.
if [[ "$command" != "lint" ]] && [[ "$command" != "list-changed" ]]; then
configure_kube
fi

run_ct
install_chart_testing
}

parse_command_line() {
Expand All @@ -64,52 +30,12 @@ parse_command_line() {
show_help
exit
;;
-i|--image)
if [[ -n "${2:-}" ]]; then
image="$2"
shift
else
echo "ERROR: '-i|--image' cannot be empty." >&2
show_help
exit 1
fi
;;
-c|--command)
if [[ -n "${2:-}" ]]; then
command="$2"
shift
else
echo "ERROR: '-c|--command' cannot be empty." >&2
show_help
exit 1
fi
;;
--config)
if [[ -n "${2:-}" ]]; then
config="$2"
shift
else
echo "ERROR: '--config' cannot be empty." >&2
show_help
exit 1
fi
;;
--kubeconfig)
if [[ -n "${2:-}" ]]; then
kubeconfig="$2"
shift
else
echo "ERROR: '--kubeconfig' cannot be empty." >&2
show_help
exit 1
fi
;;
--docker-args)
-v|--version)
if [[ -n "${2:-}" ]]; then
IFS=" " read -r -a docker_args <<< "$2"
version="$2"
shift
else
echo "ERROR: '--docker-args' cannot be empty." >&2
echo "ERROR: '-v|--version' cannot be empty." >&2
show_help
exit 1
fi
Expand All @@ -123,45 +49,49 @@ parse_command_line() {
done
}

run_ct_container() {
echo 'Running ct container...'
local args=(run --rm --interactive --detach --network host --name ct "--volume=$(pwd):/workdir" "--workdir=/workdir")

if [[ -n "$config" ]]; then
args+=("--volume=$(pwd)/$config:/etc/ct/ct.yaml" )
install_chart_testing() {
if [[ ! -d "$RUNNER_TOOL_CACHE" ]]; then
echo "Cache directory '$RUNNER_TOOL_CACHE' does not exist" >&2
exit 1
fi

args=("${args[@]}" "${docker_args[@]}")
local arch
arch=$(uname -m)

args+=("$image" cat)
local cache_dir="$RUNNER_TOOL_CACHE/ct/$version/$arch"
if [[ ! -d "$cache_dir" ]]; then
mkdir -p "$cache_dir"

echo docker "${args[@]}"
docker "${args[@]}"
echo
}
echo "Installing chart-testing..."
curl -sSLo ct.tar.gz "https://github.com/helm/chart-testing/releases/download/$version/chart-testing_${version#v}_linux_amd64.tar.gz"
tar -xzf ct.tar.gz -C "$cache_dir"
rm -f ct.tar.gz

configure_kube() {
# need to copy full .kube dir for certs, etc:
local confdir
confdir=$(dirname "$kubeconfig")
docker cp "$confdir" ct:/root/.kube
}
echo 'Adding ct directory to PATH...'
echo "$cache_dir" >> "$GITHUB_PATH"

run_ct() {
echo "Running 'ct $command'..."
docker_exec ct "$command"
echo
}
echo 'Setting CT_CONFIG_DIR...'
echo "CT_CONFIG_DIR=$cache_dir/etc" >> "$GITHUB_ENV"

cleanup() {
echo 'Removing ct container...'
docker kill ct > /dev/null 2>&1
echo 'Done!'
}
local venv_dir="$cache_dir/venv"

echo 'Creating virtual Python environment...'
python3 -m venv "$venv_dir"

echo 'Activating virtual environment...'
# shellcheck disable=SC1090
source "$venv_dir/bin/activate"

docker_exec() {
echo docker exec --interactive ct "$@"
docker exec --interactive ct "$@"
echo 'Installing yamllint...'
pip3 install yamllint==1.25.0

echo 'Installing Yamale...'
pip3 install yamale==3.0.4

echo 'Configuring environment variables for virtual environment for subsequent workflow steps...'
echo "VIRTUAL_ENV=$venv_dir" >> "$GITHUB_ENV"
echo "$venv_dir/bin" >> "$GITHUB_PATH"
fi
}

main "$@"
19 changes: 0 additions & 19 deletions main.js

This file was deleted.

31 changes: 0 additions & 31 deletions main.sh

This file was deleted.

0 comments on commit 7800dec

Please sign in to comment.