diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index 3936aa8b30f..88e41ff331d 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -154,3 +154,23 @@ steps: plugins: - junit-annotate#v2.4.1: artifacts: build/TEST-go-integration*.xml + + # Triggers a dynamic step: Sync K8s + # Runs only on main and if k8s files are changed + - label: "Trigger k8s sync" + branches: main + plugins: + - monebag/monorepo-diff#v2.5.9: + diff: "git diff --name-only HEAD~1" + watch: + - path: + - deploy/kubernetes/* + - version/docs/version.asciidoc + config: + label: "Sync K8s" + command: ".buildkite/scripts/steps/sync-k8s.sh" + agents: + provider: "gcp" + image: "family/core-ubuntu-2204" + env: + - GH_VERSION=2.4.0 diff --git a/.buildkite/scripts/install-gh.sh b/.buildkite/scripts/install-gh.sh new file mode 100644 index 00000000000..ff52687d02f --- /dev/null +++ b/.buildkite/scripts/install-gh.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +# Required environment variables: +# - GH_VERSION - the version of gh to install +set -exuo pipefail + +echo "--- Install gh cli" + +MSG="environment variable missing." +DEFAULT_HOME="/usr/local" +GH_VERSION=${GH_VERSION:-$MSG} +HOME=${HOME:-$DEFAULT_HOME} +GH_CMD="${HOME}/bin/gh" + +if command -v gh +then + set +e + echo "Found GH. Checking version.." + FOUND_GH_VERSION=$(gh --version 2>&1 >/dev/null | awk '{print $3}') + if [ "$FOUND_GH_VERSION" == "$GH_VERSION" ] + then + echo "GH Versions match: $GH_VERSION. No need to install gh. Exiting." + exit 0 + else + echo "GH Version mismatch. Desired version: $GH_VERSION, found version: $FOUND_GH_VERSION. Installing new version." + fi + set -e +fi + +source .buildkite/scripts/common.sh + +OS=$(uname -s| tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m| tr '[:upper:]' '[:lower:]') +if [ "${ARCH}" == "aarch64" ] ; then + ARCH_SUFFIX=arm64 +else + ARCH_SUFFIX=amd64 +fi + +echo "Downloading gh : ${GH_VERSION}..." +TMP_DIR=$(mktemp -d) +if retry 5 curl -sL "https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.tar.gz" | tar xz -C $TMP_DIR ; then + mkdir -p "${HOME}/bin" + mv "${TMP_DIR}/gh_${GH_VERSION}_linux_amd64/bin/gh" "${GH_CMD}" + rm -rf "${TMP_DIR}" +else + echo "Something bad with the download, deleting the binary" + if [ -e "${GH_CMD}" ] ; then + rm "${GH_CMD}" + fi + exit 1 +fi + + diff --git a/.buildkite/scripts/install-kind.sh b/.buildkite/scripts/install-kind.sh new file mode 100644 index 00000000000..ee0e0039719 --- /dev/null +++ b/.buildkite/scripts/install-kind.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -euo pipefail + +echo "--- Install Kind" + +MSG="environment variable missing." +DEFAULT_HOME="/usr/local" +KIND_VERSION=${KIND_VERSION:?$MSG} +HOME=${HOME:?$DEFAULT_HOME} +KIND_CMD="${HOME}/bin/kind" + +if command -v kind +then + set +e + echo "Found Kind. Checking version.." + FOUND_KIND_VERSION=$(kind --version 2>&1 >/dev/null | awk '{print $3}') + if [ "$FOUND_KIND_VERSION" == "$KIND_VERSION" ] + then + echo "Versions match. No need to install Kind. Exiting." + exit 0 + fi + set -e +fi + +echo "Installing Kind" + +OS=$(uname -s| tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m| tr '[:upper:]' '[:lower:]') +if [ "${ARCH}" == "aarch64" ] ; then + ARCH_SUFFIX=arm64 +else + ARCH_SUFFIX=amd64 +fi + +mkdir -p "${HOME}/bin" + +if curl -sSLo "${KIND_CMD}" "https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-${OS}-${ARCH_SUFFIX}" ; then + chmod +x "${KIND_CMD}" +else + echo "Something bad with the download, let's delete the corrupted binary" + if [ -e "${KIND_CMD}" ] ; then + rm "${KIND_CMD}" + fi + exit 1 +fi diff --git a/.buildkite/scripts/steps/sync-k8s.sh b/.buildkite/scripts/steps/sync-k8s.sh new file mode 100644 index 00000000000..ecd64e815a4 --- /dev/null +++ b/.buildkite/scripts/steps/sync-k8s.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -euo pipefail + +export PATH=$HOME/bin:${PATH} + +source .buildkite/scripts/install-gh.sh +source .buildkite/scripts/common.sh + +export GITHUB_TOKEN=$(retry 5 vault kv get -field token kv/ci-shared/platform-ingest/github_token) + +cd deploy/kubernetes + +echo "--- [File Creation] Create-Needed-Manifest" +WITHOUTCONFIG=true make generate-k8s +./creator_k8s_manifest.sh . + +echo "--- [Clone] Kibana-Repository" +make ci-clone-kibana-repository +cp Makefile ./kibana +cd kibana +echo "--- Create Kibana PR" +make ci-create-kubernetes-templates-pull-request \ No newline at end of file diff --git a/deploy/kubernetes/Makefile b/deploy/kubernetes/Makefile index e4d8b474908..f38f38cdf77 100644 --- a/deploy/kubernetes/Makefile +++ b/deploy/kubernetes/Makefile @@ -79,7 +79,7 @@ else echo "INFO: create pull request" @gh pr create \ --title "Update kubernetes templates for elastic-agent" \ - --body "Automated by ${BUILD_URL}" \ + --body "Automated by ${BUILDKITE_BUILD_URL}" \ --label automation \ --label release_note:skip \ --base main \