-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild.sh
executable file
·82 lines (66 loc) · 2.09 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
#!/bin/bash
set -e
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64
# Might not always have services passed down -
# Github Actions needs GITHUB_TOKEN and for PR forks we don't have that.
# Check for PR number as the pull api call fails if not in a PR obviously.
if [ -n "$PR_NUMBER" ] && [ -z "$1" ]; then
echo "Getting files from github api to detect files changed "
SERVICES=($(find . -name main.go | cut -c 3- | rev | cut -c 9- | rev))
URL="https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/files"
FILES=($(curl -s -X GET -G $URL | jq -r '.[] | .filename'))
else
SERVICES=($1) # e.g. "foobar barfoo helloworld"
fi
rootDir=$(pwd)
PARAMS="$1"
function containsElement () {
# If file change was passed down, this function always returns true
if [ -n "$PARAMS" ]; then
return 0;
fi
local e match="$1"
shift
for e; do [[ "$e" =~ ^$match ]] && return 0; done
return 1
}
function build {
dir=$1
EXIT_CODE=0
# We don't want to fail the whole script if contains fails
containsElement $dir "${FILES[@]}" || EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo Building $dir
else
echo Skipping $dir
return 0
fi
cd $dir
# build the proto buffers
#find . -name "*.proto" | xargs --no-run-if-empty protoc --proto_path=. --micro_out=. --go_out=.
# build the binaries
go build -ldflags="-s -w" -o service .
cp $rootDir/dumb-init/dumb-init dumb-init
# build the docker image
tag=docker.pkg.github.com/m3o/services/$(echo $dir | tr / -)
docker build . -t $tag -f $rootDir/.github/workflows/Dockerfile
if [ -n "$1" ] && [ "$BRANCH" = "refs/heads/master" ]; then
# push the docker image
echo Pushing $tag
docker push $tag
else
echo "Skipping pushing docker images due to lack of credentials"
fi
# remove the binaries
rm service
rm dumb-init
# go back to the top level dir
cd $rootDir
}
# This must always be deployed even if it has not changed
# build "explore/web"
for dir in "${SERVICES[@]}"; do
build $dir
done