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 startoutput contains user login and password needed for logging into the cluster. -
Add the cached
ocbinary 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
pipelineservice account:$ oc get serviceaccount pipeline
-
In the OpenShift Web Console switch to the Developer perspective of the OpenShift Web Console.
-
Select the
pipelines-tutorialproject 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. ThePipelineyou 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
TaskYAML 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: - installThis
Taskstarts a pod and runs a container inside that pod using themaven:3.6.0-jdk-8-slimimage to run the specified commands. It receives an input directory calledworkspace-gitwhich contains the source code of the application.Only the requirement for a git repository is declared on the
Taskand not a specific git repository to be used. This allowsTasksto be reusable for multiplePipelines and purposes. -
Create the
Taskwithin your project:oc create -f maven-build-task.yaml
-
-
Install the
apply-manifestsandupdate-deploymenttasks from the repositories, which contain a list of reusableTasksfor `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
Tasksadded to thePipelineas follows:$ tkn task ls NAME AGE apply-manifests 10 seconds ago update-deployment 4 seconds ago
-
Verify Operator installed additional tasks -
buildahands2i-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
Taskssee 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.yamlfile 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
PipelineResourcethat 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
PipelineResourcethat 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
PipelineResourcethat 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
PipelineResourcethat 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
PipelineYAML 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-manifestsThis
Pipelineperforms the following tasks:-
Clones the source code of the frontend application from the
api-repoGit repository and the backend application from theui-reporesourceGit repository. -
Builds the container image for the frontend using the
s2i-python-3task that generates a Dockerfile for the application using Source-to-Image (S2I). -
Builds the container image for the backend using the
buildahtask that usesBuildahto build the image. -
The application image is pushed to an image registry.
-
The new application image is deployed on OpenShift using the
apply-manifestsandupdate-deploymenttasks.Pipelines 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 duringPipelineexecution.The execution order of the tasks is determined by the dependencies defined between the tasks using the
inputsandoutputsparameters, 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
Pipelinehas 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.