forked from mageddo/dns-proxy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder
executable file
·121 lines (89 loc) · 3.18 KB
/
builder
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
#!/bin/sh
set -e
CUR_DIR=`pwd`
APP_VERSION=$(cat VERSION)
REPO_URL=mageddo/dns-proxy-server
create_release(){
# release notes
DESC=$(cat RELEASE-NOTES.md | awk 'BEGIN {RS="|"} {print substr($0, 0, index(substr($0, 3), "###"))}' | sed ':a;N;$!ba;s/\n/\\r\\n/g') && \
PAYLOAD='{
"tag_name": "%s",
"target_commitish": "%s",
"name": "%s",
"body": "%s",
"draft": false,
"prerelease": true
}'
PAYLOAD=$(printf "$PAYLOAD" $APP_VERSION $CURRENT_BRANCH $APP_VERSION "$DESC")
TAG_ID=$(curl -i -s -f -X POST "https://api.github.com/repos/$REPO_URL/releases?access_token=$REPO_TOKEN" \
--data "$PAYLOAD" | grep -o -E 'id": [0-9]+'| awk '{print $2}' | head -n 1)
}
upload_file(){
curl --data-binary "@$SOURCE_FILE" -i -w '\n' -f -s -X POST -H 'Content-Type: application/octet-stream' \
"https://uploads.github.com/repos/$REPO_URL/releases/$TAG_ID/assets?name=$TARGET_FILE&access_token=$REPO_TOKEN"
}
case $1 in
setup-repository )
git remote remove origin && git remote add origin https://${REPO_TOKEN}@github.com/$REPO_URL.git
git checkout -b build_branch ${CURRENT_BRANCH}
echo "> Repository added, travisBranch=${CURRENT_BRANCH}"
;;
upload-release )
if [ "$REPO_TOKEN" = "" ] ; then echo "REPO_TOKEN cannot be empty"; exit 1; fi
if [ "`git config user.email || echo ''`" = "" ]; then
echo '> custom config'
git config user.name `git config user.name || echo 'CI BOT'`
git config user.email `git config user.email || echo 'ci-bot@mageddo.com'`
fi
echo '> config'
git config -l
echo ''
REMOTE="https://${REPO_TOKEN}@github.com/${REPO_URL}.git"
git checkout -b build_branch ${CURRENT_BRANCH}
echo "> Repository added, currentBranch=${CURRENT_BRANCH}"
git commit -am "Releasing ${APP_VERSION}" || true
git tag ${APP_VERSION}
git push "$REMOTE" "build_branch:${CURRENT_BRANCH}"
git status
echo "> Branch pushed - Branch $CURRENT_BRANCH"
create_release
echo "> Release created with id $TAG_ID"
SOURCE_FILE="build/dns-proxy-server-$APP_VERSION.tgz"
TARGET_FILE=dns-proxy-server-$APP_VERSION.tgz
echo "> Source file hash"
md5sum $SOURCE_FILE && ls -lha $SOURCE_FILE
upload_file
;;
apply-version )
# updating files version
sed -i -E "s/(dns-proxy-server.*)[0-9]+\.[0-9]+\.[0-9]+/\1$APP_VERSION/" docker-compose.yml
sed -i -E "s/[0-9]+\.[0-9]+\.[0-9]+/$APP_VERSION/g" Dockerfile.hub
;;
build )
echo "> Starting build"
rm -rf build/
mkdir -p build/
go test -v -race -cover -ldflags "-X github.com/mageddo/dns-proxy-server/flags.version=test" ./.../
go build -v -o build/dns-proxy-server -ldflags "-X github.com/mageddo/dns-proxy-server/flags.version=$APP_VERSION"
cp -r static build/
cd build/
tar -czvf dns-proxy-server-${APP_VERSION}.tgz *
echo "> Build success"
;;
validate-release )
if git rev-parse "$APP_VERSION^{}" >/dev/null 2>&1; then
echo "> Version already exists $APP_VERSION"
exit 3
fi
;;
deploy-ci )
echo "> build started, current branch=$CURRENT_BRANCH"
if [ "$CURRENT_BRANCH" = "master" ]; then
echo "> deploying new version"
builder validate-release || exit 0 && builder apply-version && builder build && builder upload-release
else
echo "> building candidate"
builder build
fi
;;
esac