Files

93 lines
3.9 KiB
YAML

name: fleetctl-gitops
description: Runs fleetctl gitops to apply configuration to Fleet
# Schema: https://json.schemastore.org/github-action.json
# This action expects the following env vars to be set:
# - FLEET_URL: The URL of the Fleet server to apply configuration to.
# - FLEET_API_TOKEN: An API token for a Fleet GitOps user.
#
# Optional:
# - FLEET_GITOPS_DIR: The directory containing the GitOps config (default.yml,
# fleets/*.yml). Defaults to the current directory.
# - FLEET_CUSTOM_HEADERS: Comma-separated "Header:Value" pairs (e.g. a Cloudflare
# Access service token) sent on every request to the Fleet server.
inputs:
working-directory:
description: 'The working directory, which should be the root of the repository.'
default: './'
dry-run-only:
description: 'Whether to only run the fleetctl gitops commands in dry-run mode.'
default: 'false'
delete-other-fleets:
description: 'Whether to delete other fleets in Fleet which are not part of the gitops config.'
default: 'true'
runs:
using: "composite"
steps:
- name: Install fleetctl
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
FLEET_URL="${FLEET_URL%/}"
# Build optional custom request headers from FLEET_CUSTOM_HEADERS, a comma-separated
# list of "Header:Value" pairs (e.g. a Cloudflare Access service token). Empty by default.
CURL_HEADER_ARGS=()
if [[ -n "${FLEET_CUSTOM_HEADERS:-}" ]]; then
IFS=',' read -ra _CUSTOM_HEADERS <<< "$FLEET_CUSTOM_HEADERS"
for _header in "${_CUSTOM_HEADERS[@]}"; do
CURL_HEADER_ARGS+=(--header "$_header")
done
fi
FLEET_VERSION="$(curl "$FLEET_URL/api/v1/fleet/version" --header "Authorization: Bearer $FLEET_API_TOKEN" "${CURL_HEADER_ARGS[@]}" --fail --silent | jq --raw-output '.version')"
DEFAULT_FLEETCTL_VERSION="latest"
# Decide which fleetctl version to install:
# If the server returns a clean version (e.g. 4.74.0), use that.
# If the server returns a snapshot (e.g. 0.0.0-SNAPSHOT-xxxxx) or is empty, pin to DEFAULT_FLEETCTL_VERSION.
if [[ -z "$FLEET_VERSION" ]]; then
INSTALL_VERSION="$DEFAULT_FLEETCTL_VERSION"
elif [[ "$FLEET_VERSION" == 0.0.0-SNAPSHOT* ]]; then
INSTALL_VERSION="$DEFAULT_FLEETCTL_VERSION"
elif [[ "$FLEET_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
INSTALL_VERSION="$FLEET_VERSION"
else
# Strip anything after + (e.g. 4.81.0+foobar -> 4.81.0)
FLEET_VERSION="${FLEET_VERSION%%\+*}"
if [[ "$FLEET_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
INSTALL_VERSION="$FLEET_VERSION"
else
INSTALL_VERSION="$DEFAULT_FLEETCTL_VERSION"
fi
fi
echo "Installing fleetctl v$INSTALL_VERSION..."
npm install -g "fleetctl@$INSTALL_VERSION" || npm install -g fleetctl@latest
- name: Configure fleetctl
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
# Build optional custom request headers from FLEET_CUSTOM_HEADERS, a comma-separated
# list of "Header:Value" pairs. fleetctl persists them and sends them on every request,
# so the gitops commands below use them too.
CUSTOM_HEADER_ARGS=()
if [[ -n "${FLEET_CUSTOM_HEADERS:-}" ]]; then
IFS=',' read -ra _CUSTOM_HEADERS <<< "$FLEET_CUSTOM_HEADERS"
for _header in "${_CUSTOM_HEADERS[@]}"; do
CUSTOM_HEADER_ARGS+=(--custom-header "$_header")
done
fi
fleetctl config set --address "$FLEET_URL" --token "$FLEET_API_TOKEN" "${CUSTOM_HEADER_ARGS[@]}"
- name: Run fleetctl gitops commands
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
FLEET_DRY_RUN_ONLY: ${{ inputs.dry-run-only }}
FLEET_DELETE_OTHER_FLEETS: ${{ inputs.delete-other-fleets }}
run: bash "$GITHUB_ACTION_PATH/gitops.sh"