Skip to content

Commit

Permalink
Merge pull request #1351 from dedis/be1-go-nkocher-deploy
Browse files Browse the repository at this point in the history
Adds build info and automatic deployment
  • Loading branch information
pierluca authored Jan 23, 2023
2 parents 392b328 + 2a5fe7b commit 2fc9e58
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 4 deletions.
50 changes: 47 additions & 3 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Triggers a new deployment when a release starting with "fe1-" is created.

name: Release and deployment of fe1-web
name: Release and deployment

on:
release:
types: [published]

jobs:
deploy:
deploy_fe1:
name: Release and deploy fe1-web

if: ${{ startsWith(github.event.release.tag_name, 'fe1-') }}
runs-on: ubuntu-latest

Expand Down Expand Up @@ -68,4 +70,46 @@ jobs:
chmod 600 ./deploy_key
rsync -chav --delete \
-e 'ssh -p ${{secrets.FE1_DEPLOY_PORT}} -i ./deploy_key -o StrictHostKeyChecking=no' \
./web-build/ ${{env.dest}}
./web-build/ ${{env.dest}}
deploy_be1:
name: Release and deploy be1-go

if: ${{ startsWith(github.event.release.tag_name, 'be1-') }}
runs-on: ubuntu-latest

env:
base_folder: be1-go

defaults:
run:
working-directory: ./${{ env.base_folder }}

steps:
- uses: actions/checkout@v3

- name: Use go >= 1.19
uses: actions/setup-go@v3
with:
go-version: ">=1.19"

- name: build
run: |
make build
- name: Upload release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: ${{ env.base_folder }}/pop-*

# Use rsync to deploy the new website
- name: Sync
env:
dest: "popdemo@${{secrets.POPDEMO_DEPLOY_ADDR}}:be1-go/"
run: |
echo "${{secrets.POPDEMO_DEPLOY_KEY}}" > deploy_key
chmod 600 ./deploy_key
rsync -chav --delete \
-e 'ssh -p ${{secrets.POPDEMO_DEPLOY_PORT}} -i ./deploy_key -o StrictHostKeyChecking=no' \
./pop ${{env.dest}}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

# Go backend executable
be1-go/pop
be1-go/pop-*
be1-go/validation/protocol

# Karate files
Expand Down
13 changes: 12 additions & 1 deletion be1-go/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
version=$(shell git describe --abbrev=0 --tags || echo '0.0.0')
versionFlag="popstellar.Version=$(version)"
versionFile=$(shell echo $(version) | tr . _)
timeFlag="popstellar.BuildTime=$(shell date +'%d/%m/%y_%H:%M')"
shortsha=$(shell git rev-parse --short HEAD)
shaFlag="popstellar.ShortSHA=$(shortsha)"

.PHONY: test

build: protocol
go build -o pop ./cli
go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop ./cli
GOOS=linux GOARCH=amd64 go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop-linux-amd64-$(versionFile) ./cli
GOOS=darwin GOARCH=amd64 go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop-darwin-amd64-$(versionFile) ./cli
GOOS=darwin GOARCH=arm64 go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop-darwin-arm64-$(versionFile) ./cli
GOOS=windows GOARCH=amd64 go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop-windows-amd64-$(versionFile) ./cli

lint:
@go install honnef.co/go/tools/cmd/staticcheck@latest
Expand Down
15 changes: 15 additions & 0 deletions be1-go/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ import (
"github.com/rs/zerolog"
)

// Version contains the current or build version. This variable can be changed
// at build time with:
//
// go build -ldflags="-X 'popstellar.Version=v1.0.0'"
//
// Version should be fetched from git: `git describe --tags`
var Version = "unknown"

// BuildTime indicates the time at which the binary has been built. Must be set
// as with Version.
var BuildTime = "unknown"

// ShortSHA is the short SHA commit id. Must be set as with Version.
var ShortSHA = "unknown"

// EnvLogLevel is the name of the environment variable to change the logging
// level.
const EnvLogLevel = "LLVL"
Expand Down
27 changes: 27 additions & 0 deletions be1-go/network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"popstellar"
"popstellar/hub"
"popstellar/network/socket"
"sync"
Expand All @@ -18,6 +19,8 @@ type key int

const requestIDKey key = 0

const infoPath = "/infos"

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Expand Down Expand Up @@ -63,6 +66,8 @@ func NewServer(hub hub.Hub, addr string, port int, st socket.SocketType, log zer
mux := http.NewServeMux()
mux.HandleFunc(path, server.ServeHTTP)

mux.HandleFunc(infoPath, server.infoHandler)

nextRequestID := func() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
Expand Down Expand Up @@ -135,6 +140,28 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (s *Server) infoHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "not supported", http.StatusMethodNotAllowed)
return
}

w.Header().Set("Content-Type", "application/json")

fmtStr := `{
"version": "%s",
"commit": "%s",
"buildTime": "%s",
"hubType": "%s",
"socketType": "%s"
}`

resp := fmt.Sprintf(fmtStr, popstellar.Version, popstellar.ShortSHA,
popstellar.BuildTime, s.hub.Type(), s.st)

w.Write([]byte(resp))
}

// Shutdown shuts the HTTP server, signals the read and write pumps to
// close and waits for them to finish.
func (s *Server) Shutdown() error {
Expand Down
36 changes: 36 additions & 0 deletions be1-go/network/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package network

import (
"io"
"net/http"
"net/http/httptest"
"popstellar/crypto"
"popstellar/hub"
"popstellar/hub/standard_hub"
Expand All @@ -25,3 +27,37 @@ func TestServerStartAndShutdown(t *testing.T) {
require.NoError(t, err)
<-srv.Stopped
}

func TestInfoHandler(t *testing.T) {
s := Server{
hub: fakeHub{},
st: "fake",
}

rr := httptest.NewRecorder()
s.infoHandler(rr, &http.Request{Method: http.MethodGet})

res, err := io.ReadAll(rr.Body)
require.NoError(t, err)

expected := `{
"version": "unknown",
"commit": "unknown",
"buildTime": "unknown",
"hubType": "fake",
"socketType": "fake"
}`

require.Equal(t, expected, string(res))
}

// -----------------------------------------------------------------------------
// Utility functions

type fakeHub struct {
hub.Hub
}

func (fakeHub) Type() hub.HubType {
return "fake"
}

0 comments on commit 2fc9e58

Please sign in to comment.