Skip to content

Commit de20b6d

Browse files
authored
Update pipeline to test with integrations (#1277)
Update Buildkite pipeline to add all the required steps to create a Pull Request or update it to test the current elastic-package code with all the (integration and input) packages in the integrations repository
1 parent 374a34c commit de20b6d

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed

.buildkite/hooks/pre-command

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,9 @@ if [[ "$BUILDKITE_PIPELINE_SLUG" == "elastic-package-package-storage-publish" &&
7070
# publishing job
7171
export PACKAGE_UPLOADER_GCS_CREDENTIALS_SECRET=$(retry 5 vault kv get -field value ${PACKAGE_UPLOADER_GCS_CREDENTIALS_PATH})
7272
fi
73+
74+
if [[ "$BUILDKITE_PIPELINE_SLUG" == "elastic-package-test-with-integrations" && "$BUILDKITE_STEP_KEY" == "pr-integrations" ]]; then
75+
export GITHUB_USERNAME_SECRET=$(retry 5 vault kv get -field username ${GITHUB_TOKEN_VAULT_PATH})
76+
export GITHUB_EMAIL_SECRET=$(retry 5 vault kv get -field email ${GITHUB_TOKEN_VAULT_PATH})
77+
export GITHUB_TOKEN=$(retry 5 vault kv get -field token ${GITHUB_TOKEN_VAULT_PATH})
78+
fi

.buildkite/pipeline.test-with-integrations-repo.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
env:
2+
SETUP_GVM_VERSION: 'v0.5.0' # https://github.com/andrewkroh/gvm/issues/44#issuecomment-1013231151
23
GOLANG_VERSION: "1.20.3"
4+
GH_CLI_VERSION: "2.29.0"
5+
JQ_VERSION: "1.6"
36

47
steps:
58
- label: ":go: Run check-static"
@@ -9,4 +12,22 @@ steps:
912
image: "golang:${GOLANG_VERSION}"
1013
cpu: "8"
1114
memory: "4G"
15+
- label: ":go: :linux: Run unit tests"
16+
key: unit-tests-linux
17+
command: "make test-go-ci"
18+
artifact_paths:
19+
- "build/test-results/*.xml"
20+
- "build/test-coverage/*.xml"
21+
agents:
22+
image: "golang:${GOLANG_VERSION}"
23+
cpu: "8"
24+
memory: "4G"
25+
- label: ":hammer: Create PR in integrations"
26+
key: pr-integrations
27+
command: ".buildkite/scripts/test-with-integrations.sh"
28+
agents:
29+
provider: "gcp"
30+
depends_on:
31+
- check-static
32+
- unit-tests-linux
1233

.buildkite/scripts/install_deps.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,30 @@ with_docker_compose() {
3939
chmod +x ${WORKSPACE}/bin/docker-compose
4040
docker-compose version
4141
}
42+
43+
with_github_cli() {
44+
mkdir -p ${WORKSPACE}/bin
45+
mkdir -p ${WORKSPACE}/tmp
46+
47+
local gh_filename="gh_${GH_CLI_VERSION}_linux_amd64"
48+
local gh_tar_file="${gh_filename}.tar.gz"
49+
local gh_tar_full_path="${WORKSPACE}/tmp/${gh_tar_file}"
50+
51+
retry 5 curl -sL -o ${gh_tar_full_path} "https://github.com/cli/cli/releases/download/v${GH_CLI_VERSION}/${gh_tar_file}"
52+
53+
# just extract the binary file from the tar.gz
54+
tar -C ${WORKSPACE}/bin -xpf ${gh_tar_full_path} ${gh_filename}/bin/gh --strip-components=2
55+
56+
chmod +x ${WORKSPACE}/bin/gh
57+
rm -rf ${WORKSPACE}/tmp
58+
59+
gh version
60+
}
61+
62+
with_jq() {
63+
mkdir -p ${WORKSPACE}/bin
64+
retry 5 curl -sL -o ${WORKSPACE}/bin/jq "https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64"
65+
66+
chmod +x ${WORKSPACE}/bin/jq
67+
jq --version
68+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
WORKSPACE="$(pwd)"
6+
7+
TMP_FOLDER_TEMPLATE_BASE="tmp.${GITHUB_PR_BASE_REPO}"
8+
TMP_FOLDER_TEMPLATE="${TMP_FOLDER_TEMPLATE_BASE}.XXXXXXXXX"
9+
10+
cleanup() {
11+
echo "Deleting temporal files..."
12+
cd ${WORKSPACE}
13+
rm -rf "${TMP_FOLDER_TEMPLATE_BASE}.*"
14+
echo "Done."
15+
}
16+
17+
trap cleanup EXIT
18+
source .buildkite/scripts/install_deps.sh
19+
20+
add_bin_path
21+
22+
echo "--- install gh cli"
23+
with_github_cli
24+
25+
echo "--- install jq"
26+
with_jq
27+
28+
29+
INTEGRATIONS_SOURCE_BRANCH=main
30+
INTEGRATIONS_GITHUB_OWNER=elastic
31+
INTEGRATIONS_GITHUB_REPO_NAME=integrations
32+
INTEGRATIONS_PR_BRANCH="test-${GITHUB_PR_BASE_REPO}-pr-${BUILDKITE_PULL_REQUEST}"
33+
INTEGRATIONS_PR_TITLE="Test ${GITHUB_PR_BASE_REPO}#${BUILDKITE_PULL_REQUEST} - DO NOT MERGE"
34+
VERSION_DEP=""
35+
36+
get_pr_number() {
37+
# requires GITHUB_TOKEN
38+
local branch="$1"
39+
gh pr list -H "${branch}" --json number | jq -r '.[]|.number'
40+
}
41+
42+
get_integrations_pr_link() {
43+
local pr_number=$1
44+
echo "https://github.com/elastic/integrations/pull/${pr_number}"
45+
}
46+
47+
get_source_pr_link() {
48+
echo "https://github.com/${GITHUB_PR_BASE_OWNER}/${GITHUB_PR_BASE_REPO}/pull/${BUILDKITE_PULL_REQUEST}"
49+
}
50+
51+
get_source_commit_link() {
52+
echo "https://github.com/${GITHUB_PR_BASE_OWNER}/${GITHUB_PR_BASE_REPO}/commit/${GITHUB_PR_HEAD_SHA}"
53+
}
54+
55+
set_git_config() {
56+
git config user.name "${GITHUB_USERNAME_SECRET}"
57+
git config user.email "${GITHUB_EMAIL_SECRET}"
58+
}
59+
60+
git_push_with_auth() {
61+
local owner="$1"
62+
local repository="$2"
63+
local branch="$3"
64+
65+
retry 3 git push https://${GITHUB_USERNAME_SECRET}:${GITHUB_TOKEN}@github.com/${owner}/${repository}.git "${branch}"
66+
}
67+
68+
clone_repository() {
69+
local target="$1"
70+
retry 5 git clone https://github.com/elastic/integrations ${target}
71+
}
72+
73+
create_integrations_pull_request() {
74+
# requires GITHUB_TOKEN
75+
local temp_path=$(mktemp -d -p ${WORKSPACE} -t ${TMP_FOLDER_TEMPLATE})
76+
echo "Creating Pull Request"
77+
message="Update ${GITHUB_PR_BASE_REPO} reference to $(get_source_commit_link).\nAutomated by [Buildkite build](${BUILDKITE_BUILD_URL})\n\nRelates: $(get_source_pr_link)"
78+
echo -e $message > ${temp_path}/body-pr.txt
79+
retry 3 \
80+
gh pr create \
81+
--title "${INTEGRATIONS_PR_TITLE}" \
82+
--body-file ${temp_path}/body-pr.txt \
83+
--draft \
84+
--base ${INTEGRATIONS_SOURCE_BRANCH} \
85+
--head ${INTEGRATIONS_PR_BRANCH} \
86+
--assignee ${GITHUB_PR_HEAD_USER}
87+
}
88+
89+
update_dependency() {
90+
# it needs to set the Golang version from the integrations repository (.go-version file)
91+
echo "--- install go for integrations repository :go:"
92+
with_go
93+
94+
echo "--- Updating go.mod and go.sum with ${GITHUB_PR_HEAD_SHA} :hammer_and_wrench:"
95+
local source_dep="github.com/${GITHUB_PR_BASE_OWNER}/${GITHUB_PR_BASE_REPO}${VERSION_DEP}"
96+
local target_dep="github.com/${GITHUB_PR_OWNER}/${GITHUB_PR_REPO}${VERSION_DEP}@${GITHUB_PR_HEAD_SHA}"
97+
98+
go mod edit -replace ${source_dep}=${target_dep}
99+
go mod tidy
100+
101+
git add go.mod
102+
git add go.sum
103+
104+
# allow not to commit if there are no changes
105+
# previous execution could fail and just pushed the branch but PR is not created
106+
if ! git diff-index --quiet HEAD ; then
107+
git commit -m "Test elastic-package from PR ${BUILDKITE_PULL_REQUEST} - ${GITHUB_PR_HEAD_SHA}"
108+
fi
109+
110+
echo ""
111+
git --no-pager show --format=oneline HEAD
112+
echo ""
113+
}
114+
115+
116+
exists_branch() {
117+
local owner="$1"
118+
local repository="$2"
119+
local branch="$3"
120+
121+
git ls-remote --exit-code --heads https://github.com/${owner}/${repository}.git ${branch}
122+
}
123+
124+
create_or_update_pull_request() {
125+
local temp_path=$(mktemp -d -p ${WORKSPACE} -t ${TMP_FOLDER_TEMPLATE})
126+
local repo_path="${temp_path}/elastic-integrations"
127+
local checkout_options=""
128+
local integrations_pr_number=""
129+
130+
echo "Cloning repository"
131+
clone_repository "${repo_path}"
132+
133+
pushd "${repo_path}" > /dev/null
134+
135+
set_git_config
136+
137+
echo "Checking branch ${INTEGRATIONS_PR_BRANCH} in remote ${INTEGRATIONS_GITHUB_OWNER}/${INTEGRATIONS_GITHUB_REPO_NAME}"
138+
if ! exists_branch ${INTEGRATIONS_GITHUB_OWNER} ${INTEGRATIONS_GITHUB_REPO_NAME} ${INTEGRATIONS_PR_BRANCH} ; then
139+
checkout_options=" -b "
140+
echo "Creating a new branch..."
141+
else
142+
echo "Already existed"
143+
fi
144+
145+
integrations_pr_number=$(get_pr_number "${INTEGRATIONS_PR_BRANCH}")
146+
if [ -z "${integrations_pr_number}" ]; then
147+
echo "Exists PR in integrations repository: ${integrations_pr_number}"
148+
fi
149+
150+
git checkout ${checkout_options} ${INTEGRATIONS_PR_BRANCH}
151+
152+
echo "--- Updating dependency :pushpin:"
153+
update_dependency
154+
155+
echo "--- Pushing branch ${INTEGRATIONS_PR_BRANCH} to integrations repository..."
156+
git_push_with_auth ${INTEGRATIONS_GITHUB_OWNER} ${INTEGRATIONS_GITHUB_REPO_NAME} ${INTEGRATIONS_PR_BRANCH}
157+
158+
if [ -z "${integrations_pr_number}" ]; then
159+
echo "--- Creating pull request :github:"
160+
create_integrations_pull_request
161+
162+
sleep 10
163+
164+
integrations_pr_number=$(get_pr_number "${INTEGRATIONS_PR_BRANCH}")
165+
fi
166+
167+
popd > /dev/null
168+
169+
rm -rf "${temp_path}"
170+
171+
echo "--- adding comment into ${GITHUB_PR_BASE_REPO} pull request :memo:"
172+
add_pr_comment "${BUILDKITE_PULL_REQUEST}" "$(get_integrations_pr_link ${integrations_pr_number})"
173+
}
174+
175+
176+
add_pr_comment() {
177+
local source_pr_number="$1"
178+
local integrations_pr_link="$2"
179+
180+
retry 3 \
181+
gh pr comment ${source_pr_number} \
182+
--body "Created or updated PR in integrations repostiory to test this vesrion. Check ${integrations_pr_link}" \
183+
--repo ${GITHUB_PR_BASE_OWNER}/${GITHUB_PR_BASE_REPO}
184+
}
185+
186+
187+
echo "--- creating or updating integrations pull request"
188+
create_or_update_pull_request

0 commit comments

Comments
 (0)