Files
pipeline-actions/kubectl-apply/action.yaml
Jose Henrique fc7ed6bd15
Some checks failed
Test build-and-push (self-test) / Build tiny multi-arch image (push) Failing after 2s
fix: strip CRLF from inputs + add .gitattributes (eol=lf) -- fixes ssh username 'invalid characters'
2026-07-13 19:39:29 -03:00

111 lines
3.6 KiB
YAML

name: Kubectl Apply (in-repo manifests)
description: Validate KUBE_CONFIG, install kubectl, and apply Kubernetes manifest(s) that live inside the repository.
inputs:
kube_config:
description: Full kubeconfig YAML content (pass via secrets)
required: true
manifest:
description: Path (relative to repo root) of a single manifest file to apply. Usable alongside `manifests`.
required: false
default: ""
manifests:
description: >-
Space- or comma-separated list of manifest file paths (relative to repo
root) to apply. Each is applied with its own `kubectl apply -f`.
required: false
default: ""
namespace:
description: >-
Optional namespace override applied with `-n`. Manifests may also declare
their own namespace in metadata; this only adds `-n` when non-empty.
required: false
default: ""
kube_version:
description: Kubernetes version to download (stable, or a specific version like v1.31.0)
required: false
default: stable
runs:
using: composite
steps:
- name: Check KUBE_CONFIG validity
shell: bash
env:
KUBE_CONFIG: ${{ inputs.kube_config }}
run: |
if [ -z "${KUBE_CONFIG}" ] || [ "${KUBE_CONFIG// }" = "" ]; then
echo "KUBE_CONFIG is not set or is empty."
exit 1
fi
- name: Check out repository
uses: actions/checkout@v4
- name: Download and install kubectl
shell: bash
env:
KUBE_VERSION: ${{ inputs.kube_version }}
run: |
apt-get update -y
apt-get install -y curl
if [ "${KUBE_VERSION}" = "stable" ]; then
KUBE_VER="$(curl -L -s https://dl.k8s.io/release/stable.txt)"
else
KUBE_VER="${KUBE_VERSION}"
fi
curl -LO "https://dl.k8s.io/release/${KUBE_VER}/bin/linux/amd64/kubectl"
install -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
- name: Set up kubeconfig
shell: bash
env:
KUBE_CONFIG: ${{ inputs.kube_config }}
run: |
# strip stray CR (CRLF checkout via core.autocrlf)
printf '%s' "${KUBE_CONFIG}" | tr -d '\r' > kubeconfig.yaml
kubectl --kubeconfig=kubeconfig.yaml cluster-info
- name: Apply manifests
shell: bash
env:
MANIFEST: ${{ inputs.manifest }}
MANIFESTS: ${{ inputs.manifests }}
NAMESPACE: ${{ inputs.namespace }}
run: |
# strip stray CR (CRLF checkout via core.autocrlf)
NAMESPACE=$(printf '%s' "${NAMESPACE}" | tr -d '\r')
MANIFEST=$(printf '%s' "${MANIFEST}" | tr -d '\r')
MANIFESTS=$(printf '%s' "${MANIFESTS}" | tr -d '\r')
NS_FLAG=""
if [ -n "${NAMESPACE}" ] && [ "${NAMESPACE// }" != "" ]; then
NS_FLAG="-n ${NAMESPACE}"
fi
# Collect files from both inputs (manifests splits on comma or space).
FILES=""
if [ -n "${MANIFEST}" ] && [ "${MANIFEST// }" != "" ]; then
FILES="${FILES} ${MANIFEST}"
fi
if [ -n "${MANIFESTS}" ] && [ "${MANIFESTS// }" != "" ]; then
for f in $(echo "${MANIFESTS}" | tr ',' ' '); do
FILES="${FILES} ${f}"
done
fi
if [ -z "${FILES// }" ]; then
echo "::error::No manifest provided. Set the \`manifest\` or \`manifests\` input."
exit 1
fi
for f in ${FILES}; do
if [ ! -f "${f}" ]; then
echo "::error::Manifest not found in repo: ${f}"
exit 1
fi
echo "== applying ${f} =="
kubectl --kubeconfig=kubeconfig.yaml apply -f "${f}" ${NS_FLAG}
done