From 330cde8074446649d8c063e62477d56a60d690a6 Mon Sep 17 00:00:00 2001 From: Luigi Date: Sat, 7 Oct 2023 21:42:42 +0200 Subject: [PATCH] chore: add versioning release, use new fly deploy strategy --- .env.dist | 2 ++ .envrc.dist | 3 ++- .github/workflows/deploy.yaml | 18 ----------------- .github/workflows/release.yaml | 34 ++++++++++++++++++++++++++++++++ Dockerfile | 8 +++++++- cmd/root.go | 31 +++-------------------------- go.mod | 2 ++ go.sum | 4 ++++ internal/x/calver/calver_test.go | 10 +++++----- main.go | 20 ++++++++++++++++++- scripts/deploy.sh | 14 +++++++++++++ 11 files changed, 92 insertions(+), 54 deletions(-) delete mode 100644 .github/workflows/deploy.yaml create mode 100644 .github/workflows/release.yaml create mode 100644 scripts/deploy.sh diff --git a/.env.dist b/.env.dist index f32009b..dcd9358 100644 --- a/.env.dist +++ b/.env.dist @@ -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= diff --git a/.envrc.dist b/.envrc.dist index 3a8e379..dcd9358 100644 --- a/.envrc.dist +++ b/.envrc.dist @@ -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= diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml deleted file mode 100644 index 38db813..0000000 --- a/.github/workflows/deploy.yaml +++ /dev/null @@ -1,18 +0,0 @@ -name: Deploy -on: - push: - tags: - - 'v*' - -env: - UNCONDITIONAL_API_SOURCE_CLIENT_KEY: ${{ secrets.UNCONDITIONAL_API_SOURCE_CLIENT_KEY }} - FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} - -jobs: - deploy: - name: Deploy app - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: superfly/flyctl-actions/setup-flyctl@master - - run: fly deploy --build-secret UNCONDITIONAL_API_SOURCE_CLIENT_KEY=${UNCONDITIONAL_API_SOURCE_CLIENT_KEY} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..32e8f8c --- /dev/null +++ b/.github/workflows/release.yaml @@ -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 diff --git a/Dockerfile b/Dockerfile index 45f0ac7..cfeab63 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/cmd/root.go b/cmd/root.go index c1b8a3b..0c0860a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,18 +3,12 @@ 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{ @@ -22,27 +16,8 @@ func NewRootCommand() *cobra.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()) diff --git a/go.mod b/go.mod index 683cb6f..11200fd 100644 --- a/go.mod +++ b/go.mod @@ -118,6 +118,7 @@ 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 @@ -125,5 +126,6 @@ require ( 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 ) diff --git a/go.sum b/go.sum index 702b820..e7bc350 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/internal/x/calver/calver_test.go b/internal/x/calver/calver_test.go index 99d5b20..ada3345 100644 --- a/internal/x/calver/calver_test.go +++ b/internal/x/calver/calver_test.go @@ -14,10 +14,10 @@ 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 @@ -25,10 +25,10 @@ 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 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 @@ -36,6 +36,6 @@ func TestCalVerLower(t *testing.T) { 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") } } diff --git a/main.go b/main.go index b8deaac..62ca38c 100644 --- a/main.go +++ b/main.go @@ -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) } } diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..296597f --- /dev/null +++ b/scripts/deploy.sh @@ -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