Skip to content

Commit

Permalink
Cleanup from first release with dagger
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Oct 18, 2024
1 parent 24ddfa7 commit 31289d1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 148 deletions.
53 changes: 0 additions & 53 deletions .goreleaser-nodocker.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ brews:
- homepage: https://docs.replicated.com/reference/replicated-cli-installing
description: "Package Replicated applications and manage releases, channels, customers and entitlements using a command-line interface."
repository:
name: replicatedhq/replicated
name: homebrew-replicated
owner: replicatedhq
branch: main
install: bin.install "replicated"
Expand Down
35 changes: 6 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
API_PKGS=apps channels releases

VERSION=$(shell git describe)
ABBREV_VERSION=$(shell git describe --abbrev=0)
VERSION_PACKAGE = github.com/replicatedhq/replicated/pkg/version
DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
BUILDTAGS = containers_image_ostree_stub exclude_graphdriver_devicemapper exclude_graphdriver_btrfs containers_image_openpgp

export GO111MODULE=on

GIT_TREE = $(shell git rev-parse --is-inside-work-tree 2>/dev/null)
ifneq "$(GIT_TREE)" ""
define GIT_UPDATE_INDEX_CMD
git update-index --assume-unchanged
endef
define GIT_SHA
`git rev-parse HEAD`
endef
else
define GIT_UPDATE_INDEX_CMD
echo "Not a git repo, skipping git update-index"
endef
define GIT_SHA
""
endef
endif

define LDFLAGS
-ldflags "\
-X ${VERSION_PACKAGE}.version=${VERSION} \
-X ${VERSION_PACKAGE}.gitSHA=${GIT_SHA} \
-X ${VERSION_PACKAGE}.buildTime=${DATE} \
"
endef
export CGO_ENABLED=0

.PHONY: test-unit
test-unit:
Expand Down Expand Up @@ -138,4 +111,8 @@ docs:

.PHONE: release
release:
dagger call release --one-password-service-account env:OP_SERVICE_ACCOUNT_PRODUCTION --version $(version)
dagger call release \
--one-password-service-account-production env:OP_SERVICE_ACCOUNT_PRODUCTION \
--version $(version) \
--github-token env:GITHUB_TOKEN \
--progress plain
71 changes: 57 additions & 14 deletions dagger/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/Masterminds/semver"
)
Expand All @@ -27,32 +28,68 @@ func (r *Replicated) Release(
clean bool,

onePasswordServiceAccountProduction *dagger.Secret,

githubToken *dagger.Secret,
) error {
gitTreeOK, err := checkGitTree(ctx, source)
if err != nil {
return err
}
if !gitTreeOK {
return fmt.Errorf("git tree is not clean")
return fmt.Errorf("Your git tree is not clean. You cannot release what's not commited.")
}

major, minor, patch, err := parseVersion(ctx, version)
latestVersion, err := getLatestVersion(ctx)
if err != nil {
return err
}

_ = dag.Container().
major, minor, patch, err := parseVersion(ctx, latestVersion, version)
if err != nil {
return err
}

fmt.Printf("Releasing as version %d.%d.%d\n", major, minor, patch)

githubTokenPlaintext, err := githubToken.Plaintext(ctx)
if err != nil {
return err
}
tagContainer := dag.Container().
From("alpine/git:latest").
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source).
WithWorkdir("/go/src/github.com/replicatedhq/replicated").
WithExec([]string{"git", "tag", fmt.Sprintf("v%d.%d.%d", major, minor, patch)}).
WithExec([]string{"git", "push", "origin", fmt.Sprintf("v%d.%d.%d", major, minor, patch)})
WithExec([]string{"git", "remote", "add", "tag", fmt.Sprintf("https://%[email protected]/replicatedhq/replicated.git", githubTokenPlaintext)}).
With(CacheBustingExec([]string{"git", "tag", fmt.Sprintf("v%d.%d.%d", major, minor, patch)})).
With(CacheBustingExec([]string{"git", "push", "tag", fmt.Sprintf("v%d.%d.%d", major, minor, patch)}))
_, err = tagContainer.Stdout(ctx)
if err != nil {
return err
}

// copy the source that has the tag included in it
updatedSource := tagContainer.Directory("/go/src/github.com/replicatedhq/replicated")

// replace the version in the Makefile
buildFileContent, err := updatedSource.File("./pkg/version/build.go").Contents(ctx)
if err != nil {
return err
}
buildFileContent = strings.ReplaceAll(buildFileContent, "const version = \"unknown\"", fmt.Sprintf("const version = \"%s\"", version))
updatedSource = updatedSource.WithNewFile("./pkg/version/build.go", buildFileContent)

goModCache := dag.CacheVolume("replicated-go-mod-122")
goBuildCache := dag.CacheVolume("replicated-go-build-121")

