Skip to content

Commit 950abd3

Browse files
authored
Review buildkite scripts cleanup process (#1487)
Review buildkite scripts and hooks to ensure that the cleanup process performs all the needed operations
1 parent 1a3750c commit 950abd3

File tree

5 files changed

+71
-38
lines changed

5 files changed

+71
-38
lines changed

.buildkite/hooks/pre-command

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
#!/bin/bash
22

3+
source .buildkite/scripts/tooling.sh
34
set -euo pipefail
45

5-
function retry {
6-
local retries=$1
7-
shift
8-
9-
local count=0
10-
until "$@"; do
11-
exit=$?
12-
wait=$((2 ** count))
13-
count=$((count + 1))
14-
if [ $count -lt "$retries" ]; then
15-
>&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
16-
sleep $wait
17-
else
18-
>&2 echo "Retry $count/$retries exited $exit, no more retries left."
19-
return $exit
20-
fi
21-
done
22-
return 0
23-
}
24-
256
export GO_VERSION=$(cat .go-version)
267

278
GCP_SERVICE_ACCOUNT_SECRET_PATH=secret/ci/elastic-elastic-package/gcp-service-account

.buildkite/hooks/pre-exit

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
source .buildkite/scripts/tooling.sh
4+
5+
set -euo pipefail
6+
7+
unset_secrets
8+
9+
# integrations-parallel-gcp
10+
unset GOOGLE_CREDENTIALS
11+
unset GCP_PROJECT_ID
12+
13+
# integrations-parallel-aws and intregrations-parallel-aws_logs
14+
unset ELASTIC_PACKAGE_AWS_ACCESS_KEY
15+
unset ELASTIC_PACKAGE_AWS_SECRET_KEY
16+
unset AWS_ACCESS_KEY_ID
17+
unset AWS_SECRET_ACCESS_KEY

.buildkite/scripts/integration_tests.sh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ WORKSPACE="$(pwd)"
55
TMP_FOLDER_TEMPLATE_BASE="tmp.elastic-package"
66

77
cleanup() {
8+
local error_code=$?
9+
10+
if [ $error_code != 0 ] ; then
11+
# if variable is defined run the logout
12+
if [ -n "${GOOGLE_APPLICATION_CREDENTIALS+x}" ]; then
13+
google_cloud_logout_active_account
14+
fi
15+
fi
16+
817
echo "Deleting temporal files..."
918
cd ${WORKSPACE}
1019
rm -rf "${TMP_FOLDER_TEMPLATE_BASE}.*"
1120
echo "Done."
21+
22+
exit $error_code
1223
}
1324
trap cleanup EXIT
1425

@@ -67,14 +78,12 @@ if [[ "${TARGET}" == "" ]]; then
6778
fi
6879

6980
google_cloud_auth_safe_logs() {
70-
local gsUtilLocation=$(mktemp -d -p . -t ${TMP_FOLDER_TEMPLATE})
81+
local gsUtilLocation=$(mktemp -d -p ${WORKSPACE} -t ${TMP_FOLDER_TEMPLATE})
7182
local secretFileLocation=${gsUtilLocation}/${GOOGLE_CREDENTIALS_FILENAME}
7283

7384
echo "${PRIVATE_CI_GCS_CREDENTIALS_SECRET}" > ${secretFileLocation}
7485

7586
google_cloud_auth "${secretFileLocation}"
76-
77-
echo "${gsUtilLocation}"
7887
}
7988

8089
upload_safe_logs() {
@@ -87,12 +96,11 @@ upload_safe_logs() {
8796
return
8897
fi
8998

90-
local gsUtilLocation=$(google_cloud_auth_safe_logs)
99+
google_cloud_auth_safe_logs
91100

92101
gsutil cp ${source} "gs://${bucket}/buildkite/${REPO_BUILD_TAG}/${target}"
93102

94-
rm -rf "${gsUtilLocation}"
95-
unset GOOGLE_APPLICATION_CREDENTIALS
103+
google_cloud_logout_active_account
96104
}
97105

98106
add_bin_path

.buildkite/scripts/signAndPublishPackage.sh

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@ source .buildkite/scripts/install_deps.sh
88
source .buildkite/scripts/tooling.sh
99

1010
cleanup() {
11+
local error_code=$?
12+
13+
if [ $error_code != 0 ] ; then
14+
# if variable is defined, run the logout
15+
if [ -n "${GOOGLE_APPLICATION_CREDENTIALS+x}" ]; then
16+
google_cloud_logout_active_account
17+
fi
18+
fi
19+
1120
echo "Deleting temporal files..."
1221
cd ${WORKSPACE}
1322
rm -rf ${TMP_FOLDER_TEMPLATE_BASE}.*
1423
echo "Done."
24+
25+
exit $error_code
1526
}
1627

1728
trap cleanup EXIT
@@ -55,32 +66,28 @@ PACKAGE_STORAGE_INTERNAL_BUCKET_QUEUE_PUBLISHING_PATH="gs://elastic-bekitzur-pac
5566

5667

5768
google_cloud_auth_signing() {
58-
local gsUtilLocation=$(mktemp -d -p . -t ${TMP_FOLDER_TEMPLATE})
69+
local gsUtilLocation=$(mktemp -d -p ${WORKSPACE} -t ${TMP_FOLDER_TEMPLATE})
5970

6071
local secretFileLocation=${gsUtilLocation}/${GOOGLE_CREDENTIALS_FILENAME}
6172
echo "${SIGNING_PACKAGES_GCS_CREDENTIALS_SECRET}" > ${secretFileLocation}
6273

6374
google_cloud_auth "${secretFileLocation}"
64-
65-
echo "${gsUtilLocation}"
6675
}
6776

6877
google_cloud_auth_publishing() {
69-
local gsUtilLocation=$(mktemp -d -p . -t ${TMP_FOLDER_TEMPLATE})
78+
local gsUtilLocation=$(mktemp -d -p ${WORKSPACE} -t ${TMP_FOLDER_TEMPLATE})
7079

7180
local secretFileLocation=${gsUtilLocation}/${GOOGLE_CREDENTIALS_FILENAME}
7281
echo "${PACKAGE_UPLOADER_GCS_CREDENTIALS_SECRET}" > ${secretFileLocation}
7382

7483
google_cloud_auth "${secretFileLocation}"
75-
76-
echo "${gsUtilLocation}"
7784
}
7885

7986
sign_package() {
8087
local package=${1}
8188
local packageZip=$(basename ${package})
8289

83-
local gsUtilLocation=$(google_cloud_auth_signing)
90+
google_cloud_auth_signing
8491

8592
# upload zip package (trailing forward slashes are required)
8693
echo "Upload package .zip file for signing ${package} to ${INFRA_SIGNING_BUCKET_ARTIFACTS_PATH}"
@@ -106,16 +113,15 @@ sign_package() {
106113

107114
ls -l "${BUILD_PACKAGES_PATH}"
108115

109-
echo "Removing temporal location ${gsUtilLocation}"
110-
rm -r "${gsUtilLocation}"
116+
google_cloud_logout_active_account
111117
}
112118

113119
publish_package() {
114120
local package=$1
115121
local packageZip=$(basename ${package})
116122

117123
# create file with credentials
118-
local gsUtilLocation=$(google_cloud_auth_publishing)
124+
google_cloud_auth_publishing
119125

120126
# upload files (trailing forward slashes are required)
121127
echo "Upload package .zip file ${package} to ${PACKAGE_STORAGE_INTERNAL_BUCKET_QUEUE_PUBLISHING_PATH}"
@@ -135,8 +141,7 @@ publish_package() {
135141

136142
popd > /dev/null
137143

138-
echo "Removing temporal location ${gsUtilLocation}"
139-
rm -r "${gsUtilLocation}"
144+
google_cloud_logout_active_account
140145
}
141146

142147
add_bin_path

.buildkite/scripts/tooling.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
unset_secrets () {
5+
for var in $(printenv | sed 's;=.*;;' | sort); do
6+
if [[ "$var" == *_SECRET || "$var" == *_TOKEN ]]; then
7+
unset "$var"
8+
fi
9+
done
10+
}
11+
412
repo_name() {
513
# Example of URL: [email protected]:acme-inc/my-project.git
614
local repoUrl=$1
@@ -44,3 +52,17 @@ retry() {
4452
done
4553
return 0
4654
}
55+
56+
google_cloud_logout_active_account() {
57+
local active_account=$(gcloud auth list --filter=status:ACTIVE --format="value(account)" 2>/dev/null)
58+
if [[ -n "$active_account" && -n "${GOOGLE_APPLICATION_CREDENTIALS+x}" ]]; then
59+
echo "Logging out from GCP for active account"
60+
gcloud auth revoke $active_account > /dev/null 2>&1
61+
else
62+
echo "No active GCP accounts found."
63+
fi
64+
if [ -n "${GOOGLE_APPLICATION_CREDENTIALS+x}" ]; then
65+
rm -rf ${GOOGLE_APPLICATION_CREDENTIALS}
66+
unset GOOGLE_APPLICATION_CREDENTIALS
67+
fi
68+
}

0 commit comments

Comments
 (0)