Skip to content

Commit 0d7545b

Browse files
migrate metricbeat pipeline to Buildkite (#37592) (#37857)
migrate the metricbeat pipeline to Buildkite (cherry picked from commit a611494) Co-authored-by: sharbuz <87968844+sharbuz@users.noreply.github.com>
1 parent 5e431f3 commit 0d7545b

13 files changed

+727
-7
lines changed

.buildkite/hooks/post-checkout

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
checkout_merge() {
6+
local target_branch=$1
7+
local pr_commit=$2
8+
local merge_branch=$3
9+
10+
if [[ -z "${target_branch}" ]]; then
11+
echo "No pull request target branch"
12+
exit 1
13+
fi
14+
15+
git fetch -v origin "${target_branch}"
16+
git checkout FETCH_HEAD
17+
echo "Current branch: $(git rev-parse --abbrev-ref HEAD)"
18+
19+
# create temporal branch to merge the PR with the target branch
20+
git checkout -b ${merge_branch}
21+
echo "New branch created: $(git rev-parse --abbrev-ref HEAD)"
22+
23+
# set author identity so it can be run git merge
24+
git config user.name "github-merged-pr-post-checkout"
25+
git config user.email "auto-merge@buildkite"
26+
27+
git merge --no-edit "${BUILDKITE_COMMIT}" || {
28+
local merge_result=$?
29+
echo "Merge failed: ${merge_result}"
30+
git merge --abort
31+
exit ${merge_result}
32+
}
33+
}
34+
35+
pull_request="${BUILDKITE_PULL_REQUEST:-false}"
36+
37+
if [[ "${pull_request}" == "false" ]]; then
38+
echo "Not a pull request, skipping"
39+
exit 0
40+
fi
41+
42+
TARGET_BRANCH="${BUILDKITE_PULL_REQUEST_BASE_BRANCH:-master}"
43+
PR_COMMIT="${BUILDKITE_COMMIT}"
44+
PR_ID=${BUILDKITE_PULL_REQUEST}
45+
MERGE_BRANCH="pr_merge_${PR_ID}"
46+
47+
checkout_merge "${TARGET_BRANCH}" "${PR_COMMIT}" "${MERGE_BRANCH}"
48+
49+
echo "Commit information"
50+
git --no-pager log --format=%B -n 1
51+
52+
# Ensure buildkite groups are rendered
53+
echo ""

.buildkite/hooks/pre-command

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
set -euo pipefail
44

5-
source .buildkite/env-scripts/env.sh
6-
source .buildkite/env-scripts/util.sh
7-
source .buildkite/env-scripts/win-env.sh
8-
9-
105
if [[ "$BUILDKITE_PIPELINE_SLUG" == "filebeat" ]]; then
6+
source .buildkite/env-scripts/env.sh
7+
source .buildkite/env-scripts/util.sh
8+
source .buildkite/env-scripts/win-env.sh
9+
1110
if [[ ${PLATFORM_TYPE} = MINGW* ]]; then
1211
install_python_win
1312
fi
@@ -16,3 +15,11 @@ if [[ "$BUILDKITE_PIPELINE_SLUG" == "filebeat" ]]; then
1615
export GOLANG_VERSION=$(cat "${WORKSPACE}/.go-version")
1716
fi
1817
fi
18+
19+
if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-metricbeat" ]]; then
20+
source .buildkite/scripts/setenv.sh
21+
if [[ "${BUILDKITE_COMMAND}" =~ ^buildkite-agent ]]; then
22+
echo "Skipped pre-command when running the Upload pipeline"
23+
exit 0
24+
fi
25+
fi

.buildkite/metricbeat/pipeline.yml

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
# yaml-language-server: $schema=https://raw.githubusercontent.com/buildkite/pipeline-schema/main/schema.json
2+
env:
3+
IMAGE_UBUNTU_X86_64: "family/core-ubuntu-2204"
4+
IMAGE_UBUNTU_ARM_64: "core-ubuntu-2004-aarch64"
5+
IMAGE_WIN_10: "family/general-windows-10"
6+
IMAGE_WIN_11: "family/general-windows-11"
7+
IMAGE_WIN_2016: "family/core-windows-2016"
8+
IMAGE_WIN_2019: "family/core-windows-2019"
9+
IMAGE_WIN_2022: "family/core-windows-2022"
10+
IMAGE_MACOS_X86_64: "generic-13-ventura-x64"
11+
GO_AGENT_IMAGE: "golang:${GO_VERSION}"
12+
BEATS_PROJECT_NAME: "metricbeat"
213

314
steps:
4-
- label: "Example test"
5-
command: echo "Hello!"
15+
16+
- input: "Input Parameters"
17+
key: "input-run-all-stages"
18+
fields:
19+
- select: "Metricbeat - runAllStages"
20+
key: "runAllStages"
21+
options:
22+
- label: "True"
23+
value: "true"
24+
- label: "False"
25+
value: "false"
26+
default: "false"
27+
- select: "Metricbeat - runMacOsTests"
28+
key: "UI_MACOS_TESTS"
29+
options:
30+
- label: "True"
31+
value: "true"
32+
- label: "False"
33+
value: "false"
34+
default: "false"
35+
if: "build.source == 'ui'"
36+
37+
- wait: ~
38+
if: "build.source == 'ui'"
39+
allow_dependency_failure: false
40+
41+
- label: ":linux: Load dynamic metricbeat pipeline"
42+
key: "metricbeat-pipeline"
43+
command: ".buildkite/scripts/generate_metricbeat_pipeline.sh"
44+
agents:
45+
provider: "gcp"
46+
image: "${IMAGE_UBUNTU_X86_64}"

.buildkite/scripts/common.sh

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
WORKSPACE=${WORKSPACE:-"$(pwd)"}
5+
BIN="${WORKSPACE}/bin"
6+
platform_type="$(uname)"
7+
platform_type_lowercase=$(echo "$platform_type" | tr '[:upper:]' '[:lower:]')
8+
arch_type="$(uname -m)"
9+
GITHUB_PR_TRIGGER_COMMENT=${GITHUB_PR_TRIGGER_COMMENT:-""}
10+
ONLY_DOCS=${ONLY_DOCS:-"true"}
11+
UI_MACOS_TESTS="$(buildkite-agent meta-data get UI_MACOS_TESTS --default ${UI_MACOS_TESTS:-"false"})"
12+
runAllStages="$(buildkite-agent meta-data get runAllStages --default ${runAllStages:-"false"})"
13+
metricbeat_changeset=(
14+
"^metricbeat/.*"
15+
"^go.mod"
16+
"^pytest.ini"
17+
"^dev-tools/.*"
18+
"^libbeat/.*"
19+
"^testing/.*"
20+
)
21+
oss_changeset=(
22+
"^go.mod"
23+
"^pytest.ini"
24+
"^dev-tools/.*"
25+
"^libbeat/.*"
26+
"^testing/.*"
27+
)
28+
ci_changeset=(
29+
"^.buildkite/.*"
30+
)
31+
go_mod_changeset=(
32+
"^go.mod"
33+
)
34+
docs_changeset=(
35+
".*\\.(asciidoc|md)"
36+
"deploy/kubernetes/.*-kubernetes\\.yaml"
37+
)
38+
packaging_changeset=(
39+
"^dev-tools/packaging/.*"
40+
".go-version"
41+
)
42+
43+
with_docker_compose() {
44+
local version=$1
45+
echo "Setting up the Docker-compose environment..."
46+
create_workspace
47+
retry 3 curl -sSL -o ${BIN}/docker-compose "https://github.com/docker/compose/releases/download/${version}/docker-compose-${platform_type_lowercase}-${arch_type}"
48+
chmod +x ${BIN}/docker-compose
49+
export PATH="${BIN}:${PATH}"
50+
docker-compose version
51+
}
52+
53+
create_workspace() {
54+
if [[ ! -d "${BIN}" ]]; then
55+
mkdir -p "${BIN}"
56+
fi
57+
}
58+
59+
add_bin_path() {
60+
echo "Adding PATH to the environment variables..."
61+
create_workspace
62+
export PATH="${BIN}:${PATH}"
63+
}
64+
65+
check_platform_architeture() {
66+
case "${arch_type}" in
67+
"x86_64")
68+
go_arch_type="amd64"
69+
;;
70+
"aarch64")
71+
go_arch_type="arm64"
72+
;;
73+
"arm64")
74+
go_arch_type="arm64"
75+
;;
76+
*)
77+
echo "The current platform/OS type is unsupported yet"
78+
;;
79+
esac
80+
}
81+
82+
with_mage() {
83+
local install_packages=(
84+
"github.com/magefile/mage"
85+
"github.com/elastic/go-licenser"
86+
"golang.org/x/tools/cmd/goimports"
87+
"github.com/jstemmer/go-junit-report"
88+
"gotest.tools/gotestsum"
89+
)
90+
create_workspace
91+
for pkg in "${install_packages[@]}"; do
92+
go install "${pkg}@latest"
93+
done
94+
}
95+
96+
with_go() {
97+
echo "Setting up the Go environment..."
98+
create_workspace
99+
check_platform_architeture
100+
retry 5 curl -sL -o "${BIN}/gvm" "https://github.com/andrewkroh/gvm/releases/download/${SETUP_GVM_VERSION}/gvm-${platform_type_lowercase}-${go_arch_type}"
101+
chmod +x "${BIN}/gvm"
102+
eval "$(gvm $GO_VERSION)"
103+
go version
104+
which go
105+
local go_path="$(go env GOPATH):$(go env GOPATH)/bin"
106+
export PATH="${go_path}:${PATH}"
107+
}
108+
109+
with_python() {
110+
if [ "${platform_type}" == "Linux" ]; then
111+
#sudo command doesn't work at the "pre-command" hook because of another user environment (root with strange permissions)
112+
sudo apt-get update
113+
sudo apt-get install -y python3-pip python3-venv
114+
elif [ "${platform_type}" == "Darwin" ]; then
115+
brew update
116+
pip3 install virtualenv
117+
ulimit -Sn 10000
118+
fi
119+
}
120+
121+
with_dependencies() {
122+
if [ "${platform_type}" == "Linux" ]; then
123+
#sudo command doesn't work at the "pre-command" hook because of another user environment (root with strange permissions)
124+
sudo apt-get update
125+
sudo apt-get install -y libsystemd-dev libpcap-dev
126+
elif [ "${platform_type}" == "Darwin" ]; then
127+
pip3 install libpcap
128+
fi
129+
}
130+
131+
retry() {
132+
local retries=$1
133+
shift
134+
local count=0
135+
until "$@"; do
136+
exit=$?
137+
wait=$((2 ** count))
138+
count=$((count + 1))
139+
if [ $count -lt "$retries" ]; then
140+
>&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
141+
sleep $wait
142+
else
143+
>&2 echo "Retry $count/$retries exited $exit, no more retries left."
144+
return $exit
145+
fi
146+
done
147+
return 0
148+
}
149+
150+
are_paths_changed() {
151+
local patterns=("${@}")
152+
local changelist=()
153+
154+
for pattern in "${patterns[@]}"; do
155+
changed_files=($(git diff --name-only HEAD@{1} HEAD | grep -E "$pattern"))
156+
if [ "${#changed_files[@]}" -gt 0 ]; then
157+
changelist+=("${changed_files[@]}")
158+
fi
159+
done
160+
161+
if [ "${#changelist[@]}" -gt 0 ]; then
162+
echo "Files changed:"
163+
echo "${changelist[*]}"
164+
return 0
165+
else
166+
echo "No files changed within specified changeset:"
167+
echo "${patterns[*]}"
168+
return 1
169+
fi
170+
}
171+
172+
are_changed_only_paths() {
173+
local patterns=("${@}")
174+
local changelist=()
175+
local changed_files=$(git diff --name-only HEAD@{1} HEAD)
176+
if [ -z "$changed_files" ] || grep -qE "$(IFS=\|; echo "${patterns[*]}")" <<< "$changed_files"; then
177+
return 0
178+
else
179+
return 1
180+
fi
181+
}
182+
183+
are_conditions_met_mandatory_tests() {
184+
if [[ "${BUILDKITE_PULL_REQUEST}" == "" ]] || [[ "${runAllStages}" == "true" ]] || [[ "${ONLY_DOCS}" == "false" && "${BUILDKITE_PULL_REQUEST}" != "" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L107-L137
185+
if are_paths_changed "${metricbeat_changeset[@]}" || are_paths_changed "${oss_changeset[@]}" || are_paths_changed "${ci_changeset[@]}" || [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test metricbeat" ]] || [[ "${GITHUB_PR_LABELS}" =~ Metricbeat ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12
186+
return 0
187+
else
188+
return 1
189+
fi
190+
else
191+
return 1
192+
fi
193+
}
194+
195+
are_conditions_met_extended_tests() {
196+
if are_conditions_met_mandatory_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171
197+
return 0
198+
else
199+
return 1
200+
fi
201+
}
202+
203+
are_conditions_met_macos_tests() {
204+
if are_conditions_met_mandatory_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171
205+
if [[ "${UI_MACOS_TESTS}" == true ]] || [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test metricbeat for macos" ]] || [[ "${GITHUB_PR_LABELS}" =~ macOS ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12
206+
return 0
207+
else
208+
return 1
209+
fi
210+
else
211+
return 1
212+
fi
213+
}
214+
215+
are_conditions_met_extended_windows_tests() {
216+
if [[ "${ONLY_DOCS}" == "false" && "${BUILDKITE_PULL_REQUEST}" != "" ]] || [[ "${runAllStages}" == "true" ]]; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171
217+
if are_paths_changed "${metricbeat_changeset[@]}" || are_paths_changed "${oss_changeset[@]}" || are_paths_changed "${ci_changeset[@]}" || [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test metricbeat" ]] || [[ "${GITHUB_PR_LABELS}" =~ Metricbeat ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12
218+
return 0
219+
else
220+
return 1
221+
fi
222+
else
223+
return 1
224+
fi
225+
}
226+
227+
are_conditions_met_packaging() {
228+
if are_conditions_met_extended_windows_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171
229+
if are_paths_changed "${metricbeat_changeset[@]}" || are_paths_changed "${oss_changeset[@]}" || [[ "${BUILDKITE_TAG}" == "" ]] || [[ "${BUILDKITE_PULL_REQUEST}" != "" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L101-L103
230+
return 0
231+
else
232+
return 1
233+
fi
234+
else
235+
return 1
236+
fi
237+
}
238+
239+
if ! are_changed_only_paths "${docs_changeset[@]}" ; then
240+
ONLY_DOCS="false"
241+
echo "Changes include files outside the docs_changeset vairiabe. ONLY_DOCS=$ONLY_DOCS."
242+
else
243+
echo "All changes are related to DOCS. ONLY_DOCS=$ONLY_DOCS."
244+
fi
245+
246+
if are_paths_changed "${go_mod_changeset[@]}" ; then
247+
GO_MOD_CHANGES="true"
248+
fi
249+
250+
if are_paths_changed "${packaging_changeset[@]}" ; then
251+
PACKAGING_CHANGES="true"
252+
fi

.buildkite/scripts/crosscompile.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
source .buildkite/scripts/install_tools.sh
4+
5+
set -euo pipefail
6+
7+
echo "--- Run Crosscompile for $BEATS_PROJECT_NAME"
8+
make -C "${BEATS_PROJECT_NAME}" crosscompile

0 commit comments

Comments
 (0)