forked from nirui/sshwifty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.ci.sh
148 lines (112 loc) · 5.68 KB
/
.ci.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
#!/bin/sh
PATH=$PATH:"$(go env GOPATH)/bin"
GO111MODULE=on
ASC_URL=https://keybase.io/nirui/pgp_keys.asc
VERSION_VARIABLE=github.com/nirui/sshwifty/application.version
BUILD_TARGETS="darwin/amd64 windows/386 windows/amd64 openbsd/386 openbsd/amd64 openbsd/arm openbsd/arm64 freebsd/386 freebsd/amd64 freebsd/arm freebsd/arm64 linux/386 linux/amd64 linux/arm linux/arm64 linux/riscv64 linux/ppc64 linux/ppc64le linux/mips linux/mipsle linux/mips64 linux/mips64le"
DOCKER_BUILD_TARGETS="linux/amd64,linux/arm/v7,linux/arm64"
DOCKER_CLI_EXPERIMENTAL=enabled
DOCKER_BUILDKIT=1
SSHWIFTY_VERSION=$(git describe --always --dirty='*' --tag)
SSHWIFTY_COMMIT=$(git describe --always)
SSHWIFTY_RELEASE=$([ "$(echo $SSHWIFTY_VERSION | grep -oP ^[0-9]+\.[0-9]+\.[0-9]+\-[a-zA-Z0-9]+\-release$)" = '' ] || echo 'yes')
SSHWIFTY_DEPLOY=$([ "$SSHWIFTY_RELEASE" != 'yes' ] || echo 'yes')
SSHWIFTY_DOCKER_IMAGE_TAG="$DOCKER_HUB_USER/sshwifty"
SSHWIFTY_DOCKER_IMAGE_PUSH_TAG="$SSHWIFTY_DOCKER_IMAGE_TAG:$SSHWIFTY_VERSION"
SSHWIFTY_DOCKER_IMAGE_PUSH_TAG_LATEST="$SSHWIFTY_DOCKER_IMAGE_TAG:latest"
child() {
cpid=""
ret=0
i=0
echo "+ Spawning $# childs ..."
for c in "$@"; do
( (((((eval "$c"; echo $? >&3) | sed "s/^/\|------ ($i) /" >&4) 2>&1 | sed "s/^/\|------ ($i)!/" >&2) 3>&1) | (read xs; exit $xs)) 4>&1) & ppid=$!
cpid="$cpid $ppid"
echo "+ Child $i (PID $ppid): $c ..."
i=$((i+1))
done
for c in $cpid; do
wait $c
cret=$?
[ $cret -eq 0 ] && continue
echo "* Child PID $c has failed." >&2
ret=$cret
done
return $ret
}
retry() {
res=0
for i in $(seq 0 36); do
$@
res=$?
[ $res -eq 0 ] && return $res || sleep 10
done
return $res
}
catch() {
(eval '"$@"')
res=$?
[ $res -eq 0 ] && return $res
echo "Command \"$@\" has failed. Exit code: $res"
exit $res
}
SSHWIFTY_LAST_TAG_NAME=HEAD~1
if [ "$SSHWIFTY_DEPLOY" = 'yes' ]; then
echo 'Downloading compile & deploy tools ...'
[ "$(which ghr)" != '' ] || catch retry go get -v github.com/tcnksm/ghr
[ "$(which gox)" != '' ] || catch retry go get -v github.com/mitchellh/gox
echo 'Fetching extra references from the repository ...'
GIT_TAG_FETCH_PARAM=
if [ "$(git rev-parse --is-shallow-repository)" ]; then
echo 'Shallow clone detected, will unshallow ...'
GIT_TAG_FETCH_PARAM='--unshallow'
fi
catch retry git fetch --tags "$GIT_TAG_FETCH_PARAM"
SSHWIFTY_LAST_TAG_NAME=$(git describe --tags --abbrev=0 --always HEAD~1)
fi
echo "Version: $SSHWIFTY_VERSION"
echo "Files: $(pwd)" && ls -la
export
git status --short
git log --pretty=format:"- %h %s - (%an) %GK %G?" "$SSHWIFTY_LAST_TAG_NAME"..HEAD
catch retry npm install
catch npm run generate
catch go vet ./...
catch npm run testonly
if [ "$SSHWIFTY_DEPLOY" = 'yes' ]; then
catch child \
'
docker login -u "$DOCKER_HUB_USER" -p "$DOCKER_HUB_PASSWORD" &&
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes &&
docker buildx create --use --driver docker-container --name buildx-instance &&
docker buildx inspect --bootstrap &&
docker buildx ls &&
docker buildx build --tag "$SSHWIFTY_DOCKER_IMAGE_PUSH_TAG" --tag "$SSHWIFTY_DOCKER_IMAGE_PUSH_TAG_LATEST" --platform "$DOCKER_BUILD_TARGETS" --builder buildx-instance --build-arg GOMIPS=softfloat --build-arg CUSTOM_COMMAND="$DOCKER_CUSTOM_COMMAND" --progress plain --push .
' \
'
mkdir -p ./.tmp/generated ./.tmp/release &&
curl "$ASC_URL" > ./.tmp/release/GPG.asc &&
gpg --import ./.tmp/release/GPG.asc &&
git archive --format tar --output ./.tmp/release/src HEAD &&
CGO_ENABLED=0 GOMIPS=softfloat gox -ldflags "-s -w -X $VERSION_VARIABLE=$SSHWIFTY_VERSION" -osarch "$BUILD_TARGETS" -output "./.tmp/release/{{.Dir}}_${SSHWIFTY_VERSION}_{{.OS}}_{{.Arch}}/{{.Dir}}_{{.OS}}_{{.Arch}}" &&
echo "# Version $SSHWIFTY_VERSION" > ./.tmp/release/Note &&
echo >> ./.tmp/release/Note &&
echo "Updates introduced since $SSHWIFTY_LAST_TAG_NAME" >> ./.tmp/release/Note &&
git log --pretty=format:"- %h %s - (%an) %GK %G?" "$SSHWIFTY_LAST_TAG_NAME"..HEAD >> ./.tmp/release/Note &&
echo '"'"'#!/bin/sh'"'"' > ./.tmp/generated/prepare.sh &&
echo '"'"'echo Preparing for $1 ... && \'"'"' >> ./.tmp/generated/prepare.sh &&
echo '"'"'(cd $1/ && find . -maxdepth 1 -type f ! -name "SUM.*" -exec sha512sum {} \; > SUM.sha512) && \'"'"' >> ./.tmp/generated/prepare.sh &&
echo '"'"'(cp -v ./*.md $1/) && \'"'"' >> ./.tmp/generated/prepare.sh &&
echo '"'"'(cp -v ./*.example.json $1/) && \'"'"' >> ./.tmp/generated/prepare.sh &&
echo '"'"'(cp -v ./.tmp/release/GPG.asc $1/) && \'"'"' >> ./.tmp/generated/prepare.sh &&
echo '"'"'(cp -v ./.tmp/release/Note $1/) && \'"'"' >> ./.tmp/generated/prepare.sh &&
echo '"'"'(cp -v ./.tmp/release/src $1/) && \'"'"' >> ./.tmp/generated/prepare.sh &&
echo '"'"'(cd $1/ && tar zpcvf ../$(basename $(pwd)).tar.gz * --owner=0 --group=0)'"'"' >> ./.tmp/generated/prepare.sh &&
chmod +x ./.tmp/generated/prepare.sh &&
find ./.tmp/release/ -maxdepth 1 -type d ! -name "release" -exec ./.tmp/generated/prepare.sh {} \; &&
find ./.tmp/release/ -maxdepth 1 -type d ! -name "release" -exec rm {} -rf \; &&
find ./.tmp/release/ -maxdepth 1 -type f -name "*.tar.gz" -execdir sha512sum {} \; > ./.tmp/release/SUM.sha512 &&
cat ./.tmp/release/SUM.sha512 &&
ghr -t "$GITHUB_USER_TOKEN" -u "$GITHUB_USER" -n "$SSHWIFTY_VERSION-prebuild" -b "$(cat ./.tmp/release/Note)" -delete -prerelease "$SSHWIFTY_VERSION-prebuild" ./.tmp/release
'
fi