forked from pulumi/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
executable file
·128 lines (109 loc) · 4.34 KB
/
common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
aws_region() {
echo "$(pulumi -C infrastructure config get 'aws:region')"
}
# Posts a message to Slack. Requires a valid access token is available in $SLACK_ACCESS_TOKEN.
# Usage: post_to_slack <channel> <message>
post_to_slack() {
local channel=$1
local message=$2
local escaped=$(echo ${message} | sed 's/"/\"/g' | sed "s/'/\'/g" )
local json="{\"channel\": \"#${channel}\", \"text\": \"${escaped}\", \"as_user\": true}"
curl -s \
-X POST \
-H "Content-type: application/json" \
-H "Authorization: Bearer ${SLACK_ACCESS_TOKEN}" \
-d "${json}" \
https://slack.com/api/chat.postMessage > /dev/null
}
# Posts a comment to a GitHub PR. Requires a GitHub token is available in $GITHUB_TOKEN.
# Usage: post_github_pr_comment "Hi!" "https://api.github.com/repos/<org>/<repo>/issues/<pr-number>/comments"
post_github_pr_comment() {
local pr_comment=$1
local pr_comment_api_url=$2
local pr_comment_body=$(printf '{ "body": "%s" }' "$pr_comment")
curl -s \
-X POST \
-H "Authorization: token ${PULUMI_BOT_TOKEN}" \
-d "$pr_comment_body" \
$pr_comment_api_url > /dev/null
}
# Returns the Git SHA of the HEAD commit. For pull requests, we take this from GitHub event metadata, since in that case, the HEAD commit will contain the SHA of the merge commit with the base branch.
git_sha() {
if [[ "$GITHUB_EVENT_NAME" == "pull_request" && ! -z "$GITHUB_EVENT_PATH" ]]; then
echo "$(cat "$GITHUB_EVENT_PATH" | jq -r ".pull_request.head.sha")"
else
echo "$(git rev-parse HEAD)"
fi
}
# Returns the shortened version of either the GITHUB_SHA, if present, or that of the most
# recent commit.
git_sha_short() {
echo "$(git_sha)" | cut -c1-8
}
# current_time_in_ms returns the epoch time in milliseconds.
current_time_in_ms() {
echo "$(node -e 'console.log(Date.now())')"
}
origin_bucket_prefix() {
# This function returns the bucket name prefix to be used when naming the
# S3 buckets. We are adding a `www` prefix to the buckets being deployed
# to the new account, in order to account for collisions in the global
# bucket namespace.
echo "www-${DEPLOYMENT_ENVIRONMENT}-pulumi-docs-origin"
}
# Returns the name of the metadata file we expect to exist locally before running Pulumi.
origin_bucket_metadata_filepath() {
echo "./origin-bucket-metadata.json"
}
# build_identifier returns a string that is used to identify the current build for naming
# S3 buckets and asset bundles.
build_identifier() {
local identifier
# For CI builds, we use the GitHub Actions event to generate more readable identifiers.
# - For pull_request actions, return "pr-<number>-<git-sha>"
# - For others, return "<event-name>-<git-sha>".
if [[ ! -z "$GITHUB_EVENT_NAME" && ! -z "$GITHUB_EVENT_PATH" ]]; then
identifier="$GITHUB_EVENT_NAME"
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
identifier="pr-$(cat "$GITHUB_EVENT_PATH" | jq -r ".number")"
fi
identifier="${identifier}-$(git_sha_short)"
else
# For on-demand builds, if an identifier's been set, use it.
identifier="$BUILD_IDENTIFIER"
# Otherwise, just use the current Git SHA.
if [ -z "$BUILD_IDENTIFIER" ]; then
identifier="$(git_sha_short)"
fi
fi
echo "$identifier"
}
# List the 100 most recent bucket in the current account, sorted descendingly by
# CreationDate, matching the prefix we use to name website buckets. Supports an optional
# suffix to filter by (e.g., "pr" or "push").
get_recent_buckets() {
aws s3api list-buckets \
--query "reverse(sort_by(Buckets,&CreationDate))[:100].{id:Name,date:CreationDate}|[?starts_with(id,'$(origin_bucket_prefix)-${1}')]" \
--region "$(aws_region)" \
--output json | jq -r '.[].id'
}
# Retry the given command some number of times, with a delay of some number of seconds between calls.
# Usage: retry some_command <retry-count> <delay-in-seconds>
retry() {
local n=1
local max=$2
local delay=$3
while true; do
"$@" && break || {
if [[ $n -lt $max ]]; then
((n++))
echo "Command failed. Attempt $n/$max:"
sleep $delay;
else
echo "The command has failed after $n attempts." >&2
return 1
fi
}
done
}