Using Pipelines
The following section demonstrates how to trigger and run pipelines as well as to check on the application status.
Deploying an application to OpenShift
Prerequisites
-
You have installed OpenShift Pipelines using the OpenShift Pipeline Operator listed in the OpenShift OperatorHub. Once installed, it is applicable to the entire cluster.
-
You have installed the Tekton CLI
-
You have installed CodeReady Containers (CRC) - a recommended solution for quick deployment of OpenShift clusters locally. For more information read the documentation for CRC.
Procedure
-
Create a local CRC virtual machine:
$ crc start
If you use a hosted hypervisor, see the CRC documentation. crc start
output contains user login and password needed for logging into the cluster. -
Add the cached
oc
binary to your PATH:-
Run the crc oc-env command to print the command needed to add the cached oc binary to your PATH:
$ crc oc-env
-
Run the printed command.
-
-
Log into the OpenShift cluster with the provided login and password:
$ oc login -u <login> -p <password> https://api.crc.testing:6443
-
Create a project for the sample application:
$ oc new-project pipelines-tutorial
-
Run the following command to see the
pipeline
service account:$ oc get serviceaccount pipeline
-
In the OpenShift Web Console switch to the Developer perspective of the OpenShift Web Console.
-
Select the
pipelines-tutorial
project from the Project drop-down menu. On the Topology view of the Developer perspective, you will be able to see the resources of your Pipeline. The deployment at this stage is not complete. ThePipeline
you create in the next steps builds the simple application which has a frontend and a backend and completes the deployment.
Defining and creating pipeline tasks
Tasks
are the building blocks of a Pipeline
and consist of sequentially executed steps. steps
are a series of commands that achieve a specific goal, for example, build an image.
Every Task
runs as a pod and each step
runs in its own container within the same pod. Since they run within the same pod they have access to the same volumes to cache files, configmaps, and secrets. They use inputs
parameters, for example, a git , and outputs
parameters, for example, an image in a registry, to interact with other tasks.
Tasks
are reusable and can be used in multiple Pipeline
s. For this tutorial, you create a Maven Task
with a single step to build a Maven based application, and then add two reusable Tasks
from the catalog repository.
Procedure
-
To create the Maven
Task
:-
Copy the contents of the following sample
Task
YAML file and save it:apiVersion: tekton.dev/v1alpha1 kind: Task metadata: name: maven-build spec: inputs: resources: - name: workspace-git targetPath: / type: git steps: - name: build image: maven:3.6.0-jdk-8-slim command: - /usr/bin/mvn args: - install
This
Task
starts a pod and runs a container inside that pod using themaven:3.6.0-jdk-8-slim
image to run the specified commands. It receives an input directory calledworkspace-git
which contains the source code of the application.Only the requirement for a git repository is declared on the
Task
and not a specific git repository to be used. This allowsTasks
to be reusable for multiplePipeline
s and purposes. -
Create the
Task
within your project:oc create -f maven-build-task.yaml
-
-
Install the
apply-manifests
andupdate-deployment
tasks from the repositories, which contain a list of reusableTasks
for `Pipeline`s:$ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/master/pipeline/update_deployment_task.yaml $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/master/pipeline/apply_manifest_task.yaml
-
Verify the
Tasks
added to thePipeline
as follows:$ tkn task ls NAME AGE apply-manifests 10 seconds ago update-deployment 4 seconds ago
-
Verify Operator installed additional tasks -
buildah
ands2i-python-3
:$ tkn clustertask ls NAME AGE buildah 9 hours ago buildah-v0-8-0 9 hours ago openshift-client 9 hours ago openshift-client-v0-8-0 9 hours ago s2i 9 hours ago s2i-go 9 hours ago s2i-go-v0-8-0 9 hours ago s2i-java-11 9 hours ago s2i-java-11-v0-8-0 9 hours ago s2i-java-8 9 hours ago s2i-java-8-v0-8-0 9 hours ago s2i-nodejs 9 hours ago s2i-nodejs-v0-8-0 9 hours ago s2i-python-3 9 hours ago s2i-python-3-v0-8-0 9 hours ago s2i-v0-8-0 9 hours ago
Additional resources
-
For more examples of reusable
Tasks
see the Tekton Catalog and OpenShift Catalog repositories.
Defining and creating pipeline resources
PipelineResources
are artifacts that are used as inputs to a Task
and can be output by a Task
.
Procedure
After you create Task`s, create `PipelineResources
that contain the specifics of the Git repository and the image registry to be used in the Pipeline
during execution as follows:
-
Create a
resources.yaml
file and with the `PipelineResource`s that contain the specifics of the Git repository and the image registry to be used in the pipeline during execution.-
Create a
PipelineResource
that defines the Git repository for the frontend application:apiVersion: tekton.dev/v1alpha1 kind: PipelineResource metadata: name: ui-repo spec: type: git params: - name: url value: http://github.com/openshift-pipelines/vote-ui.git
-
Create a
PipelineResource
that defines the OpenShift internal image registry to which you want to push the frontend image:apiVersion: tekton.dev/v1alpha1 kind: PipelineResource metadata: name: ui-image spec: type: image params: - name: url value: image-registry.openshift-image-registry.svc:5000/pipelines-tutorial/ui:latest
-
Create a
PipelineResource
that defines the Git repository for the backend application:apiVersion: tekton.dev/v1alpha1 kind: PipelineResource metadata: name: api-repo spec: type: git params: - name: url value: http://github.com/openshift-pipelines/vote-api.git
-
Create a
PipelineResource
that defines the OpenShift internal image registry to which you want to push the backend image:apiVersion: tekton.dev/v1alpha1 kind: PipelineResource metadata: name: api-image spec: type: image params: - name: url value: image-registry.openshift-image-registry.svc:5000/pipelines-tutorial/api:latest
-
-
Create the Pipeline Resources:
$ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/master/pipeline/resources.yaml
-
See the list of resources:
$ tkn resource ls NAME TYPE DETAILS api-repo git url: http://github.com/openshift-pipelines/vote-api.git ui-repo git url: http://github.com/openshift-pipelines/vote-ui.git api-image image url: image-registry.openshift-image-registry.svc:5000/pipelines-tutorial/api:latest ui-image image url: image-registry.openshift-image-registry.svc:5000/pipelines-tutorial/ui:latest
Assembling a pipeline
A Pipeline
represents a CI/CD flow and is defined by the Tasks
to be executed. It specifies how the Tasks
interact with each other and their order of execution using the inputs
, outputs
, and the run-After
parameters. It is designed to be generic and reusable in multiple applications and environments.
In this section you will create a Pipeline that takes the source code of the application from GitHub and then builds and deploys it on OpenShift:
Procedure
-
Copy the contents of the following sample
Pipeline
YAML file and save it:apiVersion: tekton.dev/v1alpha1 kind: Pipeline metadata: name: build-and-deploy spec: resources: - name: api-repo type: git - name: api-image type: image - name: ui-repo type: git - name: ui-image type: image tasks: - name: build-api taskRef: name: buildah kind: ClusterTask resources: inputs: - name: source resource: api-repo outputs: - name: image resource: api-image params: - name: TLSVERIFY value: "false" - name: apply-api-manifests taskRef: name: apply-manifests resources: inputs: - name: source resource: api-repo runAfter: - build-api - name: update-api-image taskRef: name: update-deployment resources: inputs: - name: image resource: api-image params: - name: deployment value: "api" runAfter: - apply-api-manifests - name: build-ui taskRef: name: s2i-python-3 kind: ClusterTask resources: inputs: - name: source resource: ui-repo outputs: - name: image resource: ui-image params: - name: TLSVERIFY value: "false" - name: apply-ui-manifests taskRef: name: apply-manifests resources: inputs: - name: source resource: ui-repo runAfter: - build-ui - update-api-image - name: update-ui-image taskRef: name: update-deployment resources: inputs: - name: image resource: ui-image params: - name: deployment value: "ui" runAfter: - apply-ui-manifests
This
Pipeline
performs the following tasks:-
Clones the source code of the frontend application from the
api-repo
Git repository and the backend application from theui-reporesource
Git repository. -
Builds the container image for the frontend using the
s2i-python-3
task that generates a Dockerfile for the application using Source-to-Image (S2I). -
Builds the container image for the backend using the
buildah
task that usesBuildah
to build the image. -
The application image is pushed to an image registry.
-
The new application image is deployed on OpenShift using the
apply-manifests
andupdate-deployment
tasks.Pipeline
s in Tekton are designed to be generic and re-usable across environments and stages through the applications lifecycle. They abstract away the specifics of the Git source repository and image registries to be used duringPipeline
execution.The execution order of the tasks is determined by the dependencies defined between the tasks using the
inputs
andoutputs
parameters, and the explicit orders that are defined usingrunAfter
.
-
-
Create the
Pipeline
:$ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/master/pipeline/pipeline.yaml
-
Verify that the
Pipeline
has been added to the application as follows:$ tkn pipeline ls NAME AGE LAST RUN STARTED DURATION STATUS build-and-deploy 1 minute ago --- --- --- ---
Additional resources
-
A bulleted list of links to other material closely related to the contents of the procedure module.
-
For more details on writing procedure modules, see the Modular Documentation Reference Guide.
-
Use a consistent system for file names, IDs, and titles. For tips, see Anchor Names and File Names in Modular Documentation Reference Guide.
Additional resources
-
A bulleted list of links to other material closely related to the contents of the procedure module.
-
For more details on writing procedure modules, see the Modular Documentation Reference Guide.
-
Use a consistent system for file names, IDs, and titles. For tips, see Anchor Names and File Names in Modular Documentation Reference Guide.
Additional resources
-
A bulleted list of links to other material closely related to the contents of the procedure module.
-
For more details on writing procedure modules, see the Modular Documentation Reference Guide.
-
Use a consistent system for file names, IDs, and titles. For tips, see Anchor Names and File Names in Modular Documentation Reference Guide.