Skip to content

Commit

Permalink
chore: add versioning release, use new fly deploy strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
luigibarbato committed Oct 7, 2023
1 parent fa27c19 commit 330cde8
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export UNCONDITIONAL_API_ADDRESS="localhost"
export UNCONDITIONAL_API_SOURCE_REPO="source"
export UNCONDITIONAL_API_SOURCE_CLIENT_KEY="secret"
export UNCONDITIONAL_API_LOG_ENV="dev"
export UNCONDITIONAL_API_BUILD_COMMIT_VERSION=
export UNCONDITIONAL_API_BUILD_RELEASE_VERSION=
3 changes: 2 additions & 1 deletion .envrc.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export UNCONDITIONAL_API_ADDRESS="localhost"
export UNCONDITIONAL_API_SOURCE_REPO="source"
export UNCONDITIONAL_API_SOURCE_CLIENT_KEY="secret"
export UNCONDITIONAL_API_LOG_ENV="dev"

export UNCONDITIONAL_API_BUILD_COMMIT_VERSION=
export UNCONDITIONAL_API_BUILD_RELEASE_VERSION=
18 changes: 0 additions & 18 deletions .github/workflows/deploy.yaml

This file was deleted.

34 changes: 34 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Release
on:
release:
types: [published]

env:
UNCONDITIONAL_API_SOURCE_CLIENT_KEY: ${{ secrets.UNCONDITIONAL_API_SOURCE_CLIENT_KEY }}
UNCONDITIONAL_API_BUILD_COMMIT_VERSION: ${{ github.sha }}
UNCONDITIONAL_API_BUILD_RELEASE_VERSION: ${{ github.event.release.tag_name }}
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

jobs:
deploy-release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version-file: go.mod
cache: true
- name: Prepare tools
run: make install-tools
- name: Get dependencies
run: go get -v -t -d ./...
- name: Install dependencies
run: go mod download
- name: Unit test
run: make test-unit
- name: Integration test
run: make test-integration
- name: Deploy
run: chmod +x scripts/deploy.sh && sh scripts/deploy.sh
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ COPY go.mod go.sum ./
RUN go mod download
COPY . .

ARG UNCONDITIONAL_API_BUILD_COMMIT_VERSION
ARG UNCONDITIONAL_API_BUILD_RELEASE_VERSION

ENV CGO_ENABLED=0
RUN go build -o main .
ENV UNCONDITIONAL_API_BUILD_COMMIT_VERSION=${UNCONDITIONAL_API_BUILD_COMMIT_VERSION}
ENV UNCONDITIONAL_API_BUILD_RELEASE_VERSION=${UNCONDITIONAL_API_BUILD_RELEASE_VERSION}

RUN go build --ldflags "-X 'main.version=${UNCONDITIONAL_API_BUILD_RELEASE_VERSION}' -X 'main.gitCommit=${UNCONDITIONAL_API_BUILD_COMMIT_VERSION}'" --tags=release -o main .

FROM builder as data
WORKDIR /data
Expand Down
31 changes: 3 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,21 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/unconditionalday/server/internal/version"
cobrax "github.com/unconditionalday/server/internal/x/cobra"
)

var (
releaseVersion = "unknown"
gitCommit = "unknown"
)

