diff --git a/.gitignore b/.gitignore index 50ccb43..8f8bdda 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ coverage.out /bin *.db tags -.vscode \ No newline at end of file +.vscode +dist \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 49b8007..b176181 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,12 +7,12 @@ sudo: required before_script: - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls -script: go test -v -covermode=count -coverprofile=coverage.out +script: go test -v ./... -covermode=count -coverprofile=coverage.out after_script: - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN before_deploy: - go get github.com/mitchellh/gox - - gox -ldflags "-X main.AppVersion=$TRAVIS_TAG -X main.Sha=$TRAVIS_COMMIT" -output "dist/hmrc-lib-upgrade-{{.OS}}-{{.Arch}}" + - gox -ldflags "-X main.AppVersion=$TRAVIS_TAG -X main.Sha=$TRAVIS_COMMIT" -output "dist/slack-tube-service-{{.OS}}-{{.Arch}}" - sudo chmod +x ./dist/* deploy: provider: releases @@ -22,6 +22,12 @@ deploy: file: dist/* skip_cleanup: true on: - repo: thoeni/hmrc-lib-upgrade + repo: thoeni/slack-tube-service tags: true condition: "$TRAVIS_TAG =~ ^v[0-9]+(.[0-9]+)*" +deploy: + provider: script + script: deploy_prod.sh + on: + repo: thoeni/slack-tube-service + branch: master \ No newline at end of file diff --git a/circle.yml b/circle.yml index b1758e6..2b4c1ae 100644 --- a/circle.yml +++ b/circle.yml @@ -6,9 +6,12 @@ test: - go test -v -covermode=count -coverprofile=coverage.out post: - goveralls -coverprofile=coverage.out -service=circle-ci -repotoken $COVERALLS_REPO_TOKEN - deployment: - production: - branch: master + release: + tag: /^v[0-9]+(.[0-9]+)*/ commands: - - ./deploy_prod.sh + - go get github.com/mitchellh/gox + - go get github.com/tcnksm/ghr + - gox -ldflags "-X main.AppVersion=$CIRCLE_TAG -X main.Sha=$CIRCLE_SHA1" -output "dist/slack-tube-service-{{.OS}}-{{.Arch}}" + - ghr -t $GITHUB_TOKEN -u $CIRCLE_PROJECT_USERNAME -r $CIRCLE_PROJECT_REPONAME --replace $CIRCLE_TAG dist/ + - sh deploy_prod.sh diff --git a/deploy_prod.sh b/deploy_prod.sh index e7eead0..5801c02 100755 --- a/deploy_prod.sh +++ b/deploy_prod.sh @@ -1,5 +1,5 @@ #!/bin/bash ssh ec2-user@services.thoeni.io 'pkill -f slack-tube-service' ./mkbin.sh linux -scp bin/slack-tube-service-linux-amd64 ec2-user@services.thoeni.io:~/ +scp dist/slack-tube-service-linux-amd64 ec2-user@services.thoeni.io:~/ ssh ec2-user@services.thoeni.io screen -d -m './slack-tube-service-linux-amd64 &' diff --git a/main.go b/main.go index bbdd69d..52b11c3 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os" "time" + "flag" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" @@ -20,6 +21,8 @@ var tokenStore Repository var svc *dynamodb.DynamoDB var listenPort = os.Getenv("PORT") +var AppVersion string +var Sha string const defaultPort = "1123" @@ -56,7 +59,7 @@ var ( var tubeService TflService = TubeService{tfl.NewCachedClient(120)} -func init() { +func initialise() { if listenPort == "" { listenPort = defaultPort @@ -65,9 +68,9 @@ func init() { err := dbInit() if err != nil { log.Fatal("Couldn't initialise DB", err) - } else { - fmt.Printf("BoltDB initiliased (%v), bucket created!\n", tokenStore) + return } + fmt.Printf("BoltDB initiliased (%v), bucket created!\n", tokenStore) // DynamoDB sess := session.Must(session.NewSession()) @@ -81,6 +84,14 @@ func init() { func main() { + printVersion := flag.Bool("version", false, "Prints the version of this application") + flag.Parse() + if *printVersion { + fmt.Printf("Current version is: %s\nGit commit: %s", AppVersion, Sha) + return + } + + initialise() defer tokenStore.Close() _, authorisedTokenSet = tokenStore.RetrieveAllTokens() diff --git a/mkbin.sh b/mkbin.sh index c8b1493..9368ed4 100755 --- a/mkbin.sh +++ b/mkbin.sh @@ -1,15 +1,17 @@ #!/bin/bash +BUILD_VERSION=$(git describe --tags --always) +CID=$(git log --format="%H" -n 1) if [ "$1" = "linux" ] || [ "$1" = "" ]; then echo "Generating Linux binary" - env GOOS=linux GOARCH=amd64 go build -o bin/slack-tube-service-linux-amd64 + env GOOS=linux GOARCH=amd64 go build -ldflags "-X main.AppVersion=$BUILD_VERSION -X main.Sha=$CID" -o dist/slack-tube-service-$BUILD_VERSION-linux-amd64 fi if [ "$1" = "windows" ] || [ "$1" = "" ]; then echo "Generating Windows binary" - env GOOS=windows GOARCH=amd64 go build -o bin/slack-tube-service.exe + env GOOS=windows GOARCH=amd64 go build -ldflags "-X main.AppVersion=$BUILD_VERSION -X main.Sha=$CID" -o dist/slack-tube-service-$BUILD_VERSION-windows-amd64.exe fi if [ "$1" = "mac" ] || [ "$1" = "" ]; then echo "Generating MacOS binary" - env GOOS=darwin GOARCH=amd64 go build -o bin/slack-tube-service-darwin + env GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.AppVersion=$BUILD_VERSION -X main.Sha=$CID" -o dist/slack-tube-service-$BUILD_VERSION-darwin-amd64 fi echo "Done!" -ls -al bin/ +ls -al dist/ \ No newline at end of file