Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version in binary and docker #1030

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ jobs:
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Set version
shell: bash
run: |
echo "VERSION=$(echo ${GITHUB_REF#refs/tags/v})" >> $GITHUB_ENV

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
Expand All @@ -73,7 +78,9 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: SENTRY_DSN_DOCKER=${{secrets.SENTRY_DSN_DOCKER}}
build-args: |
SENTRY_DSN_DOCKER=${{secrets.SENTRY_DSN_DOCKER}}
VERSION=${{ env.VERSION }}

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
Expand Down
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ FROM golang:1.21 AS build
# Set the working directory
WORKDIR /app

# Define build arguments for ldflags
ARG SENTRY_DSN_DOCKER
ARG VERSION

# Copy the Go module files and download dependencies
COPY go.mod go.sum /app/
RUN go mod download
Expand All @@ -12,14 +16,12 @@ RUN go mod download
COPY . /app

# Build the keploy binary
RUN go build -o keploy .
RUN go build -ldflags="-X main.Dsn=$SENTRY_DSN_DOCKER -X main.version=$VERSION" -o keploy .

# === Runtime Stage ===
FROM debian:bookworm-slim

ARG SENTRY_DSN_DOCKER
ENV IS_DOCKER_CMD=true
ENV Dsn=$SENTRY_DSN_DOCKER
ENV IS_DOCKER_CMD=true

# Update the package lists and install required packages
RUN apt-get update && \
Expand Down
1 change: 1 addition & 0 deletions goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ builds:
main: ./main.go
ldflags:
- -s -w -X main.Dsn={{.Env.SENTRY_DSN_BINARY}}
- -s -w -X main.version={{.Version}}
env:
- CGO_ENABLED=0
goos:
Expand Down
52 changes: 5 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ package main
import (
"fmt"
_ "net/http/pprof"
"os"
"time"

sentry "github.com/getsentry/sentry-go"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/cloudflare/cfssl/log"
v "github.com/hashicorp/go-version"
sentry "github.com/getsentry/sentry-go"
"go.keploy.io/server/cmd"
"go.keploy.io/server/utils"
)
Expand All @@ -19,7 +15,7 @@ import (
// see https://goreleaser.com/customization/build/

var version string
var Dsn string
var dsn string

const logo string = `
▓██▓▄
Expand All @@ -33,53 +29,15 @@ const logo string = `
`

func getKeployVersion() string {

repo, err := git.PlainOpen(".")
if err != nil {
return "v0.1.0-dev"
}

tagIter, err := repo.Tags()
if err != nil {
return "v0.1.0-dev"
}
var latestTag string
var latestTagVersion *v.Version

err = tagIter.ForEach(func(tagRef *plumbing.Reference) error {
tagName := tagRef.Name().Short()
tagVersion, err := v.NewVersion(tagName)
if err == nil {
if latestTagVersion == nil || latestTagVersion.LessThan(tagVersion) {
latestTagVersion = tagVersion
latestTag = tagName
}
}
return nil
})

if err != nil {
return "v0.1.0-dev"
}

return latestTag + "-dev"
}


func main() {
if version == "" {
version = getKeployVersion()
version = "2-dev"
}
fmt.Println(logo, " ")
fmt.Printf("%v\n\n", version)
isDocker := os.Getenv("IS_DOCKER_CMD")
if isDocker != "" {
Dsn = os.Getenv("Dsn")
}
fmt.Printf("version: %v\n\n", version)
//Initialise sentry.
err := sentry.Init(sentry.ClientOptions{
Dsn: Dsn,
Dsn: dsn,
TracesSampleRate: 1.0,
})
log.Level = 0
Expand Down