-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
188 lines (171 loc) · 6.19 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
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
187
188
#!/bin/bash
#
## CI build script for projects based on gfoidl's schema.
#
# Arguments:
# build builds the solution
# test runs all tests under ./tests
# deploy deploys to $2, which must be either nuget or myget
# * when CI_SKIP_DEPLOY is set, no deploy is done
# * when DEBUG is set, the action is echoed and not done
#
# Environment-Variables:
# NAME project-name used for packaging
# CI_BUILD_NUMBER build-number used for version-info
# BRANCH_NAME branch the commit is on
# TAG_NAME tag the commit is on
# CI_SKIP_DEPLOY when set no deploy is done, even if deploy is called
# DEBUG when set deploy is simulted by echoing the action
#
# Functions (sorted alphabetically):
# build builds the solution
# deploy deploys the solution either to nuget or myget
# main entry-point
# setBuildEnv sets the environment variables regarding the build-environment
# test runs tests for projects in ./tests
# _deployCore helper -- used by deploy
# _testCore helper -- used by test
#
# Exit-codes:
# 1000 NAME environment variable is not set to project-name (for packaging)
# 1001 deploy target is neither 'nuget' nor 'myget', so it is unknown
# 1002 no args given for script, help is displayed and exited
# $? exit-code for build-step is returned unmodified
#------------------------------------------------------------------------------
set -e
#------------------------------------------------------------------------------
help() {
echo "build script"
echo ""
echo "Arguments:"
echo " build builds the solution"
echo " test runs all tests under ./tests"
echo " deploy [nuget|myget] deploys to the destination"
}
#------------------------------------------------------------------------------
setBuildEnv() {
if [[ -z "$NAME" ]]; then
echo "NAME environment variable must be set to project-name (for packaging)"
exit 1000
fi
if [[ -z "$CI_BUILD_NUMBER" ]]; then
if [[ -n "$CIRCLECI" ]]; then
export CI_BUILD_NUMBER=$CIRCLE_BUILD_NUM
export BRANCH_NAME=$CIRCLE_BRANCH
export TAG_NAME=$CIRCLE_TAG
elif [[ -n "$TRAVIS" ]]; then
export CI_BUILD_NUMBER=$TRAVIS_BUILD_NUMBER
export BRANCH_NAME=$(if [[ -n "$TRAVIS_PULL_REQUEST_BRANCH" ]]; then echo "$TRAVIS_PULL_REQUEST_BRANCH"; else echo "$TRAVIS_BRANCH"; fi)
export TAG_NAME=$TRAVIS_TAG
elif [[ -n "$BITBUCKET_BUILD_NUMBER" ]]; then
export CI_BUILD_NUMBER=$BITBUCKET_BUILD_NUMBER
export BRANCH_NAME=$BITBUCKET_BRANCH
export TAG_NAME=$BITBUCKET_TAG
fi
fi
# BuildNumber is used by MsBuild for version information.
# ci tools clone usually to depth 50, so this is not good
#export BuildNumber=$(git log --oneline | wc -l)
export BuildNumber=$CI_BUILD_NUMBER
export VersionSuffix="preview-$CI_BUILD_NUMBER"
if [[ -n "$TAG_NAME" ]]; then
if [[ "$TAG_NAME" =~ ^v[0-9]\.[0-9]\.[0-9]$ ]]; then
unset VersionSuffix
fi
fi
echo "-------------------------------------------------"
echo "Branch: $BRANCH_NAME"
echo "Tag: $TAG_NAME"
echo "BuildNumber: $BuildNumber"
echo "VersionSuffix: $VersionSuffix"
echo "-------------------------------------------------"
}
#------------------------------------------------------------------------------
build() {
dotnet restore
dotnet build -c Release --no-restore
}
#------------------------------------------------------------------------------
_testCore() {
local testFullName
local testDir
local testName
local testResultName
testFullName="$1"
testDir=$(dirname "$testFullName")
testName=$(basename "$testFullName")
testResultName="$testName-$(date +%s).trx"
echo ""
echo "test fullname: $testFullName"
echo "testing: $testName..."
echo "test result name: $testResultName"
echo ""
dotnet test -c Release --no-build --logger "trx;LogFileName=$testResultName" "$testFullName"
local result=$?
mkdir -p "./tests/TestResults"
mv "$testDir/TestResults/$testResultName" "./tests/TestResults/$testResultName"
if [[ $result != 0 ]]; then
exit $result
fi
}
#------------------------------------------------------------------------------
test() {
local testDir
testDir="./tests"
if [[ ! -d "$testDir" ]]; then
echo "test-directory not existing -> no test need to run"
return
fi
export -f _testCore
find "$testDir" -name "*.csproj" -print0 | xargs -0 -n1 bash -c '_testCore "$@"' _
}
#------------------------------------------------------------------------------
_deployCore() {
dotnet pack -o "$(pwd)/NuGet-Packed" --no-build -c Release "source/$NAME"
ls -l ./NuGet-Packed
echo ""
if [[ -z "$DEBUG" ]]; then
dotnet nuget push --source "$1" --api-key "$2" -t 60 ./NuGet-Packed/*.nupkg
else
echo "DEBUG: simulate nuget push to $1"
fi
}
#------------------------------------------------------------------------------
deploy() {
if [[ -n "$CI_SKIP_DEPLOY" ]]; then
echo "Skipping deploy because CI_SKIP_DEPLOY is set"
return
fi
if [[ "$1" == "nuget" ]]; then
_deployCore "$NUGET_FEED" "$NUGET_KEY"
elif [[ "$1" == "myget" ]]; then
_deployCore "$MYGET_FEED" "$MYGET_KEY"
else
echo "Unknown deploy target '$1', aborting"
exit 1001
fi
}
#------------------------------------------------------------------------------
main() {
setBuildEnv
case "$1" in
build) build
;;
test) test
;;
deploy)
shift
deploy "$1"
;;
*)
help
exit
;;
esac
}
#------------------------------------------------------------------------------
if [[ $# -lt 1 ]]; then
help
exit 1002
fi
main $*