Cyac
Cyac

Reputation: 477

Passing variables between Tekton Steps

I am trying to implement a basic Tekton CI pipeline. All the pipeline does is 1) get the source code 2) build an image with a new version and push it to an image registry.

The image version is generated by a Tekton Step. Images are built by another Tekton step that uses Kaniko as described here.

I am aware of using workspaces to pass variables between Tekton steps. Meaning I can write the version to a file in the workspace. But cant figure out a way to read this version from the file in the Kaniko build step below:

  steps:
    - name: build-and-push
      image: gcr.io/kaniko-project/executor:latest
      # specifying DOCKER_CONFIG is required to allow kaniko to detect docker credential
      env:
        - name: "DOCKER_CONFIG"
          value: "/tekton/home/.docker/"
      command:
        - /kaniko/executor
      args:
        - --dockerfile=$(params.pathToDockerFile)
        - --destination=$(resources.outputs.builtImage.url):<IMAGE-VERSION-NEEDED-HERE>
        - --context=$(params.pathToContext)
        - --build-arg=BASE=alpine:3

There should be a common pattern to resolve this but I am not sure if I am looking at the right places in Tekton documentation for this.

Can anyone offer some pointers?

Upvotes: 2

Views: 5192

Answers (3)

t7e
t7e

Reputation: 509

You can use the gcr.io/kaniko-project/executor:debug image that has shell at /busybox/sh.

And create something like this (pass kaniko commands via script):

  steps:
    - name: write-to-workspace
      image: ubuntu
      script: |
        #!/usr/bin/env bash
        echo "IMAGE_VERSION" > /workspace/FOO
    - name: read-from-workspace
      image: gcr.io/kaniko-project/executor:debug
      script: |
        #!/busybox/sh
        export IMAGE_VERSION=$(cat /workspace/FOO)
        echo "$IMAGE_VERSION"
        /kaniko/executor \
          --dockerfile=$(params.pathToDockerFile) \
          --destination=$(resources.outputs.builtImage.url):"${IMAGE_VERSION}" \
          --context=$(params.pathToContext) \
          --build-arg=BASE=alpine:3

You can refer to this discussion: https://github.com/tektoncd/pipeline/issues/1476

Upvotes: 0

andreaf
andreaf

Reputation: 86

All steps in a Task share the same Pod and thus as access to a shared workspace implemented as an emptyDir volume:

Volumes:
  tekton-internal-workspace:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  <unset>

A common way to share data between steps is to a file in the /workspace and read it in the next step.

Alternatively, as suggested by @Jonas, if you use different Tasks you can write a result in the first Task and feed it into a parameter of the second Task in the Pipeline definition.

Using results this way implicitly creates a dependency between the two Tasks, so the Tekton controller won't schedule the second Task until the first one has successfully completed and results are available.

Upvotes: 0

Cyac
Cyac

Reputation: 477

This is to confirm that I managed to resolve the issue by redesigning the steps to tasks as suggested by @Jonas.

Tekton Tasks can have outputs which can be referred in other tasks. At the time of writing this Tekton steps don't seem to have this feature.

For more details refer the links in @Jonas comments above.

Upvotes: 1

Related Questions