From b5e355b8125a49ecdcf047c2eb7f13233868b828 Mon Sep 17 00:00:00 2001 From: Senjuti De Date: Mon, 19 Feb 2024 19:51:29 +0530 Subject: [PATCH] Source annotation getting added as first line --- internal/catalog/catalog.go | 113 ++++++- internal/catalog/catalog_test.go | 4 +- .../tasks/go-crane-image/go-crane-image.yaml | 1 + .../tasks/go-ko-image/go-ko-image.yaml | 1 + .../go-crane-image/go-crane-image.yaml | 248 ++++++++------- .../cmd/testdata/go-ko-image/go-ko-image.yaml | 287 +++++++++--------- 6 files changed, 370 insertions(+), 284 deletions(-) diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index fb0824a8..bff8e62b 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -2,6 +2,7 @@ package catalog import ( "archive/tar" + "bufio" "compress/gzip" "crypto/sha256" "encoding/hex" @@ -11,6 +12,7 @@ import ( "net/http" "os" "path/filepath" + "regexp" "strings" "github.com/cli/go-gh/v2/pkg/api" @@ -92,11 +94,10 @@ func fetchAndExtract(path string, release Release, version, resourceType string) } // Let's get the file we want to fetch from the release object tektonResources := getResourcesFromType(release, resourceType) - return untar(path, version, tektonResources, resp.Body) + return untar(path, version, tektonResources, release.ResourcesURI, resp.Body) // Pass release.ResourcesURI to untar } -// untar, filter and validate content. -func untar(dst, version string, tektonResources map[string]contract.TektonResource, r io.Reader) error { +func untar(dst, version string, tektonResources map[string]contract.TektonResource, resourcesURI string, r io.Reader) error { gzr, err := gzip.NewReader(r) if err != nil { return err @@ -166,16 +167,101 @@ func untar(dst, version string, tektonResources map[string]contract.TektonResour // to wait until all operations have completed. f.Close() - if filename != "README.md" { - if tektonResource.Checksum != sum { - fmt.Fprintf(os.Stderr, "%s checksum is different than the specified checksum in the catalog file: %s", sum, tektonResource.Checksum) - // FIXME: maybe handle *all* file before erroring out ? - return fmt.Errorf("invalid checksum for %s: %s != %s", filename, sum, tektonResource.Checksum) - } - fmt.Fprintf(os.Stderr, "✅ %s\n", tektonResource.Filename) + if filename != "README.md" { + if tektonResource.Checksum != sum { + fmt.Fprintf(os.Stderr, "%s checksum is different than the specified checksum in the catalog file: %s", sum, tektonResource.Checksum) + // FIXME: maybe handle *all* file before erroring out ? + return fmt.Errorf("invalid checksum for %s: %s != %s", filename, sum, tektonResource.Checksum) + } + fmt.Fprintf(os.Stderr, "✅ %s\n", tektonResource.Filename) + } + + // Add "source" annotation to task YAML file + if strings.HasSuffix(target, ".yaml") { + if err := addSourceAnnotationToTask(target, resourcesURI); err != nil { + return err + } + } } - } } + +} + +func addSourceAnnotationToTask(file, resourcesURI string) error { + // Add the new annotation + repoURL := extractRepositoryURL(resourcesURI) + + // Open the Task YAML file + f, err := os.OpenFile(file, os.O_RDWR, 0644) + if err != nil { + return err + } + defer f.Close() + + // Create a scanner to read the file line by line + scanner := bufio.NewScanner(f) + var updatedContent []string + + // Regular expression pattern to match the annotations in Task metadata + annotationsPattern := regexp.MustCompile(`^\s+annotations:\s*$`) + sourceAnnotationPattern := regexp.MustCompile(`^\s+tekton\.dev/source:\s*".*"$`) + + // Flag to indicate if the "tekton.dev/source" annotation is already present + var sourceAnnotationExists bool + + // Read the file line by line + for scanner.Scan() { + line := scanner.Text() + + // Check if the line matches the annotations pattern + if annotationsPattern.MatchString(line) { + // If annotations block is found, initialize sourceAnnotationExists to false + sourceAnnotationExists = false + }else if !sourceAnnotationExists && sourceAnnotationPattern.MatchString(line) { + // If source annotation is found within annotations block, set sourceAnnotationExists to true + sourceAnnotationExists = true + } + + + + // Append the line to updatedContent slice + updatedContent = append(updatedContent, line) + + // Check if we are still within the annotations block + if !sourceAnnotationExists && annotationsPattern.MatchString(line) { + // Add the source annotation as the first line of the annotations block + updatedContent = append(updatedContent, fmt.Sprintf(" tekton.dev/source: \"%s\"", repoURL)) + sourceAnnotationExists = true // Set sourceAnnotationExists to true after adding the annotation + } + } + + // Check for scanner errors + if err := scanner.Err(); err != nil { + return err + } + + // Clear the file content and write the updated content + if err := f.Truncate(0); err != nil { + return err + } + if _, err := f.Seek(0, 0); err != nil { + return err + } + writer := bufio.NewWriter(f) + for _, line := range updatedContent { + fmt.Fprintln(writer, line) + } + return writer.Flush() +} + +// Function to extract repository URL from resource tarball URL +func extractRepositoryURL(url string) string { + // Assuming the resource tarball URL format is consistent: https://github.com/{organization}/{repository}/releases/download/{version}/resources.tar.gz + parts := strings.Split(url, "/") + if len(parts) < 5 { + return "" + } + return strings.Join(parts[:5], "/") } func getResourcesFromType(release Release, resourceType string) map[string]contract.TektonResource { @@ -199,3 +285,8 @@ func getResourcesFromType(release Release, resourceType string) map[string]contr } return m } + + + + + diff --git a/internal/catalog/catalog_test.go b/internal/catalog/catalog_test.go index 87c2502b..4e1be4ee 100644 --- a/internal/catalog/catalog_test.go +++ b/internal/catalog/catalog_test.go @@ -72,7 +72,7 @@ func TestGenerateFilesystem(t *testing.T) { Repositories: map[string]catalog.Repository{ "sbr-golang": map[string]catalog.Release{ "0.5.0": { - ResourcesURI: "https://fake.host/resources.tar.gz", + ResourcesURI: "https://fake.host/repo/resources.tar.gz", Catalog: contract.Catalog{ Resources: &contract.Resources{ Tasks: []*contract.TektonResource{{ @@ -113,4 +113,4 @@ func TestGenerateFilesystem(t *testing.T) { )) assert.Assert(t, fs.Equal(dir.Path(), expected)) -} +} \ No newline at end of file diff --git a/internal/catalog/testdata/tasks/go-crane-image/go-crane-image.yaml b/internal/catalog/testdata/tasks/go-crane-image/go-crane-image.yaml index d09e3e5d..b64cb09e 100644 --- a/internal/catalog/testdata/tasks/go-crane-image/go-crane-image.yaml +++ b/internal/catalog/testdata/tasks/go-crane-image/go-crane-image.yaml @@ -5,6 +5,7 @@ metadata: labels: app.kubernetes.io/version: "0.5.0" annotations: + tekton.dev/source: "https://fake.host/repo/resources.tar.gz" tekton.dev/pipelines.minVersion: "0.50.0" tekton.dev/categories: language tekton.dev/tags: go diff --git a/internal/catalog/testdata/tasks/go-ko-image/go-ko-image.yaml b/internal/catalog/testdata/tasks/go-ko-image/go-ko-image.yaml index 3584cf64..e228bd4d 100644 --- a/internal/catalog/testdata/tasks/go-ko-image/go-ko-image.yaml +++ b/internal/catalog/testdata/tasks/go-ko-image/go-ko-image.yaml @@ -5,6 +5,7 @@ metadata: labels: app.kubernetes.io/version: "0.5.0" annotations: + tekton.dev/source: "https://fake.host/repo/resources.tar.gz" tekton.dev/pipelines.minVersion: "0.50.0" tekton.dev/categories: language tekton.dev/tags: go diff --git a/internal/cmd/testdata/go-crane-image/go-crane-image.yaml b/internal/cmd/testdata/go-crane-image/go-crane-image.yaml index d26fe3b3..8f5fd7e7 100644 --- a/internal/cmd/testdata/go-crane-image/go-crane-image.yaml +++ b/internal/cmd/testdata/go-crane-image/go-crane-image.yaml @@ -1,151 +1,149 @@ -apiVersion: tekton.dev/v1beta1 +apiVersion: tekton.dev/v1 kind: Task metadata: name: go-crane-image labels: - app.kubernetes.io/version: "0.2.1" + app.kubernetes.io/version: "0.5.0" annotations: - tekton.dev/pipelines.minVersion: "0.40.0" + tekton.dev/pipelines.minVersion: "0.50.0" tekton.dev/categories: language tekton.dev/tags: go tekton.dev/displayName: "go crane image" - tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le,linux/arm64" + tekton.dev/platforms: "linux/amd64,linux/arm64" spec: description: >- - The go-crane-image Task will build a container image based of off a go - project to be compiled, using crane. + The go-crane-image Task will build a container image based of off a go project to be compiled, using crane. workspaces: - - name: source - description: The go source to build - - name: dockerconfig - description: Includes a docker `config.json` or `.dockerconfigjson` - optional: true + - name: source + description: The go source to build + - name: dockerconfig + description: Includes a docker `config.json` or `.dockerconfigjson` + optional: true params: - - name: app - description: >- - The name of the "application" to build. This will have an impact on the binary - and possibly the image reference - - name: package - description: >- - The package to build. It needs to be a package `main` that compiles into a binary. - The default value is `.`, usual value can be `./cmd/{name}` - default: . - - name: image - description: >- - The image specific options such as prefix, labels, env, … - type: object - properties: - base: {type: string} - labels: {type: string} - envs: {type: string} - push: {type: string} - prefix: {type: string} - tag: {type: string} - default: - base: "" - labels: "" - envs: "" - push: "true" - tag: "latest" - - name: go - description: >- - Golang options, such as flags, version, … - type: object - properties: - version: {type: string} - GOFLAGS: {type: string} - GOOS: {type: string} - GOARCH: {type: string} - CGO_ENABLED: {type: string} - default: - version: "1.18" - GOFLAGS: "-v" - GOOS: "" - GOARCH: "" - CGO_ENABLED: "0" + - name: app + description: >- + The name of the "application" to build. This will have an impact on the binary and possibly the image reference + - name: package + description: >- + The package to build. It needs to be a package `main` that compiles into a binary. The default value is `.`, usual value can be `./cmd/{name}` + default: . + - name: image + description: >- + The image specific options such as prefix, labels, env, … + type: object + properties: + base: {type: string} + labels: {type: string} + envs: {type: string} + push: {type: string} + prefix: {type: string} + tag: {type: string} + default: + base: "" + labels: "" + envs: "" + push: "true" + tag: "latest" + - name: go + description: >- + Golang options, such as flags, … + type: object + properties: + GOFLAGS: {type: string} + GOOS: {type: string} + GOARCH: {type: string} + CGO_ENABLED: {type: string} + default: + GOFLAGS: "-v" + GOOS: "" + GOARCH: "" + CGO_ENABLED: "0" results: - name: IMAGE_DIGEST description: Digest of the image just built. - name: IMAGE_URL description: URL of the image just built. steps: - - name: build-go - image: docker.io/library/golang:$(params.go.version) - workingDir: $(workspaces.source.path) - script: | - #!/usr/bin/env bash - set -e - go env - go build -o $(params.app) $(params.package) - env: - - name: GOFLAGS - value: "$(params.go.GOFLAGS)" - - name: GOOS - value: "$(params.go.GOOS)" - - name: GOARCH - value: "$(params.go.GOARCH)" - - name: CGO_ENABLED - value: "$(params.go.CGO_ENABLED)" - - name: publish-image - image: ghcr.io/shortbrain/golang-tasks/crane:main - workingDir: $(workspaces.source.path) - script: | - #!/usr/bin/env bash - set -e + - name: build-go + image: ghcr.io/shortbrain/golang-tasks/go-1.21:latest + workingDir: $(workspaces.source.path) + script: | + #!/usr/bin/env bash + set -e - if [[ "$(params.image.push)" == "false" ]]; then - echo "Not doing anything as push is disabled" - echo -n "" > $(resutls.IMAGE_DIGEST.path) - echo -n "" > $(resutls.IMAGE_URL.path) - exit 0 - fi + git config --global --add safe.directory $(workspaces.source.path) - # Prepare the layer to add - mkdir output - cp $(params.app) ./output - # FIXME: extra things to copy ? + go env + go build -o $(params.app) $(params.package) + env: + - name: GOFLAGS + value: "$(params.go.GOFLAGS)" + - name: GOOS + value: "$(params.go.GOOS)" + - name: GOARCH + value: "$(params.go.GOARCH)" + - name: CGO_ENABLED + value: "$(params.go.CGO_ENABLED)" + - name: publish-image + image: ghcr.io/shortbrain/golang-tasks/crane:latest + workingDir: $(workspaces.source.path) + script: | + #!/usr/bin/env bash + set -e - if [[ "$(workspaces.dockerconfig.bound)" == "true" ]]; then - # if config.json exists at workspace root, we use that - if test -f "$(workspaces.dockerconfig.path)/config.json"; then - export DOCKER_CONFIG="$(workspaces.dockerconfig.path)" - # else we look for .dockerconfigjson at the root - elif test -f "$(workspaces.dockerconfig.path)/.dockerconfigjson"; then - cp "$(workspaces.dockerconfig.path)/.dockerconfigjson" "$HOME/.docker/config.json" - export DOCKER_CONFIG="$HOME/.docker" - # need to error out if neither files are present - else - echo "neither 'config.json' nor '.dockerconfigjson' found at workspace root" - exit 1 + if [[ "$(params.image.push)" == "false" ]]; then + echo "Not doing anything as push is disabled" + echo -n "" > $(resutls.IMAGE_DIGEST.path) + echo -n "" > $(resutls.IMAGE_URL.path) + exit 0 fi - fi - APPEND_FLAGS="--new_tag $(params.image.prefix)/$(params.app):$(params.image.tag)" - if [[ -n "$(params.image.base)" ]]; then - APPEND_FLAGS="--base $(params.image.base) ${APPEND_FLAGS}" - fi + # Prepare the layer to add + mkdir output + cp $(params.app) ./output + # FIXME: extra things to copy ? - MUTATE_FLAGS="--entrypoint=/$(params.app) --tag $(params.image.prefix)/$(params.app):$(params.image.tag)" - # envs - while IFS=';' read -ra ENVS; do - for ENV in "${ENVS[@]}"; do - MUTATE_FLAGS="${MUTATE_FLAGS} --env ${ENV}" - done - done <<< "$(params.image.envs)" + if [[ "$(workspaces.dockerconfig.bound)" == "true" ]]; then + # if config.json exists at workspace root, we use that + if test -f "$(workspaces.dockerconfig.path)/config.json"; then + export DOCKER_CONFIG="$(workspaces.dockerconfig.path)" + # else we look for .dockerconfigjson at the root + elif test -f "$(workspaces.dockerconfig.path)/.dockerconfigjson"; then + cp "$(workspaces.dockerconfig.path)/.dockerconfigjson" "$HOME/.docker/config.json" + export DOCKER_CONFIG="$HOME/.docker" + # need to error out if neither files are present + else + echo "neither 'config.json' nor '.dockerconfigjson' found at workspace root" + exit 1 + fi + fi + + APPEND_FLAGS="--new_tag $(params.image.prefix)/$(params.app):$(params.image.tag)" + if [[ -n "$(params.image.base)" ]]; then + APPEND_FLAGS="--base $(params.image.base) ${APPEND_FLAGS}" + fi + + MUTATE_FLAGS="--entrypoint=/$(params.app) --tag $(params.image.prefix)/$(params.app):$(params.image.tag)" + # envs + while IFS=';' read -ra ENVS; do + for ENV in "${ENVS[@]}"; do + MUTATE_FLAGS="${MUTATE_FLAGS} --env ${ENV}" + done + done <<< "$(params.image.envs)" - # labels - while IFS=';' read -ra LABELS; do - for LABEL in "${LABELS[@]}"; do - MUTATE_FLAGS="${MUTATE_FLAGS} --label ${LABEL}" - done - done <<< "$(params.image.labels)" + # labels + while IFS=';' read -ra LABELS; do + for LABEL in "${LABELS[@]}"; do + MUTATE_FLAGS="${MUTATE_FLAGS} --label ${LABEL}" + done + done <<< "$(params.image.labels)" - crane mutate $( \ - crane append ${APPEND_FLAGS} \ - --new_layer <(cd ./output && tar -f - -c .) \ - ) \ - ${MUTATE_FLAGS} > crane_output - CRANE_OUTPUT=$(cat crane_output) - echo -n ${CRANE_OUTPUT#*@} > $(results.IMAGE_DIGEST.path) - echo -n ${CRANE_OUTPUT} > $(results.IMAGE_URL.path) - # echo -n ${CRANE_OUTPUT%@*} > $(results.IMAGE_URL.path) + crane mutate $( \ + crane append ${APPEND_FLAGS} \ + --new_layer <(cd ./output && tar -f - -c .) \ + ) \ + ${MUTATE_FLAGS} > crane_output + CRANE_OUTPUT=$(cat crane_output) + echo -n ${CRANE_OUTPUT#*@} > $(results.IMAGE_DIGEST.path) + echo -n ${CRANE_OUTPUT} > $(results.IMAGE_URL.path) + # echo -n ${CRANE_OUTPUT%@*} > $(results.IMAGE_URL.path) \ No newline at end of file diff --git a/internal/cmd/testdata/go-ko-image/go-ko-image.yaml b/internal/cmd/testdata/go-ko-image/go-ko-image.yaml index bc9bdd2e..56c07230 100644 --- a/internal/cmd/testdata/go-ko-image/go-ko-image.yaml +++ b/internal/cmd/testdata/go-ko-image/go-ko-image.yaml @@ -1,178 +1,173 @@ -apiVersion: tekton.dev/v1beta1 +apiVersion: tekton.dev/v1 kind: Task metadata: name: go-ko-image labels: - app.kubernetes.io/version: "0.2.1" + app.kubernetes.io/version: "0.5.0" annotations: - tekton.dev/pipelines.minVersion: "0.40.0" + tekton.dev/pipelines.minVersion: "0.50.0" tekton.dev/categories: language tekton.dev/tags: go tekton.dev/displayName: "go ko image" - tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le,linux/arm64" + tekton.dev/platforms: "linux/amd64,linux/arm64" spec: description: >- - The go-koimage Task will build a container image based of off a go - project using ko. + The go-koimage Task will build a container image based of off a go project using ko. workspaces: - - name: source - description: The go source to build - - name: dockerconfig - description: Includes a docker `config.json` or `.dockerconfigjson` - optional: true + - name: source + description: The go source to build + - name: dockerconfig + description: Includes a docker `config.json` or `.dockerconfigjson` + optional: true params: - - name: app - description: >- - The name of the "application" to build. This will have an impact on the binary - and possibly the image reference - - name: package - description: >- - The package to build. It needs to be a package `main` that compiles into a binary. - The default value is `.`, usual value can be `./cmd/{name}` - default: . - - name: flags - description: >- - ko extra flags to pass to the ko command - default: "--sbom none" - - name: image - description: >- - The image specific options such as prefix, labels, env, … - type: object - properties: - base: {type: string} - labels: {type: string} - envs: {type: string} - push: {type: string} - prefix: {type: string} - tag: {type: string} - default: - base: "" - labels: "" - envs: "" - push: "true" - tag: "latest" - - name: go - description: >- - Golang options, such as flags, version, … - type: object - properties: - # FIXME: support go version - # version: {type: string} - GOFLAGS: {type: string} - GOOS: {type: string} - GOARCH: {type: string} - CGO_ENABLED: {type: string} - default: - # version: "1.18" - GOFLAGS: "-v" - GOOS: "" - GOARCH: "" - CGO_ENABLED: "0" + - name: app + description: >- + The name of the "application" to build. This will have an impact on the binary and possibly the image reference + - name: package + description: >- + The package to build. It needs to be a package `main` that compiles into a binary. The default value is `.`, usual value can be `./cmd/{name}` + default: . + - name: flags + description: >- + ko extra flags to pass to the ko command + default: "--sbom none" + - name: image + description: >- + The image specific options such as prefix, labels, env, … + type: object + properties: + base: {type: string} + labels: {type: string} + envs: {type: string} + push: {type: string} + prefix: {type: string} + tag: {type: string} + default: + base: "" + labels: "" + envs: "" + push: "true" + tag: "latest" + - name: go + description: >- + Golang options, such as flags, version, … + type: object + properties: + GOFLAGS: {type: string} + GOOS: {type: string} + GOARCH: {type: string} + CGO_ENABLED: {type: string} + default: + GOFLAGS: "-v" + GOOS: "" + GOARCH: "" + CGO_ENABLED: "0" results: - name: IMAGE_DIGEST description: Digest of the image just built. - name: IMAGE_URL description: URL of the image just built. steps: - - name: build-and-publish - image: ghcr.io/ko-build/ko:latest - workingDir: $(workspaces.source.path) - script: | - #!/usr/bin/env bash - set -e + - name: build-and-publish + image: ghcr.io/shortbrain/golang-tasks/ko-go-1.21:latest + workingDir: $(workspaces.source.path) + script: | + #!/usr/bin/env bash + set -e - if [[ "$(workspaces.dockerconfig.bound)" == "true" ]]; then - # if config.json exists at workspace root, we use that - if test -f "$(workspaces.dockerconfig.path)/config.json"; then - export DOCKER_CONFIG="$(workspaces.dockerconfig.path)" - # else we look for .dockerconfigjson at the root - elif test -f "$(workspaces.dockerconfig.path)/.dockerconfigjson"; then - cp "$(workspaces.dockerconfig.path)/.dockerconfigjson" "$HOME/.docker/config.json" - export DOCKER_CONFIG="$HOME/.docker" - # need to error out if neither files are present - else - echo "neither 'config.json' nor '.dockerconfigjson' found at workspace root" - exit 1 + git config --global --add safe.directory $(workspaces.source.path) + if [[ "$(workspaces.dockerconfig.bound)" == "true" ]]; then + # if config.json exists at workspace root, we use that + if test -f "$(workspaces.dockerconfig.path)/config.json"; then + export DOCKER_CONFIG="$(workspaces.dockerconfig.path)" + # else we look for .dockerconfigjson at the root + elif test -f "$(workspaces.dockerconfig.path)/.dockerconfigjson"; then + cp "$(workspaces.dockerconfig.path)/.dockerconfigjson" "$HOME/.docker/config.json" + export DOCKER_CONFIG="$HOME/.docker" + # need to error out if neither files are present + else + echo "neither 'config.json' nor '.dockerconfigjson' found at workspace root" + exit 1 + fi fi - fi - KO_FLAGS="$(params.flags) --tags $(params.image.tag)" - if [[ "$(params.image.push)" == "false" ]]; then - KO_FLAGS="${KO_FLAGS} --push=false" - fi + KO_FLAGS="$(params.flags) --tags $(params.image.tag)" + if [[ "$(params.image.push)" == "false" ]]; then + KO_FLAGS="${KO_FLAGS} --push=false" + fi - # labels - while IFS=';' read -ra LABELS; do - for LABEL in "${LABELS[@]}"; do - KO_FLAGS="${KO_FLAGS} --image-label ${LABEL}" - done - done <<< "$(params.image.labels)" + # labels + while IFS=';' read -ra LABELS; do + for LABEL in "${LABELS[@]}"; do + KO_FLAGS="${KO_FLAGS} --image-label ${LABEL}" + done + done <<< "$(params.image.labels)" - go env + go env - echo "defaultBaseImage: $(params.image.base)" > .ko.yaml + echo "defaultBaseImage: $(params.image.base)" > .ko.yaml - set -x - ko build --base-import-paths ${KO_FLAGS} $(params.package) > $(results.IMAGE_URL.path) - set +x + set -x + ko build --base-import-paths ${KO_FLAGS} $(params.package) > $(results.IMAGE_URL.path) + set +x - KO_OUTPUT=$(results.IMAGE_URL.path) - echo -n ${CRANE_OUTPUT#*@} > $(results.IMAGE_DIGEST.path) - env: - - name: GOFLAGS - value: "$(params.go.GOFLAGS)" - - name: GOOS - value: "$(params.go.GOOS)" - - name: GOARCH - value: "$(params.go.GOARCH)" - - name: CGO_ENABLED - value: "$(params.go.CGO_ENABLED)" - - name: KO_DOCKER_REPO - value: $(params.image.prefix) - - name: add-metadata - image: ghcr.io/shortbrain/golang-tasks/crane:main - workingDir: $(workspaces.source.path) - script: | - #!/usr/bin/env bash - set -e + KO_OUTPUT=$(results.IMAGE_URL.path) + echo -n ${CRANE_OUTPUT#*@} > $(results.IMAGE_DIGEST.path) + env: + - name: GOFLAGS + value: "$(params.go.GOFLAGS)" + - name: GOOS + value: "$(params.go.GOOS)" + - name: GOARCH + value: "$(params.go.GOARCH)" + - name: CGO_ENABLED + value: "$(params.go.CGO_ENABLED)" + - name: KO_DOCKER_REPO + value: $(params.image.prefix) + - name: publish-image + image: ghcr.io/shortbrain/golang-tasks/crane:latest + workingDir: $(workspaces.source.path) + script: | + #!/usr/bin/env bash + set -e - if [[ "$(params.image.push)" == "false" ]]; then - echo "Not doing anything as push is disabled" - echo -n "" > $(resutls.IMAGE_DIGEST.path) - echo -n "" > $(resutls.IMAGE_URL.path) - exit 0 - fi + if [[ "$(params.image.push)" == "false" ]]; then + echo "Not doing anything as push is disabled" + echo -n "" > $(resutls.IMAGE_DIGEST.path) + echo -n "" > $(resutls.IMAGE_URL.path) + exit 0 + fi - if [[ "$(workspaces.dockerconfig.bound)" == "true" ]]; then - # if config.json exists at workspace root, we use that - if test -f "$(workspaces.dockerconfig.path)/config.json"; then - export DOCKER_CONFIG="$(workspaces.dockerconfig.path)" - # else we look for .dockerconfigjson at the root - elif test -f "$(workspaces.dockerconfig.path)/.dockerconfigjson"; then - cp "$(workspaces.dockerconfig.path)/.dockerconfigjson" "$HOME/.docker/config.json" - export DOCKER_CONFIG="$HOME/.docker" - # need to error out if neither files are present - else - echo "neither 'config.json' nor '.dockerconfigjson' found at workspace root" - exit 1 + if [[ "$(workspaces.dockerconfig.bound)" == "true" ]]; then + # if config.json exists at workspace root, we use that + if test -f "$(workspaces.dockerconfig.path)/config.json"; then + export DOCKER_CONFIG="$(workspaces.dockerconfig.path)" + # else we look for .dockerconfigjson at the root + elif test -f "$(workspaces.dockerconfig.path)/.dockerconfigjson"; then + cp "$(workspaces.dockerconfig.path)/.dockerconfigjson" "$HOME/.docker/config.json" + export DOCKER_CONFIG="$HOME/.docker" + # need to error out if neither files are present + else + echo "neither 'config.json' nor '.dockerconfigjson' found at workspace root" + exit 1 + fi fi - fi - if [[ -z "$(params.image.envs)" ]]; then - echo "Not doing anything as there is no envs specified" - fi + if [[ -z "$(params.image.envs)" ]]; then + echo "Not doing anything as there is no envs specified" + fi - MUTATE_FLAGS=" --tag $(params.image.prefix)/$(params.app)" - # envs - while IFS=';' read -ra ENVS; do - for ENV in "${ENVS[@]}"; do - MUTATE_FLAGS="${MUTATE_FLAGS} --env ${ENV}" - done - done <<< "$(params.image.envs)" + MUTATE_FLAGS=" --tag $(params.image.prefix)/$(params.app)" + # envs + while IFS=';' read -ra ENVS; do + for ENV in "${ENVS[@]}"; do + MUTATE_FLAGS="${MUTATE_FLAGS} --env ${ENV}" + done + done <<< "$(params.image.envs)" - crane mutate $(cat $(results.IMAGE_URL.path)) \ - ${MUTATE_FLAGS} > crane_output - CRANE_OUTPUT=$(cat crane_output) - echo -n ${CRANE_OUTPUT#*@} > $(results.IMAGE_DIGEST.path) - echo -n ${CRANE_OUTPUT} > $(results.IMAGE_URL.path) - # echo -n ${CRANE_OUTPUT%@*} > $(results.IMAGE_URL.path) + crane mutate $(cat $(results.IMAGE_URL.path)) \ + ${MUTATE_FLAGS} > crane_output + CRANE_OUTPUT=$(cat crane_output) + echo -n ${CRANE_OUTPUT#*@} > $(results.IMAGE_DIGEST.path) + echo -n ${CRANE_OUTPUT} > $(results.IMAGE_URL.path) + # echo -n ${CRANE_OUTPUT%@*} > $(results.IMAGE_URL.path) \ No newline at end of file