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 image digest to Helm deploy #173

Merged
merged 3 commits into from
Jan 21, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ jobs:

- name: 'Run tests for ${{ matrix.command }} command'
run: './tests/${{ matrix.command }}/run_tests.sh'
env:
# Increases rate-limit of when downloading 3lv from GitHub in tests for '3lv upgrade'.
GITHUB_TOKEN: ${{ github.token }}
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.28.3
0.29.0
10 changes: 7 additions & 3 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import (
"github.com/urfave/cli/v3"
)

const commandName = "build"
const (
commandName = "build"
DefaultElviaContainerRegistry = "containerregistryelvia.azurecr.io"
DefaultCacheTag = "latest-cache"
)

func Command() *cli.Command {
return &cli.Command{
Expand Down Expand Up @@ -54,7 +58,7 @@ func Command() *cli.Command {
&cli.StringFlag{
Name: "cache-tag",
Usage: "The tag to use for the cache image.",
Value: "latest-cache",
Value: DefaultCacheTag,
Sources: cli.EnvVars("3LV_CACHE_TAG"),
},
&cli.StringFlag{
Expand Down Expand Up @@ -173,7 +177,7 @@ func Build(_ context.Context, c *cli.Command) error {
}

cacheTag := c.String("cache-tag")
registry := utils.StringWithDefault(c.String("registry"), "containerregistryelvia.azurecr.io")
registry := utils.StringWithDefault(c.String("registry"), DefaultElviaContainerRegistry)

push := c.Bool("push")
skipAuthentication := c.Bool("skip-authentication") || !push
Expand Down
10 changes: 4 additions & 6 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ func TestBuildCommand2(t *testing.T) {
dockerfilePath = "Dockerfile"
buildContext = "."
imageName = "ghcr.io/test-image"
cacheTag = "latest-cache"
)

imageNameWithCacheTag := imageName + ":" + cacheTag
imageNameWithCacheTag := imageName + ":" + DefaultCacheTag

expectedCommandString := strings.Join(
[]string{
Expand All @@ -152,7 +151,7 @@ func TestBuildCommand2(t *testing.T) {
dockerfilePath,
buildContext,
imageName,
cacheTag,
DefaultCacheTag,
[]string{},
&command.RunOptions{DryRun: true},
)
Expand All @@ -171,10 +170,9 @@ func TestBuildCommand3(t *testing.T) {
dockerfilePath = "Dockerfile"
buildContext = "."
imageName = "ghcr.io/test-image"
cacheTag = "latest-cache"
)

imageNameWithCacheTag := imageName + ":" + cacheTag
imageNameWithCacheTag := imageName + ":" + DefaultCacheTag
additionalTags := []string{"latest", "v42.0.1", "v420alpha"}

expectedCommandString := strings.Join(
Expand Down Expand Up @@ -206,7 +204,7 @@ func TestBuildCommand3(t *testing.T) {
dockerfilePath,
buildContext,
imageName,
cacheTag,
DefaultCacheTag,
additionalTags,
&command.RunOptions{DryRun: true},
)
Expand Down
41 changes: 41 additions & 0 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/3lvia/cli/pkg/auth"
"github.com/3lvia/cli/pkg/build"
"github.com/3lvia/cli/pkg/command"
"github.com/3lvia/cli/pkg/shared"
"github.com/3lvia/cli/pkg/style"
Expand Down Expand Up @@ -199,6 +200,7 @@ func Deploy(ctx context.Context, c *cli.Command) error {
return configForApplication.HelmValuesFile
}()
imageTag := c.String("image-tag")
imageDigest := getImageDigest(systemName, applicationName, imageTag)

commitHash, err := utils.ResolveCommitHash(c.String("commit-hash"))
if err != nil {
Expand Down Expand Up @@ -261,6 +263,7 @@ func Deploy(ctx context.Context, c *cli.Command) error {
environment,
workloadType,
imageTag,
imageDigest,
repositoryName,
commitHash,
dryRun,
Expand Down Expand Up @@ -428,3 +431,41 @@ func setupKubernetes(

return nil
}

func dockerInspectCommand(
imageName string,
runOptions *command.RunOptions,
) command.Output {
return command.Run(
*exec.Command(
"docker",
"inspect",
"--format",
"{{index .RepoDigests 0}}",
imageName+":"+build.DefaultCacheTag,
),
runOptions,
)
}

func getImageDigest(
systemName string,
applicationName string,
imageTag string,
) string {
imageName, err := build.GetImageName(
build.DefaultElviaContainerRegistry,
systemName,
applicationName,
)
if err != nil {
return ""
}

dockerInspectCommandOutput := dockerInspectCommand(imageName+":"+imageTag, nil)
if command.IsError(dockerInspectCommandOutput) {
return ""
}

return dockerInspectCommandOutput.Output
}
36 changes: 36 additions & 0 deletions pkg/deploy/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package deploy

import (
"strings"
"testing"

"github.com/3lvia/cli/pkg/command"
)

func TestDockerInspectCommand(t *testing.T) {
t.Parallel()

const imageName = "ghcr.io/3lvia/core/demo-api"

expectedCommandString := strings.Join(
[]string{
"docker",
"inspect",
"--format",
"{{index .RepoDigests 0}}",
imageName + ":latest-cache",
},
" ",
)

actualCommand := dockerInspectCommand(
imageName,
&command.RunOptions{DryRun: true},
)

command.ExpectedCommandStringEqualsActualCommand(
t,
expectedCommandString,
actualCommand,
)
}
11 changes: 9 additions & 2 deletions pkg/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func helmDeployCommand(
environment string,
workloadType string,
imageTag string,
imageDigest string,
repositoryName string,
commitHash string,
dryRun bool,
Expand Down Expand Up @@ -103,13 +104,19 @@ func helmDeployCommand(
"--set-string",
"environment="+environment,
"--set-string",
"image.tag="+imageTag,
"--set-string",
"labels.repositoryName="+repositoryName,
"--set-string",
"labels.commitHash=\""+commitHash+"\"",
)

if imageTag != "" {
cmd.Args = append(cmd.Args, "--set-string", "image.tag="+imageTag)
}

if imageDigest != "" {
cmd.Args = append(cmd.Args, "--set-string", "image.digest="+imageDigest)
}

if dryRun {
cmd.Args = append(cmd.Args, "--dry-run")
}
Expand Down
28 changes: 21 additions & 7 deletions pkg/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestHelmDeployCommand1(t *testing.T) {
environment = "dev"
workloadType = "deployment"
imageTag = "v12"
imageDigest = "sha256:1234567890"
repositoryName = "core"
commitHash = "123456"
)
Expand All @@ -120,11 +121,13 @@ func TestHelmDeployCommand1(t *testing.T) {
"--set-string",
"environment=" + environment,
"--set-string",
"image.tag=" + imageTag,
"--set-string",
"labels.repositoryName=" + repositoryName,
"--set-string",
"labels.commitHash=\"" + commitHash + "\"",
"--set-string",
"image.tag=" + imageTag,
"--set-string",
"image.digest=" + imageDigest,
},
" ",
)
Expand All @@ -136,6 +139,7 @@ func TestHelmDeployCommand1(t *testing.T) {
environment,
workloadType,
imageTag,
imageDigest,
repositoryName,
commitHash,
false,
Expand All @@ -160,6 +164,7 @@ func TestHelmDeployCommand2(t *testing.T) {
environment = "prod"
workloadType = "statefulset"
imageTag = "v420"
imageDigest = "sha256:abcdef"
repositoryName = "core-not-monorepo"
commitHash = "abcdef"
)
Expand All @@ -179,11 +184,13 @@ func TestHelmDeployCommand2(t *testing.T) {
"--set-string",
"environment=" + environment,
"--set-string",
"image.tag=" + imageTag,
"--set-string",
"labels.repositoryName=" + repositoryName,
"--set-string",
"labels.commitHash=\"" + commitHash + "\"",
"--set-string",
"image.tag=" + imageTag,
"--set-string",
"image.digest=" + imageDigest,
},
" ",
)
Expand All @@ -195,6 +202,7 @@ func TestHelmDeployCommand2(t *testing.T) {
environment,
workloadType,
imageTag,
imageDigest,
repositoryName,
commitHash,
false,
Expand All @@ -219,6 +227,7 @@ func TestHelmDeployCommand3(t *testing.T) {
environment = "prod"
workloadType = "job"
imageTag = "v420"
imageDigest = "sha256:abcdef"
repositoryName = "core-not-monorepo"
commitHash = "abcdef"
)
Expand All @@ -230,6 +239,7 @@ func TestHelmDeployCommand3(t *testing.T) {
environment,
workloadType,
imageTag,
imageDigest,
repositoryName,
commitHash,
false,
Expand All @@ -251,7 +261,8 @@ func TestHelmDeployCommand4(t *testing.T) {
applicationName = "demo-api"
environment = "prod"
workloadType = "deployment"
imageTag = "v420"
imageTag = ""
imageDigest = "sha256:abcdef"
repositoryName = "iss-demo-api"
commitHash = "abcdef"
)
Expand All @@ -271,11 +282,11 @@ func TestHelmDeployCommand4(t *testing.T) {
"--set-string",
"environment=" + environment,
"--set-string",
"image.tag=" + imageTag,
"--set-string",
"labels.repositoryName=" + repositoryName,
"--set-string",
"labels.commitHash=\"" + commitHash + "\"",
"--set-string",
"image.digest=" + imageDigest,
},
" ",
)
Expand All @@ -287,6 +298,7 @@ func TestHelmDeployCommand4(t *testing.T) {
environment,
workloadType,
imageTag,
imageDigest,
repositoryName,
commitHash,
false,
Expand All @@ -311,6 +323,7 @@ func TestHelmDeployCommand5(t *testing.T) {
environment = "prod"
workloadType = "statefulset"
imageTag = "v420"
imageDigest = "sha256:abcdef"
repositoryName = "iss-demo-api"
commitHash = "abcdef"
)
Expand All @@ -322,6 +335,7 @@ func TestHelmDeployCommand5(t *testing.T) {
environment,
workloadType,
imageTag,
imageDigest,
repositoryName,
commitHash,
false,
Expand Down
2 changes: 1 addition & 1 deletion pkg/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func generateComposeFile(
composeTemplates,
ComposeFileVariables{
ApplicationName: applicationName,
ImageName: imageName + ":latest-cache",
ImageName: imageName + build.DefaultCacheTag,
Port: helmValues.Service.Port,
TargetPort: helmValues.Service.TargetPort,
EnvironmentVariables: helmValues.GetEnvironmentVariablesMap(),
Expand Down
12 changes: 6 additions & 6 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/3lvia/cli/pkg/command"
"github.com/3lvia/cli/pkg/style"
"github.com/google/go-github/v67/github"
"github.com/3lvia/cli/pkg/utils"
"github.com/urfave/cli/v3"
"golang.org/x/mod/semver"
)
Expand Down Expand Up @@ -163,6 +163,8 @@ func Upgrade(ctx context.Context, c *cli.Command, version string) error {
}

func getLatestBinaryURL(ctx context.Context) (string, error) {
githubClient := utils.GetAuthenticatedGithubClient()

if runtime.GOOS == "windows" {
return "",
errors.New(
Expand All @@ -178,9 +180,7 @@ func getLatestBinaryURL(ctx context.Context) (string, error) {
return "", fmt.Errorf("Auto-upgrade is not supported on %s", runtime.GOARCH)
}

client := github.NewClient(nil)

release, _, err := client.Repositories.GetLatestRelease(ctx, "3lvia", "cli")
release, _, err := githubClient.Repositories.GetLatestRelease(ctx, "3lvia", "cli")
if err != nil {
return "", err
}
Expand All @@ -204,9 +204,9 @@ func getLatestBinaryURL(ctx context.Context) (string, error) {
}

func GetLatestCLIVersion(ctx context.Context) (string, error) {
client := github.NewClient(nil)
githubClient := utils.GetAuthenticatedGithubClient()

release, _, err := client.Repositories.GetLatestRelease(ctx, "3lvia", "cli")
release, _, err := githubClient.Repositories.GetLatestRelease(ctx, "3lvia", "cli")
if err != nil {
return "", err
}
Expand Down
Loading