func NewRootCommand() *cobra.Command {
func NewRootCommand(versions map[string]string) *cobra.Command {
v := version.Build{
Version: releaseVersion,
Commit: gitCommit,
Version: versions["releaseVersion"],
Commit: versions["gitCommit"],
}

rootCmd := &cobra.Command{
Use: "unconditional-api [command]",
Short: "Unconditional api engine that indexes and serves the content",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(rootCmd *cobra.Command, _ []string) error {
commitVersion := cobrax.Flag[string](rootCmd, "build-commit-version").(string)
if commitVersion != "" {
v.Commit = commitVersion
}

releaseVersion := cobrax.Flag[string](rootCmd, "build-release-version").(string)
if releaseVersion != "" {
v.Version = releaseVersion
}

return nil
},
}

rootCmd.Flags().String("build-commit-version", "", "The Build GH commit version")
rootCmd.Flags().String("build-release-version", "", "The Build release version")

envPrefix := "UNCONDITIONAL_API"
cobrax.BindFlags(rootCmd, cobrax.InitEnvs(envPrefix), envPrefix)

rootCmd.AddCommand(NewServeCommand(v))
rootCmd.AddCommand(NewIndexCmd())
rootCmd.AddCommand(NewSourceCommand())
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ require (
)

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/SlyMarbo/rss v1.0.5
github.com/blevesearch/bleve/v2 v2.3.10
github.com/loadsmart/calver-go v0.0.0-20230323142215-56cf73a68e8a
github.com/microcosm-cc/bluemonday v1.0.25
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
golang.org/x/mod v0.13.0
golang.org/x/sys v0.12.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ github.com/Joker/hpp v1.0.0 h1:65+iuJYdRXv/XyN62C1uEmmOx3432rNG/rKlX6V7Kkc=
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
github.com/Joker/jade v1.1.3 h1:Qbeh12Vq6BxURXT1qZBRHsDxeURB8ztcL6f3EXSGeHk=
github.com/Joker/jade v1.1.3/go.mod h1:T+2WLyt7VH6Lp0TRxQrUYEs64nRc83wkMQrfeIQKduM=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/RoaringBitmap/roaring v1.3.0 h1:aQmu9zQxDU0uhwR8SXOH/OrqEf+X8A0LQmwW3JX8Lcg=
github.com/RoaringBitmap/roaring v1.3.0/go.mod h1:plvDsJQpxOC5bw8LRteu/MLWHsHez/3y6cubLI4/1yE=
Expand Down Expand Up @@ -485,6 +487,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
10 changes: 5 additions & 5 deletions internal/x/calver/calver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ func TestCalVerLower(t *testing.T) {
v2 := "2023.02.01"
result, err := calver.Lower(v1, v2)
if err != nil {
t.Errorf("Errore inaspettato: %v", err)
t.Errorf("Unexpected error: %v", err)
}
if !result {
t.Errorf("La versione v1 dovrebbe essere inferiore a v2")
t.Errorf("The v1 should be lower than v2")
}

// Test with v1 and v2 versions where v1 is greater
v1 = "2023.03.01"
v2 = "2023.02.01"
result, err = calver.Lower(v1, v2)
if err != nil {
t.Errorf("Errore inaspettato: %v", err)
t.Errorf("Unexpected error: %v", err)
}
if result {
t.Errorf("La versione v1 non dovrebbe essere inferiore a v2")
t.Errorf("The v1 shouldn't be lower than v2")
}

// Test with v1 and v2 versions where v1 is invalid
v1 = "invalid"
v2 = "2023.02.01"
_, err = calver.Lower(v1, v2)
if err == nil {
t.Errorf("Dovrebbe esserci un errore per una versione non valida")
t.Errorf("Error expected")
}
}
20 changes: 19 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,26 @@ import (
cmd "github.com/unconditionalday/server/cmd"
)

var (
releaseVersion string
gitCommit string
)

func main() {
if err := cmd.NewRootCommand().Execute(); err != nil {
v := map[string]string{
"releaseVersion": "unknown",
"gitCommit": "unknown",
}

if releaseVersion != "" {
v["releaseVersion"] = releaseVersion
}

if gitCommit != "" {
v["gitCommit"] = gitCommit
}

if err := cmd.NewRootCommand(v).Execute(); err != nil {
logrus.Fatal(err)
}
}
14 changes: 14 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Install flyctl if isn't present
if ! command -v flyctl &> /dev/null; then
curl -L https://fly.io/install.sh | sh
export FLYCTL_INSTALL="/home/runner/.fly"
export PATH="$FLYCTL_INSTALL/bin:$PATH"
fi

# Deploy
flyctl deploy --build-secret UNCONDITIONAL_API_SOURCE_CLIENT_KEY="$UNCONDITIONAL_API_SOURCE_CLIENT_KEY" \
--build-arg UNCONDITIONAL_API_BUILD_COMMIT_VERSION="$UNCONDITIONAL_API_BUILD_COMMIT_VERSION" \
--build-arg UNCONDITIONAL_API_BUILD_RELEASE_VERSION="$UNCONDITIONAL_API_BUILD_RELEASE_VERSION" \
--strategy bluegreen

0 comments on commit 330cde8

Please sign in to comment.