Skip to content

Commit

Permalink
Merge pull request #26 from puzzle/secret
Browse files Browse the repository at this point in the history
change registry login to secret, fixes #21
  • Loading branch information
chrira committed Jul 17, 2024
2 parents ccc3b0e + 569d603 commit 03b5661
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:

env:
# The Dagger CLI uses the DAGGER_CLOUD_TOKEN environment variable to authenticate with Dagger Cloud
DAGGER_CLOUD_TOKEN: ${{ secrets.DAGGER_CLOUD_TOKEN}}
DAGGER_CLOUD_TOKEN: ${{ secrets.DAGGER_CLOUD_TOKEN }}

jobs:
build:
Expand All @@ -19,3 +19,16 @@ jobs:
run: dagger functions -m helm/
- name: Run module tests
run: dagger -m examples/ call all
- name: Run module integration tests
env:
TEST_HELM_REGISTRY_URL: ${{ secrets.TEST_HELM_REGISTRY_URL }}
TEST_HELM_REGISTRY_REPOSITORY: ${{ secrets.TEST_HELM_REGISTRY_REPOSITORY }}
TEST_HELM_REGISTRY_HELM_USER: ${{ secrets.TEST_HELM_REGISTRY_HELM_USER }}
TEST_HELM_REGISTRY_HELM_PASSWORD: ${{ secrets.TEST_HELM_REGISTRY_HELM_PASSWORD }}
run: |
dagger -m examples/ \
call package-push \
--registry ${TEST_HELM_REGISTRY_URL} \
--repository ${TEST_HELM_REGISTRY_REPOSITORY} \
--username ${TEST_HELM_REGISTRY_HELM_USER} \
--password env:TEST_HELM_REGISTRY_HELM_PASSWORD
4 changes: 2 additions & 2 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

type Ci struct{}

// Build and publish image from Dockerfile using a build context directory
// in a different location than the current working directory
// Build helm image used in the helm module.
func (m *Ci) Build(
ctx context.Context,
) (*dagger.Container) {
Expand All @@ -38,6 +37,7 @@ func (m *Ci) Build(
})
}

// Build and publish helm image used in the helm module.
func (m *Ci) Publish(
ctx context.Context,
// URL of the registry
Expand Down
29 changes: 29 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ func (m *Examples) Version(
return nil
}

func (h *Examples) PackagePush(
// method call context
ctx context.Context,
// URL of the registry
registry string,
// name of the repository
repository string,
// registry login username
username string,
// registry login password
password *Secret,
) error {
// dagger call package-push \
// --registry registry.puzzle.ch \
// --repository helm \
// --username $REGISTRY_HELM_USER \
// --password env:REGISTRY_HELM_PASSWORD \
// --directory ./examples/testdata/mychart/

// directory that contains the Helm Chart
directory := dag.CurrentModule().Source().Directory("testdata/mychart/")
_, err := dag.Helm().PackagePush(ctx, directory, registry, repository, username, password)

if err != nil {
return err
}

return nil
}

func (m *Examples) Test(
// method call context
Expand Down
2 changes: 1 addition & 1 deletion examples/testdata/mychart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
name: mychart
name: dagger-module-helm-test
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
Expand Down
2 changes: 1 addition & 1 deletion examples/testdata/mychart/tests/deployment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tests:
of: Deployment
- matchRegex:
path: metadata.name
pattern: -mychart
pattern: -dagger-module-helm-test
- equal:
path: spec.template.spec.containers[0].image
value: nginx:latest
18 changes: 12 additions & 6 deletions helm/dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"context"
"dagger/helm/internal/dagger"
"fmt"
"os"
"strings"
Expand All @@ -23,7 +24,7 @@ type PushOpts struct {
Repository string `yaml:"repository"`
Oci bool `yaml:"oci"`
Username string `yaml:"username"`
Password string
Password *dagger.Secret
}

func (p PushOpts) getChartFqdn(name string) string {
Expand Down Expand Up @@ -71,9 +72,9 @@ func (h *Helm) Version(
// dagger call package-push \
// --registry registry.puzzle.ch \
// --repository helm \
// --username REGISTRY_HELM_USER \
// --password REGISTRY_HELM_PASSWORD \
// --directory ./mychart/
// --username $REGISTRY_HELM_USER \
// --password env:REGISTRY_HELM_PASSWORD \
// --directory ./examples/testdata/mychart/
func (h *Helm) PackagePush(
// method call context
ctx context.Context,
Expand All @@ -86,7 +87,7 @@ func (h *Helm) PackagePush(
// registry login username
username string,
// registry login password
password string,
password *dagger.Secret,
) (bool, error) {
opts := PushOpts{
Registry: registry,
Expand Down Expand Up @@ -115,7 +116,12 @@ func (h *Helm) PackagePush(

name = strings.TrimSpace(name)

c, err = c.WithExec([]string{"helm", "registry", "login", opts.Registry, "-u", opts.Username, "-p", opts.Password}).Sync(ctx)
c, err = c.
WithEnvVariable("REGISTRY_URL", opts.Registry).
WithEnvVariable("REGISTRY_USERNAME", opts.Username).
WithSecretVariable("REGISTRY_PASSWORD", opts.Password).
WithExec([]string{"sh", "-c", `echo ${REGISTRY_PASSWORD} | helm registry login ${REGISTRY_URL} --username ${REGISTRY_USERNAME} --password-stdin`}).
Sync(ctx)
if err != nil {
return false, err
}
Expand Down

0 comments on commit 03b5661

Please sign in to comment.