Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add action #1

Merged
merged 20 commits into from
Aug 17, 2023
146 changes: 146 additions & 0 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
name: test-action

on:
pull_request:
push:
branches:
- main

jobs:
default:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
permissions: {}
name: Install Kyverno CLI and test presence in path
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Install Kyverno CLI
uses: ./
- name: Check install
run: kyverno version
- name: Check root directory
shell: bash
run: |
if [[ $(git diff --stat) != '' ]]; then
echo 'should be clean'
exit 1
else
exit 0
fi

custom:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
permissions: {}
name: Install Kyverno CLI and test presence in path
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Install Kyverno CLI
uses: ./
with:
release: 'v1.9.5'
- name: Check install
run: kyverno version
- name: Check root directory
shell: bash
run: |
if [[ $(git diff --stat) != '' ]]; then
echo 'should be clean'
exit 1
else
exit 0
fi

wrong_version:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
permissions: {}
name: Try to install a wrong version
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Install Kyverno CLI
uses: ./
with:
release: honk
continue-on-error: true

custom_dir:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
permissions: {}
name: Install Kyverno CLI and test presence in path
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Install Kyverno CLI
uses: ./
with:
install-dir: "$HOME/.kyvernotest"
- name: Check install
run: kyverno version
- name: Check install dir
shell: bash
run: |
[[ $(dirname "$(which kyverno)") == "$HOME/.kyvernotest" ]]
- name: Check root directory
shell: bash
run: |
[[ -z $(git diff --stat) ]]

custom_dir_root:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
permissions: {}
name: Install Kyverno CLI and test presence in path with custom root dir
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Install Kyverno CLI
uses: ./
with:
install-dir: /usr/bin
use-sudo: true
- name: Check install
run: kyverno version
- name: Check install dir
shell: bash
run: |
[[ $(dirname "$(which kyverno)") == /usr/bin ]]
- name: Check root directory
shell: bash
run: |
[[ -z $(git diff --stat) ]]

main:
permissions: {}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest
go_version:
- '1.20'
- '1.21'
name: Try to install with go ${{ matrix.go_version }}
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: ${{ matrix.go_version }}
check-latest: true
- name: Install Kyverno CLI
uses: ./
with:
release: main
- name: Check install
run: kyverno version
159 changes: 159 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# action.yml
name: kyverno-cli-installer
author: kyverno
description: Installs kyverno CLI and includes it in your path
branding:
icon: package
color: orange
# This is pinned to the last major release, we have to bump it for each action version.
inputs:
release:
description: Kyverno CLI release version to be installed
required: false
default: v1.10.3
install-dir:
description: Where to install the kyverno CLI binary
required: false
default: $HOME/.kyverno
use-sudo:
description: Set to true if install-dir location requires sudo privs
required: false
default: 'false'
runs:
using: composite
steps:
- shell: bash
run: |
#!/bin/bash

shopt -s expand_aliases

if [ -z "$NO_COLOR" ]; then
alias log_info="echo -e \"\033[1;32mINFO\033[0m:\""
alias log_error="echo -e \"\033[1;31mERROR\033[0m:\""
else
alias log_info="echo \"INFO:\""
alias log_error="echo \"ERROR:\""
fi

set -e

mkdir -p ${{ inputs.install-dir }}

# main
if [[ ${{ inputs.release }} == "main" ]]; then
log_info "installing via 'go install' from its main version"
GOBIN=$(go env GOPATH)/bin
go install github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno@main
ln -s $GOBIN/kubectl-kyverno ${{ inputs.install-dir}}/kyverno
exit 0
fi

trap "popd >/dev/null" EXIT

pushd ${{ inputs.install-dir }} > /dev/null

case ${{ runner.os }} in
Linux)
case ${{ runner.arch }} in
X64)
release_archive='linux_x86_64.tar.gz'
;;

ARM64)
release_archive='linux_arm64.tar.gz'
;;

*)
log_error "unsupported architecture ${{ runner.arch }}"
exit 1
;;
esac
;;

macOS)
case ${{ runner.arch }} in
X64)
release_archive='darwin_x86_64.tar.gz'
;;

ARM64)
release_archive='darwin_arm64.tar.gz'
;;

*)
log_error "unsupported architecture ${{ runner.arch }}"
exit 1
;;
esac
;;

Windows)
case ${{ runner.arch }} in
X64)
release_archive='windows_x86_64.zip'
;;

*)
log_error "unsupported architecture ${{ runner.arch }}"
exit 1
;;
esac
;;

*)
log_error "unsupported os ${{ runner.os }}"
exit 1
;;
esac

SUDO=
if [[ "${{ inputs.use-sudo }}" == "true" ]] && command -v sudo >/dev/null; then
SUDO=sudo
fi

semver='^v([0-9]+\.){0,2}(\*|[0-9]+)(-?r?c?)(\.[0-9]+)$'
if [[ ${{ inputs.release }} =~ $semver ]]; then
log_info "Custom version '${{ inputs.release }}' requested"
else
log_error "Unable to validate requested version: '${{ inputs.release }}'"
exit 1
fi

release_archive=kyverno-cli_${{ inputs.release }}_${release_archive}
release_archive_url=https://github.com/kyverno/kyverno/releases/download/${{ inputs.release }}/${release_archive}

log_info "Downloading Kyverno CLI version '${{ inputs.release }}'...\n ${release_archive_url}"
$SUDO curl -sL ${release_archive_url} -o ${release_archive}


case ${{ runner.os }} in
Linux)
$SUDO tar -xvf ${release_archive} kyverno
;;

macOS)
$SUDO tar -xvf ${release_archive} kyverno
;;

Windows)
$SUDO unzip ${release_archive} kyverno.exe
;;

*)
log_error "unsupported os ${{ runner.os }}"
exit 1
;;
esac

$SUDO rm ${release_archive}
$SUDO chmod +x kyverno

log_info "Installation complete!"

- if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
run: echo "${{ inputs.install-dir }}" >> $GITHUB_PATH
shell: bash
- if: ${{ runner.os == 'Windows' }}
run: echo "${{ inputs.install-dir }}" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
shell: pwsh