Skip to content

Commit

Permalink
Support Helm deployedBy label (#179)
Browse files Browse the repository at this point in the history
* Support Helm deployedBy label

* 0.29.3
  • Loading branch information
baksetercx authored Jan 24, 2025
1 parent 99597d2 commit 4739832
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.29.2
0.29.3
5 changes: 5 additions & 0 deletions pkg/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deploy

import (
"fmt"
"os"
"os/exec"

"github.com/3lvia/cli/pkg/command"
Expand Down Expand Up @@ -117,6 +118,10 @@ func helmDeployCommand(
cmd.Args = append(cmd.Args, "--set-string", "image.digest="+imageDigest)
}

if os.Getenv("GITHUB_ACTIONS") == "true" {
cmd.Args = append(cmd.Args, "--set-string", "labels.deployedBy=github-actions")
}

if dryRun {
cmd.Args = append(cmd.Args, "--dry-run")
}
Expand Down
75 changes: 70 additions & 5 deletions pkg/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestHelmRepoUpdateCommand(t *testing.T) {
}

func TestHelmDeployCommand1(t *testing.T) {
t.Parallel()
t.Setenv("GITHUB_ACTIONS", "false") // Reset GITHUB_ACTIONS env var so tests don't fail in GitHub Actions

const (
systemName = "core"
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestHelmDeployCommand1(t *testing.T) {
}

func TestHelmDeployCommand2(t *testing.T) {
t.Parallel()
t.Setenv("GITHUB_ACTIONS", "false") // Reset GITHUB_ACTIONS env var so tests don't fail in GitHub Actions

const (
systemName = "core"
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestHelmDeployCommand2(t *testing.T) {
}

func TestHelmDeployCommand3(t *testing.T) {
t.Parallel()
t.Setenv("GITHUB_ACTIONS", "false") // Reset GITHUB_ACTIONS env var so tests don't fail in GitHub Actions

const (
systemName = "core"
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestHelmDeployCommand3(t *testing.T) {
}

func TestHelmDeployCommand4(t *testing.T) {
t.Parallel()
t.Setenv("GITHUB_ACTIONS", "false") // Reset GITHUB_ACTIONS env var so tests don't fail in GitHub Actions

const (
systemName = "core"
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestHelmDeployCommand4(t *testing.T) {
}

func TestHelmDeployCommand5(t *testing.T) {
t.Parallel()
t.Setenv("GITHUB_ACTIONS", "false") // Reset GITHUB_ACTIONS env var so tests don't fail in GitHub Actions

const (
systemName = "core"
Expand Down Expand Up @@ -347,3 +347,68 @@ func TestHelmDeployCommand5(t *testing.T) {
t.Errorf("Expected error, got %s", commandOutput)
}
}

func TestHelmDeployCommandWithGitHubActionsEnv(t *testing.T) {
const (
systemName = "core"
helmValuesFile = ".github/deploy/values.yml"
applicationName = "demo-api"
environment = "dev"
workloadType = "deployment"
imageTag = "v12"
imageDigest = "sha256:1234567890"
repositoryName = "core"
commitHash = "123456"
)

expectedCommandString := strings.Join(
[]string{
"helm",
"upgrade",
"--debug",
"--install",
"-n",
systemName,
"-f",
helmValuesFile,
applicationName,
"elvia-charts/elvia-" + workloadType,
"--set-string",
"environment=" + environment,
"--set-string",
"labels.repositoryName=" + repositoryName,
"--set-string",
"labels.commitHash=\"" + commitHash + "\"",
"--set-string",
"image.tag=" + imageTag,
"--set-string",
"image.digest=" + imageDigest,
"--set-string",
"labels.deployedBy=github-actions",
},
" ",
)

t.Setenv("GITHUB_ACTIONS", "true")

actualCommand := helmDeployCommand(
applicationName,
systemName,
helmValuesFile,
environment,
workloadType,
imageTag,
imageDigest,
repositoryName,
commitHash,
false,
false,
&command.RunOptions{DryRun: true},
)

command.ExpectedCommandStringEqualsActualCommand(
t,
expectedCommandString,
actualCommand,
)
}

0 comments on commit 4739832

Please sign in to comment.