This repository has been archived by the owner on May 7, 2024. It is now read-only.
forked from Diogomrol/gocd-s3-artifact-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks
executable file
·186 lines (171 loc) · 5.66 KB
/
tasks
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
set -Eeuo pipefail
RELEASER_VERSION="2.1.0"
SECRET_OPS_VERSION="0.6.2"
SECRET_OPS_FILE="ops/secret-ops"
SECRET_OPS_TAR_FILE="ops/secret-ops-${SECRET_OPS_VERSION}.tar.gz"
RELEASER_FILE="ops/releaser-${RELEASER_VERSION}"
VAULT_ADDR="https://vault.kudulab.io:8200"
mkdir -p ops
if [[ ! -f $RELEASER_FILE ]];then
wget --quiet -O $RELEASER_FILE https://github.com/kudulab/releaser/releases/download/${RELEASER_VERSION}/releaser
fi
source $RELEASER_FILE
if [[ ! -f $SECRET_OPS_TAR_FILE ]];then
wget --quiet -O $SECRET_OPS_TAR_FILE https://github.com/kudulab/secret-ops/releases/download/${SECRET_OPS_VERSION}/secret-ops.tar.gz
tar -xf $SECRET_OPS_TAR_FILE -C ops
fi
source $SECRET_OPS_FILE
GOCD_ENDPOINT="http://gocd:8153/go"
KUDU_SERVICE=gocd-s3-artifact-plugin-test
function tf_ops {
operation=$1
cd e2e/terraform/
terraform init -backend-config key=kudu-${KUDU_SERVICE}/terraform.tfstate
terraform get # modules
if [[ "${operation}" == "create" ]]; then
terraform plan -out="kudu_deployment.tfplan"
elif [[ "${operation}" == "destroy" ]]; then
terraform plan -out="kudu_deployment.tfplan" -destroy
else
echo "Unknown operation: ${operation}"
exit 1
fi
terraform apply kudu_deployment.tfplan
terraform output -json > tf-out.json
}
function source_tf {
AWS_BUCKET=$(cat e2e/terraform/tf-out.json | jq -r '.s3_bucket.value')
AWS_ACCESS_KEY=$(cat e2e/terraform/tf-out.json | jq -r '.s3_key_id.value')
AWS_SECRET_ACCESS_KEY=$(cat e2e/terraform/tf-out.json | jq -r '.s3_key.value')
export AWS_BUCKET
export AWS_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY
}
command="$1"
case "${command}" in
_tf_apply)
tf_ops "$2"
;;
tf_apply)
set +u
if [ -z "${AWS_ACCESS_KEY_ID}" ] || [ -z "${AWS_SECRET_ACCESS_KEY}" ]; then
echo "AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY must be set."
if [[ -n "${VAULT_TOKEN}" ]]; then
echo "Trying to get secrets from vault"
AWS_ACCESS_KEY_ID=$(vault read -field=key_id secret/gocd-s3-plugin/aws)
AWS_SECRET_ACCESS_KEY=$(vault read -field=secret_key secret/gocd-s3-plugin/aws)
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
else
exit 1
fi
fi
dojo -c e2e/terraform/Dojofile "./tasks _tf_apply $2"
;;
wait_online)
sleep 10
for i in {1..200}; do
HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" \
"${GOCD_ENDPOINT}/api/v1/health" --insecure \
-H 'Accept: application/vnd.go.cd.v1+json' \
-H 'Content-Type: application/json' \
)
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
if [ ! $HTTP_STATUS -eq 200 ]; then
echo "GoCD is not up yet" >&2
sleep 1
continue
fi
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
echo $HTTP_BODY
exit 0
done
;;
_setup_gocd)
./tasks wait_online
bash
;;
setup_gocd)
export GOCDCLI_SERVER_URL=$GOCD_ENDPOINT
dojo "./tasks _setup_gocd"
;;
_build_test)
gradle check assemble
;;
build_test)
source_tf
dojo "./tasks _build_test"
;;
set_version)
set +u
releaser::bump_changelog_version "$2" "$3"
next_version=$(releaser::get_last_version_from_changelog "${changelog_file}")
releaser::set_version_in_file "pluginVersion = " "build.gradle" "${next_version}"
;;
bump)
set +u
releaser::bump_changelog_version "" true
next_version=$(releaser::get_last_version_from_changelog "${changelog_file}")
releaser::set_version_in_file "pluginVersion = " "build.gradle" "${next_version}"
;;
verify_version)
releaser::verify_release_ready
;;
prepare_release)
next_version=$(releaser::get_last_version_from_changelog "${changelog_file}")
releaser::set_version_in_changelog "${changelog_file}" "${next_version}" false
releaser::set_version_in_file "pluginVersion = " "build.gradle" "${next_version}"
;;
commit)
git add "${changelog_file}"
git add "build.gradle"
git commit --author "Tomasz Setkowski <tom@ai-traders.com>" -m "Version bump"
;;
github_release)
set +u
if [[ -z "${GITHUB_TOKEN}" ]]; then
echo "GITHUB_TOKEN must be set for release"
if [[ -n "${VAULT_TOKEN}" ]]; then
echo "Trying to get GITHUB_TOKEN from vault"
GITHUB_TOKEN=$(vault read -field=token secret/gocd-s3-plugin/github)
export GITHUB_TOKEN
else
exit 1
fi
fi
releaser::prepare_github_release_bin
VERSION=$(ls build/libs/s3-artifact-plugin-*.jar | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
changelog_version=$(releaser::get_last_version_from_changelog "${changelog_file}")
if [ $changelog_version != $VERSION ]; then
echo "changelog version $changelog_version does not match file version $VERSION"
exit 2
fi
$GHRELEASE_BIN release \
--user Diogomrol \
--repo gocd-s3-artifact-plugin \
--tag $VERSION \
--name $VERSION \
--pre-release
cd build/libs/
for file in s3-artifact-plugin-*.jar*; do
$GHRELEASE_BIN upload \
--user Diogomrol \
--repo gocd-s3-artifact-plugin \
--tag $VERSION \
--name "$file" \
--file "$file"
done
;;
generate_vault_token)
vault_token=$(vault token create -orphan -ttl=168h -policy=gocd-s3-plugin -field token -metadata gocd_renew=true)
secured_token_gocd=$(secret_ops::encrypt_with_gocd_top "${vault_token}")
echo "Generated token: ${vault_token} and encrypted by GoCD server"
secret_ops::insert_vault_token_gocd_yaml "${secured_token_gocd}"
;;
*)
echo "Invalid command: '${command}'"
exit 1
;;
esac
set +e