-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·152 lines (124 loc) · 3.82 KB
/
build.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
#
# build.sh - Builds a Docker image for the SUNET SignService Integration REST application.
#
# Author: Martin Lindström <[email protected]>
#
usage() {
echo "Usage: $0 [options...]" >&2
echo
echo " -v, --version Version for artifact to download"
echo " -i, --image Name of image to create (default is signservice-integration-rest)"
echo " -t, --tag Optional docker tag for image"
echo " -c, --clear Clears the target directory after a successful build (default is to keep it)"
echo " -h, --help Prints this help"
echo
}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
MAVEN_REPO_URL=https://repo1.maven.org/maven2
ARTIFACT_NAME=signservice-integration-rest
ARTIFACT_REPO_PATH=/se/idsec/signservice/integration/${ARTIFACT_NAME}/
FILE_EXTENSION=jar
USERNAME=""
VERSION=""
IMAGE_NAME=""
DOCKER_TAG=""
CLEAR_FLAG="false"
while :
do
case "$1" in
-h | --help)
usage
exit 0
;;
-v | --version)
VERSION="$2"
shift 2
;;
-i | --image)
IMAGE_NAME="$2"
shift 2
;;
-t | --tag)
DOCKER_TAG="$2"
shift 2
;;
-c | --clear)
CLEAR_FLAG="true"
shift 1
;;
--)
shift
break;
;;
-*)
echo "Error: Unknown option: $1" >&2
usage
exit 0
;;
*)
break
;;
esac
done
if [ "$VERSION" == "" ]; then
echo "Error: Missing version" >&2
usage
exit 1
fi
if [ "$IMAGE_NAME" == "" ]; then
IMAGE_NAME=signservice-integration-rest
echo "Docker image name not given, defaulting to $IMAGE_NAME" >&1
fi
if [ -d "$SCRIPT_DIR/target" ]; then
rm -rf "$SCRIPT_DIR/target"
fi
mkdir "$SCRIPT_DIR/target"
#
# Download distribution and signature
#
echo "Downloading ${ARTIFACT_NAME}-${VERSION}.${FILE_EXTENSION} ..."
echo " The artifact is large, so be patient ..."
HTTP_STATUS=$(curl --silent -o ${SCRIPT_DIR}/target/${ARTIFACT_NAME}.${FILE_EXTENSION} -w "%{http_code}" ${MAVEN_REPO_URL}${ARTIFACT_REPO_PATH}${VERSION}/${ARTIFACT_NAME}-${VERSION}.${FILE_EXTENSION})
if [ "$HTTP_STATUS" != "200" ]; then
echo "Failed to download $ARTIFACT_NAME from Maven - got HTTP STATUS $HTTP_STATUS"
exit 1
fi
echo "Downloading ${ARTIFACT_NAME}-${VERSION}.${FILE_EXTENSION}.asc (signature) ..."
HTTP_STATUS=$(curl --silent --user ${USERNAME}:${PASSWD} -o ${SCRIPT_DIR}/target/${ARTIFACT_NAME}.${FILE_EXTENSION}.asc -w "%{http_code}" ${MAVEN_REPO_URL}${ARTIFACT_REPO_PATH}${VERSION}/${ARTIFACT_NAME}-${VERSION}.${FILE_EXTENSION}.asc)
if [ "$HTTP_STATUS" != "200" ]; then
echo "Failed to download signed file for $ARTIFACT_NAME from Maven - got HTTP STATUS $HTTP_STATUS"
exit 1
fi
#
# Check signature
#
# Build a keyring containing the keys stored in the 'keys' directory
echo "Verifying signature on ${ARTIFACT_NAME}-${VERSION}.${FILE_EXTENSION} ..."
echo "Building trust keyring ..."
gpg --no-default-keyring --keyring $SCRIPT_DIR/target/maven-keyring.gpg --fingerprint > /dev/null 2>&1
for keyfile in $SCRIPT_DIR/keys/*; do
echo " Adding key file $keyfile ..."
gpg --no-default-keyring --keyring=$SCRIPT_DIR/target/maven-keyring.gpg --import $keyfile > /dev/null 2>&1
done
echo
gpg --no-default-keyring --keyring $SCRIPT_DIR/target/maven-keyring.gpg --verify ${SCRIPT_DIR}/target/${ARTIFACT_NAME}.${FILE_EXTENSION}.asc ${SCRIPT_DIR}/target/${ARTIFACT_NAME}.${FILE_EXTENSION}
echo
if [ $? -eq 0 ]; then
echo "Signature verification successful"
else
echo "Signature did not verify correctly"
exit 1
fi
#
# Build Docker image
#
if [ "$DOCKER_TAG" != "" ]; then
IMAGE_NAME="$IMAGE_NAME:$DOCKER_TAG"
fi
echo "Building Docker image '$IMAGE_NAME' ..."
docker build --no-cache=true -f "$SCRIPT_DIR/Dockerfile" -t $IMAGE_NAME $SCRIPT_DIR
if [ "$CLEAR_FLAG" == "true" ]; then
rm -rf "$SCRIPT_DIR/target"
fi
echo "Done"