diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d2b3de5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM alpine + +LABEL version="1.0.0" +LABEL name="k8s-toolkit" +LABEL repository="http://github.com/bwvolleyball/k8s-toolkit" +LABEL homepage="http://github.com/bwvolleyball/k8s-toolkit" + +LABEL maintainer="Brandon Ward <@bwvolleyball>" +LABEL com.github.actions.name="Kubernetes Toolkit" +LABEL com.github.actions.description="Configures a kubernetes toolkit. The kube config must be provided. Includes kubernetes cli and helm." +LABEL com.github.actions.icon="terminal" +LABEL com.github.actions.color="blue" + +RUN apk add --no-cache curl + +COPY LICENSE README.md / +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] +CMD ["help"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..063a42e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [2021] [Brandon Ward] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ed512a --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# GitHub Actions Kubernetes Toolkit +> This action started as a fork of [steebchen/kubectl](https://github.com/steebchen/kubectl), but the solution is now very different. +> If you just need to directly run a single kubernetes command, simply use that action. +> +> If instead, you need to run multiple `kubectl` commands, or `helm`, this action installs these tools _and_ configures them, +> so that they can be used throughout the remaining steps in your job. + +This action provides `kubectl` and `helm` for GitHub Actions. + +As with the upstream inspiration for this project, please :star: it if you do end up using it! + +## Usage + +`.github/workflows/push.yml` + +```yaml +on: push +name: deploy +jobs: + deploy: + name: deploy to cluster + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: deploy to cluster + uses: bwvolleyball/k8s-toolkit@v1.0.0 + with: # defaults to latest kubectl & helm binary versions + config: ${{ secrets.KUBE_CONFIG_DATA }} + kubectl_version: v1.21.5 + helm_version: v3.7.0 + # After it's been configured, you can now freely use kubectl & helm commands the rest of the job. + # This could also be used for multi-cluster deployment, as long as your provided kube config has all required clusters. + - name: Check kubectl version + run: kubectl version + - name: Check helm version + run: helm version + - name: Update Deployment + run: set image --record deployment/my-app container=${{ github.repository }}:${{ github.sha }} + - name: Helm Deployment + run: helm upgrade my-release my/chart --namespace namespace + - name: verify deployment + run: helm test my-release --namespace namespace +``` + +## Arguments + +`config` – **required**: A base64-encoded kubeconfig file with credentials for Kubernetes to access the cluster. You can get it by running the following command: + +```bash +cat $HOME/.kube/config | base64 +``` +**Note**: Do not use kubectl config view as this will hide the certificate-authority-data. + +`kubectl_version`: The kubectl version with a 'v' prefix, e.g. `v1.21.0`. It defaults to the latest kubectl binary version available. + +`helm_version`: The helm version with a 'v' prefix, e.g. `v3.7.0`. It defaults to the latest helm binary version available. +Since this is read from the GitHub releases, it could easily be a pre-release binary. You are strongly encouraged to explicitly +set the version for this. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..01ad457 --- /dev/null +++ b/action.yml @@ -0,0 +1,24 @@ +name: 'kubernetes-toolkit' +description: 'configures a kubernetes toolkit for your actions => Includes: kubectl, helm' +runs: + using: 'docker' + image: 'Dockerfile' + args: + - ${{ inputs.kubectl_version }} + - ${{ inputs.config }} + - ${{ inputs.helm_version }} +branding: + icon: 'terminal' + color: 'blue' +inputs: + kubectl_version: + description: 'kubectl version, e.g. `v1.21.0`, defaults to latest' + required: false + default: latest + config: + description: 'kube config data' + required: true + helm_version: + description: 'helm cli version, e.g. `v3.7.0`, defaults to latest. Not necessarily stable.' + required: false + default: latest diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..f4226ba --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +set -e + +kubectl_version="$1" +config="$2" +helm_version="$3" + +if [ "$kubectl_version" = "latest" ]; then + kubectl_version=$(curl -Ls https://dl.k8s.io/release/stable.txt) +fi + +echo "using kubectl@$kubectl_version" + +curl -sLO "https://dl.k8s.io/release/$kubectl_version/bin/linux/amd64/kubectl" -o kubectl +chmod +x kubectl +mv kubectl /usr/local/bin + +# Extract the base64 encoded config data and write this to the KUBECONFIG +export KUBE_CONFIG_FOLDER="$GITHUB_WORKSPACE/.kube" +export KUBECONFIG="$KUBE_CONFIG_FOLDER/config" + +mkdir -p "$KUBE_CONFIG_FOLDER" + +echo "$config" | base64 -d > "$KUBECONFIG" +echo "KUBECONFIG=./.kube/config" >> "$GITHUB_ENV" + +# Install helm +if [ "$helm_version" = "latest" ]; then + helm_version=$(curl -s "https://api.github.com/repos/helm/helm/releases/latest" | awk -F '"' '/tag_name/{print $4}') +fi + +echo "using helm@$helm_version" + +curl -sLO "https://get.helm.sh/helm-$helm_version-linux-amd64.tar.gz" -o "helm-$helm_version-linux-amd64.tar.gz" +tar -zxvf "helm-$helm_version-linux-amd64.tar.gz" +mv linux-amd64/helm /usr/local/bin/helm