-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathdeploy-file.sh
executable file
·68 lines (59 loc) · 2.32 KB
/
deploy-file.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
#!/bin/bash
usage() {
echo "Deploy a local file to Artifactory keeping the same file name"
echo "Usage: $0 localFilePath targetFolder"
exit 1
}
if [ -z "$2" ]; then
usage
fi
localFilePath="$1"
targetFolder="$2"
artifactoryUser="admin"
artifactoryPassword="password"
if [ ! -f "$localFilePath" ]; then
echo "ERROR: local file $localFilePath does not exists!"
exit 1
fi
if command -v sha256sum > /dev/null && command -v sha1sum > /dev/null && command -v md5sum > /dev/null; then
# If the hashing tools provided by coreutils are installed then use them. They
# give a convenient output that always starts with the hash. Thus, given that
# we know how long each hash will be, we can just cut that bit off the start of
# the string and we have our hash.
md5Value="$(md5sum "$localFilePath")"
md5Value="${md5Value:0:32}"
sha1Value="$(sha1sum "$localFilePath")"
sha1Value="${sha1Value:0:40}"
sha256Value="$(sha256sum "$localFilePath")"
sha256Value="${sha256Value:0:65}"
elif command -v openssl > /dev/null; then
# If coreutils isn't installed then use openssl as a fallback, it's probably
# just as easy to use openssl all the time as it's more likely to be installed
# but the weird output syntax makes it less pleasant to strip the "non-hash"
# value out for use.
md5Value="$(openssl dgst -md5 "$localFilePath")"
md5Value="${md5Value##*)= }"
sha1Value="$(openssl dgst -sha1 "$localFilePath")"
sha1Value="${sha1Value##*)= }"
sha256Value="$(openssl dgst -sha256 "$localFilePath")"
sha256Value="${sha256Value##*)= }"
else
# There are a bunch of other ways to get the hash but it's probably just as
# simple to fail here and if you are on a system with some weird set of
# packages installed you can just customise this script.
echo "ERROR: install coreutils or openssl in order to find the md5, sha1 and \
sha256 hashes of your file."
exit 1
fi
fileName="${localFilePath##*/}"
echo "INFO: File: $localFilePath"
echo "INFO: MD5: $md5Value"
echo "INFO: SHA1: $sha1Value"
echo "INFO: SHA256 Hash: $sha256Value"
echo "INFO: Uploading $localFilePath to $targetFolder/$fileName"
curl -i -X PUT -u "$artifactoryUser:$artifactoryPassword" \
-H "X-Checksum-Md5: $md5Value" \
-H "X-Checksum-Sha1: $sha1Value" \
-H "X-Checksum-Sha256: $sha256Value" \
-T "$localFilePath" \
"$targetFolder/$fileName"