|
| 1 | +`Tasks` consist of some steps that get executed sequentially. Each step gets executed in a separate container within the same task pod. They can have inputs and outputs to interact with other `Tasks` as part of a pipeline. |
| 2 | + |
| 3 | +For this exercise, you will create the _s2i-nodejs_ task from the catalogue repositories using oc. This is the first of two tasks you add to your pipeline for this workshop. |
| 4 | + |
| 5 | +The _s2i-nodejs_ task has been broken into pieces below to help highlight its key aspects. |
| 6 | + |
| 7 | +_s2i-nodejs_ starts by defining a property called inputs, as shown below. Underneath inputs, a property called resources specify that a resource of type _git_ is required. This property indicates that this task takes a git repository as an input. |
| 8 | + |
| 9 | +[source,yaml] |
| 10 | +---- |
| 11 | +spec: |
| 12 | + inputs: |
| 13 | + resources: |
| 14 | + - name: source |
| 15 | + type: git |
| 16 | +---- |
| 17 | + |
| 18 | +The params property below defines fields that must be specified when using the task (e.g. the version of Node.js to use). |
| 19 | + |
| 20 | +[source,yaml] |
| 21 | +---- |
| 22 | + params: |
| 23 | + - name: VERSION |
| 24 | + description: The version of the nodejs |
| 25 | + default: '12' |
| 26 | + - name: PATH_CONTEXT |
| 27 | + description: The location of the path to run s2i from. |
| 28 | + default: . |
| 29 | + - name: TLSVERIFY |
| 30 | + description: Verify the TLS on the registry endpoint (for push/pull to a non-TLS registry) |
| 31 | + default: "true" |
| 32 | +---- |
| 33 | + |
| 34 | +There is also an _outputs_ property shown below that is used to specify that something is output as a result of running this task. The type of output is _image_. This property specifies that this task creates an image from the git repository provided as an input. |
| 35 | + |
| 36 | +Many resource types are possible and not only limited to git and image. You can find out more about the possible resource types in the https://github.com/tektoncd/pipeline/blob/master/docs/resources.md#resource-types[Tekton documentation]. |
| 37 | + |
| 38 | +[source,yaml] |
| 39 | +---- |
| 40 | + outputs: |
| 41 | + resources: |
| 42 | + - name: image |
| 43 | + type: image |
| 44 | +---- |
| 45 | + |
| 46 | +For each step of the task, a steps property is used to define what steps will run during this task. Each step is denoted by its name. _s2i-nodejs_ has three steps: |
| 47 | + |
| 48 | +generate |
| 49 | + |
| 50 | +[source,yaml] |
| 51 | +---- |
| 52 | + - name: generate |
| 53 | + image: quay.io/openshift-pipeline/s2i |
| 54 | + workingdir: /workspace/source |
| 55 | + command: ['s2i', 'build', '$(inputs.params.PATH_CONTEXT)', 'centos/nodejs-$(inputs.params.VERSION)-centos7', '--as-dockerfile', '/gen-source/Dockerfile.gen'] |
| 56 | + volumeMounts: |
| 57 | + - name: gen-source |
| 58 | + mountPath: /gen-source |
| 59 | + resources: |
| 60 | + limits: |
| 61 | + cpu: 500m |
| 62 | + memory: 1Gi |
| 63 | + requests: |
| 64 | + cpu: 500m |
| 65 | + memory: 1Gi |
| 66 | +---- |
| 67 | + |
| 68 | +build |
| 69 | + |
| 70 | +[source,yaml] |
| 71 | +---- |
| 72 | + - name: build |
| 73 | + image: quay.io/buildah/stable |
| 74 | + workingdir: /gen-source |
| 75 | + command: ['buildah', 'bud', '--tls-verify=$(inputs.params.TLSVERIFY)', '--layers', '-f', '/gen-source/Dockerfile.gen', '-t', '$(outputs.resources.image.url)', '.'] |
| 76 | + volumeMounts: |
| 77 | + - name: varlibcontainers |
| 78 | + mountPath: /var/lib/containers |
| 79 | + - name: gen-source |
| 80 | + mountPath: /gen-source |
| 81 | + resources: |
| 82 | + limits: |
| 83 | + cpu: 500m |
| 84 | + memory: 1Gi |
| 85 | + requests: |
| 86 | + cpu: 500m |
| 87 | + memory: 1Gi |
| 88 | + securityContext: |
| 89 | + privileged: true |
| 90 | +---- |
| 91 | + |
| 92 | +push |
| 93 | + |
| 94 | +[source,yaml] |
| 95 | +---- |
| 96 | + - name: push |
| 97 | + image: quay.io/buildah/stable |
| 98 | + command: ['buildah', 'push', '--tls-verify=$(inputs.params.TLSVERIFY)', '$(outputs.resources.image.url)', 'docker://$(outputs.resources.image.url)'] |
| 99 | + volumeMounts: |
| 100 | + - name: varlibcontainers |
| 101 | + mountPath: /var/lib/containers |
| 102 | + resources: |
| 103 | + limits: |
| 104 | + cpu: 500m |
| 105 | + memory: 1Gi |
| 106 | + requests: |
| 107 | + cpu: 500m |
| 108 | + memory: 1Gi |
| 109 | + securityContext: |
| 110 | + privileged: true |
| 111 | +---- |
| 112 | + |
| 113 | +Each step above runs serially in its own container. Since the generate step uses an s2i command to generate a Dockerfile from the source code from the git repository input, the image used for its container has s2i installed. |
| 114 | + |
| 115 | +The build and push steps both use a Buildah image to run commands to build the Dockerfile created by the generate step and then push that image to an image registry (i.e. the output of the task). |
| 116 | + |
| 117 | +You can see the images used for both these steps via the image property of each step. |
| 118 | + |
| 119 | +The order of the steps above (i.e. 1. generate 2. build 3. push) is used to specify when these steps should run. For _s2i-nodejs_, this means _generate_ will run followed by build and then the push step will execute last. |
| 120 | + |
| 121 | +Under the resources property of each step, you can define the amount of resources needed for the container in terms of CPU and memory. |
| 122 | + |
| 123 | +[source,yaml] |
| 124 | +---- |
| 125 | +resources: |
| 126 | +limits: |
| 127 | + cpu: 500m |
| 128 | + memory: 1Gi |
| 129 | +requests: |
| 130 | + cpu: 500m |
| 131 | + memory: 1Gi |
| 132 | +---- |
| 133 | + |
| 134 | +You can view the full definition of this task in the https://github.com/openshift/pipelines-catalog/blob/master/s2i-nodejs/s2i-nodejs-task.yaml[OpenShift Pipelines Catalog GitHub repository] or by using: |
| 135 | + |
| 136 | +[source,bash,role=execute] |
| 137 | +---- |
| 138 | +cat ./tektontasks/s2i-nodejs-task.yaml |
| 139 | +---- |
| 140 | + |
| 141 | +Create the _s2i-nodejs_ task that defines and builds a container image for the _nodejs-ex_ application and push the resulting image to an image registry: |
| 142 | + |
| 143 | +[source,bash,role=execute] |
| 144 | +---- |
| 145 | +oc create -f tektontasks/s2i-nodejs-task.yaml |
| 146 | +---- |
| 147 | + |
| 148 | +In the next section, you will examine the second task definitions that will be needed for our pipeline. |
0 commit comments