replicatedBinary := dag.Container().
From("golang:1.22").
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source).
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", updatedSource).
WithWorkdir("/go/src/github.com/replicatedhq/replicated").
WithExec([]string{"make", "build"}).
WithMountedCache("/go/pkg/mod", goModCache).
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", goBuildCache).
WithEnvVariable("GOCACHE", "/go/build-cache").
With(CacheBustingExec([]string{"make", "build"})).
File("/go/src/github.com/replicatedhq/replicated/bin/replicated")

dockerContainer := dag.Container().
Expand All @@ -65,6 +102,10 @@ func (r *Replicated) Release(
WithWorkdir("/out").
WithEntrypoint([]string{"/replicated"}).
WithFile("/replicated", replicatedBinary)
_, err = dockerContainer.Stdout(ctx)
if err != nil {
return err
}

username, err := dag.Onepassword().FindSecret(
onePasswordServiceAccountProduction,
Expand Down Expand Up @@ -96,12 +137,17 @@ func (r *Replicated) Release(
panic(err)
}

goreleaserContainer := dag.Goreleaser(dagger.GoreleaserOpts{
Version: goreleaserVersion,
}).Ctr().WithSecretVariable("GITHUB_TOKEN", githubToken)

if snapshot {
_, err := dag.
Goreleaser(dagger.GoreleaserOpts{
Version: goreleaserVersion,
Ctr: goreleaserContainer,
}).
WithSource(source).
WithSource(updatedSource).
Snapshot(ctx, dagger.GoreleaserSnapshotOpts{
Clean: clean,
})
Expand All @@ -112,8 +158,9 @@ func (r *Replicated) Release(
_, err := dag.
Goreleaser(dagger.GoreleaserOpts{
Version: goreleaserVersion,
Ctr: goreleaserContainer,
}).
WithSource(source).
WithSource(updatedSource).
Release(ctx, dagger.GoreleaserReleaseOpts{
Clean: clean,
})
Expand All @@ -125,11 +172,7 @@ func (r *Replicated) Release(
return nil
}

func parseVersion(ctx context.Context, version string) (int64, int64, int64, error) {
latestVersion, err := getLatestVersion(ctx)
if err != nil {
return 0, 0, 0, err
}
func parseVersion(ctx context.Context, latestVersion string, version string) (int64, int64, int64, error) {
parsedLatestVersion, err := semver.NewVersion(latestVersion)
if err != nil {
return 0, 0, 0, err
Expand Down
6 changes: 1 addition & 5 deletions pkg/version/build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
package version

// NOTE: these variables are injected at build time

var (
version, gitSHA, buildTime string
)
const version = "unknown"
49 changes: 3 additions & 46 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package version
import (
"fmt"
"os"
"runtime"
"time"

"github.com/pkg/errors"
)
Expand All @@ -15,35 +13,15 @@ var (

// Build holds details about this build of the replicated cli binary
type Build struct {
Version string `json:"version,omitempty"`
GitSHA string `json:"git,omitempty"`
BuildTime time.Time `json:"buildTime,omitempty"`
TimeFallback string `json:"buildTimeFallback,omitempty"`
GoInfo GoInfo `json:"go,omitempty"`
UpdateInfo *UpdateInfo `json:"updateInfo,omitempty"`
}

type GoInfo struct {
Version string `json:"version,omitempty"`
Compiler string `json:"compiler,omitempty"`
OS string `json:"os,omitempty"`
Arch string `json:"arch,omitempty"`
Version string `json:"version,omitempty"`
UpdateInfo *UpdateInfo `json:"updateInfo,omitempty"`
}

// initBuild sets up the version info from build args
func initBuild() {
build.Version = version
if len(gitSHA) >= 7 {
build.GitSHA = gitSHA[:7]
}
var err error
build.BuildTime, err = time.Parse(time.RFC3339, buildTime)
if err != nil {
build.TimeFallback = buildTime
}

build.GoInfo = getGoInfo()

var err error
updateChecker, err := NewUpdateChecker(build.Version, "replicatedhq/replicated/cli")
if err != nil {
return
Expand All @@ -61,35 +39,14 @@ func initBuild() {
}
}

// GetBuild gets the build
func GetBuild() Build {
return build
}

// Version gets the version
func Version() string {
return build.Version
}

// GitSHA gets the gitsha
func GitSHA() string {
return build.GitSHA
}

// BuildTime gets the build time
func BuildTime() time.Time {
return build.BuildTime
}

func getGoInfo() GoInfo {
return GoInfo{
Version: runtime.Version(),
Compiler: runtime.Compiler,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
}
}

func Print() {
fmt.Printf("replicated version %s\n", build.Version)

Expand Down

0 comments on commit 31289d1

Please sign in to comment.