Sub-pipeline
In the Radix pipeline, optionally a sub-pipeline can be run. It is run after "Build components step" (if components need to be built) or after "Prepare pipeline" step. This sub-pipeline is based on the Tekton CI/CD framework.
Note on nomenclature! The content in this guide concerns a Tekton pipeline which is defined as a pipeline within a parent Radix pipeline. In the context of the Radix platform, a Tekton pipeline is referred to as a sub-pipeline or Tekton pipeline, while the parent Radix pipeline is referred to as a pipeline or Radix pipeline.
Configure sub-pipeline
Sub-pipeline is configured with file pipeline.yaml. This file will in turn have references to one or several other YAML-files which define tasks for the sub-pipeline.
Pipeline and task files
- Default folder for sub-pipeline is
tekton, next to the Radix configuration file of the application. - Default name for the sub-pipeline is
pipeline.yaml. - Files with sub-pipeline task configurations should be located next to the file
pipeline.yaml.
Example: a sub-pipeline pipeline.yaml refers to tasks in files clone.yaml, build.yaml, migration.yaml
/
├── component1
├── component2
├── tekton/
│ ├── pipeline.yaml
│ ├── clone.yaml
│ ├── build.yaml
│ └── migration.yaml
└── radixconfig.yaml
Suppose an app has a sub-pipeline defined, like the example above. Within the Radix pipeline step "Prepare pipeline", the following logic will be executed:
- the sub-pipeline defined in
pipeline.yamlis loaded - task files referred to inside the
pipeline.yamlfile are loaded, which areclone.yaml,build.yamlandmigration.yaml - if an error occurred during loading of the sub-pipeline or its tasks, the step "Prepare pipeline" and entire Radix pipeline job is considered failed
- the sub-pipeline is run
- if any step of any task is failed - the pipeline gets status "failed", the step "Run sub-pipeline" and entire Radix pipeline job gets the status "failed"
Errors in stage (3) can be caused by:
- an invalid format of a sub-pipeline or tasks files
- an empty list of tasks in a sub-pipeline
- a missing task, referenced in a sub-pipeline
- empty step list in a task
Follow the Tekton documentation to configure a sub-pipeline and its tasks, particularly Tekton pipeline and task documentation.
Custom values supplied by Radix
Radix provides a set of runtime values that can be referenced directly in sub-pipeline definitions.
-
$(radix.git-deploy-key)is the placeholder of a read-only volume created by Radix. Mount this volume in a task step when the step needs SSH credentials for Git operations, such as cloning the application's repository. Refer to example Sub-pipeline with GitHub deploy keys for usage. -
$(radix.build-secrets)is the placeholder for the application's configured build secrets. Use it when you want to expose build secrets to a task step throughenv,envFrom, or mounted files. Refer to example Sub-pipeline with build secrets for usage. -
$(params.radix)is a reserved parameter injected by Radix into the sub-pipeline and automatically forwarded to every task. Do not define a parameter namedradixyourself in the pipeline or task files. Refer to example Sub-pipeline with GitHub deploy keys for usage.You can reference individual properties with the syntax
$(params.radix.<property-name>), for example$(params.radix.git-commit).The following properties are available on
$(params.radix):Property Description pipeline-typeType of the parent Radix pipeline that is running the sub-pipeline, for example deploy,build-deploy, orpromote.environmentName of the target environment for the current sub-pipeline run. git-ssh-urlSSH clone URL for the application repository. Use this together with $(radix.git-deploy-key)when a task needs to clone the repository over SSH.git-refGit branch or tag for the pipeline run. git-ref-typeType of git-ref. The value isbranchortag.git-commitResolved Git commit SHA for the pipeline run. git-tagsSpace-separated list of Git tags that point to git-commit. This value is empty when the commit has no matching tags. -
$(params.radix-image.<component-name>)references the fully qualified image built by Radix for a specific component or job. Use this in a task step'simagefield to run the step using the same image as the named component or job, without hardcoding registry paths or image tags.Replace
<component-name>with the name of the component or job as defined inradixconfig.yaml. The parameter is injected automatically by Radix and forwarded to every task in the sub-pipeline.This is useful for tasks that need to execute code that is part of your application such as database migrations, schema validation or smoke tests. The step runs the exact image being deployed, with the app's runtime and tooling already present.
apiVersion: tekton.dev/v1kind: Taskmetadata:name: run-migrationsspec:steps:- image: $(params.radix-image.server)name: run-db-migrationsscript: /bin/some-binary migrate-databaseIn this example,
serveris the name of a component defined inradixconfig.yaml. The step runs using that component's image, which was built earlier in the same Radix pipeline run.tipThe
$(params.radix-image.<component-name>)parameter is available inbuild-deploy, andpromotepipeline runs.
Limitations
In Radix platform, the following limitations are applied to sub-pipelines:
- sub-pipeline does not support workspaces. However, it is possible to use volumes in sub-pipeline tasks.
- sub-pipeline Task step cannot mount secrets as volumes, with some exceptions:
- the secret to access private image repository, which is mounted automatically
- build secrets
- sub-pipeline Task step cannot run as a privileged container (e.g. cannot run as root) or with a host network
- if a container image used in a step is configured to run as a root, this user can (and should) be changed to a non-root user with a field
securityContext.runAsUserin the step definition,securityContext.runAsGroupis also supported.runAsUserandrunAsGroupcannot have value0(=rootuser).
apiVersion: tekton.dev/v1kind: Taskmetadata:name: my-taskspec:steps:- image: alpinename: show-user-idscript: |#!/usr/bin/env shid:securityContext:runAsUser: 1000SuggestionThe following command can be used to find out with which user the image runs its container:
docker run -it alpine id - if a container image used in a step is configured to run as a root, this user can (and should) be changed to a non-root user with a field
Hints
-
Tekton pipeline and tasks can be developed and tested on PC within local Kubernetes cluster.
-
Name of a task, file name of a task and a name of a task in the Tekton pipeline task list - all can be different. It is important only to use the same name in the task field
metadata.nameand in the Tekton pipeline fieldtaskRef.name. In the example below it is namebuild-image: -
File
pipeline.yaml:apiVersion: tekton.dev/v1kind: Pipelinemetadata:name: pipelinespec:tasks:- name: some-build-tasktaskRef:name: build-imageFile
build-image-task.yaml:apiVersion: tekton.dev/v1kind: Taskmetadata:name: build-imagespec:steps:... -
It is not important in which order to put tasks in the sub-pipeline - tasks can run in parallel or in sequences, defined by fields runAfter, conditions, from.
-
If a task has a field
runAfter- it will be started only when all tasks, referenced in the fieldrunAfterare complete. -
Task details:
-
Each sub-pipeline task runs in its own Kubernetes pod (replica).
-
Task step runs in its own container of this task's pod.
-
Task step can be configured individually: which container image and how many resources to use, how to proceed on an error, specify a timeout, if the task runs script - is it bash or PowerShell script, etc.
-
When task step uses
script- it would be recommended to finish this script with theno-opcommand: put:(colon) on the last new line of the script. It will help to avoid some irrelevant errors (e.g. in the example below: run of this task raises an error, when the commandprintenv|grep "DB"is on the last line of the script and there are no environment variables with a fragment "DB" in names). Or just put a command likeecho ""spec:steps:- image: alpinename: show-db-env-varsscript: |#!/usr/bin/env shprintenv|grep "DB":
-