File: examples.md

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (433 lines) | stat: -rw-r--r-- 14,804 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
---
stage: Verify
group: Pipeline Authoring
info: This page is maintained by Developer Relations, author @dnsmichi, see https://handbook.gitlab.com/handbook/marketing/developer-relations/developer-advocacy/content/#maintained-documentation
---

# CI/CD component examples

DETAILS:
**Tier:** Free, Premium, Ultimate
**Offering:** GitLab.com, Self-managed, GitLab Dedicated

## Test a component

Depending on a component's functionality, [testing the component](index.md#test-the-component) might require additional files in the repository.
For example, a component which lints, builds, and tests software in a specific programming language requires actual source code samples.
You can have source code examples, configuration files, and similar in the same repository.

For example, the Code Quality CI/CD component's has several [code samples for testing](https://gitlab.com/components/code-quality/-/tree/main/src).

### Example: Test a Rust language CI/CD component

Depending on a component's functionality, [testing the component](index.md#test-the-component) might require additional files in the repository.

The following "hello world" example for the Rust programming language uses the `cargo` tool chain for simplicity:

1. Go to the CI/CD component root directory.
1. Initialize a new Rust project by using the `cargo init` command.

   ```shell
   cargo init
   ```

   The command creates all required project files, including a `src/main.rs` "hello world" example.
   This step is sufficient to build the Rust source code in a component job with `cargo build`.

   ```plaintext
   tree
   .
   ├── Cargo.toml
   ├── LICENSE.md
   ├── README.md
   ├── src
   │   └── main.rs
   └── templates
       └── build.yml
   ```

1. Ensure that the component has a job to build the Rust source code, for example,
   in `templates/build.yml`:

   ```yaml
   spec:
     inputs:
       stage:
         default: build
         description: 'Defines the build stage'
       rust_version:
         default: latest
         description: 'Specify the Rust version, use values from https://hub.docker.com/_/rust/tags Defaults to latest'
   ---

   "build-$[[ inputs.rust_version ]]":
     stage: $[[ inputs.stage ]]
     image: rust:$[[ inputs.rust_version ]]
     script:
       - cargo build --verbose
   ```

   In this example:

   - The `stage` and `rust_version` inputs can be modified from their default values.
     The CI/CD job starts with a `build-` prefix and dynamically creates the name based on the `rust_version` input.
     The command `cargo build --verbose` compiles the Rust source code.

1. Test the component's `build` template in the project's `.gitlab-ci.yml` configuration file:

   ```yaml
   include:
     # include the component located in the current project from the current SHA
     - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/build@$CI_COMMIT_SHA
       inputs:
         stage: build

   stages: [build, test, release]
   ```

1. For running tests and more, add additional functions and tests into the Rust code,
   and add a component template and job running `cargo test` in `templates/test.yml`.

   ```yaml
   spec:
     inputs:
       stage:
         default: test
         description: 'Defines the test stage'
       rust_version:
         default: latest
         description: 'Specify the Rust version, use values from https://hub.docker.com/_/rust/tags Defaults to latest'
   ---

   "test-$[[ inputs.rust_version ]]":
     stage: $[[ inputs.stage ]]
     image: rust:$[[ inputs.rust_version ]]
     script:
       - cargo test --verbose
   ```

1. Test the additional job in the pipeline by including the `test` component template:

   ```yaml
   include:
     # include the component located in the current project from the current SHA
     - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/build@$CI_COMMIT_SHA
       inputs:
         stage: build
     - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/test@$CI_COMMIT_SHA
       inputs:
         stage: test

   stages: [build, test, release]
   ```

## CI/CD component patterns

This section provides practical examples of implementing common patterns in CI/CD components.

### Use boolean inputs to conditionally configure jobs

You can compose jobs with two conditionals by combining `boolean` type inputs and
[`extends`](../../ci/yaml/index.md#extends) functionality.

For example, to configure complex caching behavior with a `boolean` input:

```yaml
spec:
  inputs:
    enable_special_caching:
      description: 'If set to `true` configures a complex caching behavior'
      type: boolean
---

.my-component:enable_special_caching:false:
  extends: null

.my-component:enable_special_caching:true:
  cache:
    policy: pull-push
    key: $CI_COMMIT_SHA
    paths: [...]

my-job:
  extends: '.my-component:enable_special_caching:$[[ inputs.enable_special_caching ]]'
  script: ... # run some fancy tooling
```

This pattern works by passing the `enable_special_caching` input into
the `extends` keyword of the job.
Depending on whether `enable_special_caching` is `true` or `false`,
the appropriate configuration is selected from the predefined hidden jobs
(`.my-component:enable_special_caching:true` or `.my-component:enable_special_caching:false`).

### Use `options` to conditionally configure jobs

You can compose jobs with multiple options, for behavior similar to `if` and `elseif`
conditionals. Use the [`extends`](../../ci/yaml/index.md#extends) with `string` type
and multiple `options` for any number of conditions.

For example, to configure complex caching behavior with 3 different options:

```yaml
spec:
  inputs:
    cache_mode:
      description: Defines the caching mode to use for this component
      type: string
      options:
        - default
        - aggressive
        - relaxed
---

.my-component:enable_special_caching:false:
  extends: null

.my-component:cache_mode:aggressive:
  cache:
    policy: push
    key: $CI_COMMIT_SHA
    paths: ['*/**']

.my-component:cache_mode:relaxed:
  cache:
    policy: pull-push
    key: $CI_COMMIT_BRANCH
    paths: ['bin/*']

my-job:
  extends: '.my-component:cache_mode:$[[ inputs.cache_mode ]]'
  script: ... # run some fancy tooling
```

In this example, `cache_mode` input offers `default`, `aggressive`, and `relaxed` options,
each corresponding to a different hidden job.
By extending the component job with `extends: '.my-component:cache_mode:$[[ inputs.cache_mode ]]'`,
the job dynamically inherits the correct caching configuration based on the selected option.

## CI/CD component migration examples

This section shows practical examples of migrating CI/CD templates and pipeline configuration
into reusable CI/CD components.

### CI/CD component migration example: Go

A complete pipeline for the software development lifecycle can be composed with multiple jobs and stages.
CI/CD templates for programming languages may provide multiple jobs in a single template file.
As a practice, the following Go CI/CD template should be migrated.

```yaml
image: golang:latest

stages:
  - test
  - build
  - deploy

format:
  stage: test
  script:
    - go fmt $(go list ./... | grep -v /vendor/)
    - go vet $(go list ./... | grep -v /vendor/)
    - go test -race $(go list ./... | grep -v /vendor/)

compile:
  stage: build
  script:
    - mkdir -p mybinaries
    - go build -o mybinaries ./...
  artifacts:
    paths:
      - mybinaries
```

NOTE:
You can also start with migrating one job, instead of all jobs. Follow the instructions below,
and only migrate the `build` CI/CD job in the first iteration.

The CI/CD template migration involves the following steps:

1. Analyze the CI/CD jobs and dependencies, and define migration actions:
   - The `image` configuration is global, [needs to be moved into the job definitions](index.md#avoid-using-global-keywords).
   - The `format` job runs multiple `go` commands in one job. The `go test` command should be moved
     into a separate job to increase pipeline efficiency.
   - The `compile` job runs `go build` and should be renamed to `build`.
1. Define optimization strategies for better pipeline efficiency.
   - The `stage` job attribute should be configurable to allow different CI/CD pipeline consumers.
   - The `image` key uses a hardcoded image tag `latest`. Add [`golang_version` as input](../yaml/inputs.md)
     with `latest` as default value for more flexible and reusable pipelines. The input must match
     the Docker Hub image tag values.
   - The `compile` job builds the binaries into a hard-coded target directory `mybinaries`,
     which can be enhanced with a dynamic [input](../yaml/inputs.md) and default value `mybinaries`.
1. Create a template [directory structure](index.md#directory-structure) for the new component,
   based on one template for each job.

   - The name of the template should follow the `go` command, for example `format.yml`, `build.yml`, and `test.yml`.
   - Create a new project, initialize a Git repository, add/commit all changes, set a remote origin and push.
     Modify the URL for your CI/CD component project path.
   - Create additional files as outlined in the guidance to [write a component](index.md#write-a-component):
     `README.md`, `LICENSE.md`, `.gitlab-ci.yml`, `.gitignore`. The following shell commands
     initialize the Go component structure:

   ```shell
   git init

   mkdir templates
   touch templates/{format,build,test}.yml

   touch README.md LICENSE.md .gitlab-ci.yml .gitignore

   git add -A
   git commit -avm "Initial component structure"

   git remote add origin https://gitlab.example.com/components/golang.git

   git push
   ```

1. Create the CI/CD jobs as template. Start with the `build` job.
   - Define the following inputs in the `spec` section: `stage`, `golang_version` and `binary_directory`.
   - Add a dynamic job name definition, accessing `inputs.golang_version`.
   - Use the similar pattern for dynamic Go image versions, accessing `inputs.golang_version`.
   - Assign the stage to the `inputs.stage` value.
   - Create the binary director from `inputs.binary_directory` and add it as parameter to `go build`.
   - Define the artifacts path to `inputs.binary_directory`.

     ```yaml
     spec:
       inputs:
         stage:
           default: 'build'
           description: 'Defines the build stage'
         golang_version:
           default: 'latest'
           description: 'Go image version tag'
         binary_directory:
           default: 'mybinaries'
           description: 'Output directory for created binary artifacts'
     ---

     "build-$[[ inputs.golang_version ]]":
       image: golang:$[[ inputs.golang_version ]]
       stage: $[[ inputs.stage ]]
       script:
         - mkdir -p $[[ inputs.binary_directory ]]
         - go build -o $[[ inputs.binary_directory ]] ./...
       artifacts:
         paths:
           - $[[ inputs.binary_directory ]]
     ```

   - The `format` job template follows the same patterns, but only requires the `stage` and `golang_version` inputs.

     ```yaml
     spec:
       inputs:
         stage:
           default: 'format'
           description: 'Defines the format stage'
         golang_version:
           default: 'latest'
           description: 'Golang image version tag'
     ---

     "format-$[[ inputs.golang_version ]]":
       image: golang:$[[ inputs.golang_version ]]
       stage: $[[ inputs.stage ]]
       script:
         - go fmt $(go list ./... | grep -v /vendor/)
         - go vet $(go list ./... | grep -v /vendor/)
     ```

   - The `test` job template follows the same patterns, but only requires the `stage` and `golang_version` inputs.

     ```yaml
     spec:
       inputs:
         stage:
           default: 'test'
           description: 'Defines the format stage'
         golang_version:
           default: 'latest'
           description: 'Golang image version tag'
     ---

     "test-$[[ inputs.golang_version ]]":
       image: golang:$[[ inputs.golang_version ]]
       stage: $[[ inputs.stage ]]
       script:
         - go test -race $(go list ./... | grep -v /vendor/)
     ```

1. In order to test the component, modify the `.gitlab-ci.yml` configuration file,
   and add [tests](index.md#test-the-component).

   - Specify a different value for `golang_version` as input for the `build` job.
   - Modify the URL for your CI/CD component path.

     ```yaml
     stages: [format, build, test]

     include:
       - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/format@$CI_COMMIT_SHA
       - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/build@$CI_COMMIT_SHA
       - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/build@$CI_COMMIT_SHA
         inputs:
           golang_version: "1.21"
       - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/test@$CI_COMMIT_SHA
         inputs:
           golang_version: latest
     ```

1. Add Go source code to test the CI/CD component. The `go` commands expect a Go project
   with `go.mod` and `main.go` in the root directory.

   - Initialize the Go modules. Modify the URL for your CI/CD component path.

     ```shell
     go mod init example.gitlab.com/components/golang
     ```

   - Create a `main.go` file with a main function, printing `Hello, CI/CD component` for example.
     Tip: Use code comments to generate Go code using [GitLab Duo Code Suggestions](../../user/project/repository/code_suggestions/index.md).

     ```go
     // Specify the package, import required packages
     // Create a main function
     // Inside the main function, print "Hello, CI/CD Component"

     package main

     import "fmt"

     func main() {
       fmt.Println("Hello, CI/CD Component")
     }
     ```

   - The directory tree should look as follows:

     ```plaintext
     tree
     .
     ├── LICENSE.md
     ├── README.md
     ├── go.mod
     ├── main.go
     └── templates
         ├── build.yml
         ├── format.yml
         └── test.yml
     ```

Follow the remaining steps in the [converting a CI/CD template into a component](index.md#convert-a-cicd-template-to-a-component)
section to complete the migration:

1. Commit and push the changes, and verify the CI/CD pipeline results.
1. Follow the guidance on [writing a component](index.md#write-a-component) to update the `README.md` and `LICENSE.md` files.
1. [Release the component](index.md#publish-a-new-release) and verify it in the CI/CD catalog.
1. Add the CI/CD component into your staging/production environment.

The [GitLab-maintained Go component](https://gitlab.com/components/golang) provides an example
for a successful migration from a Go CI/CD template, enhanced with inputs and component best practices.
You can inspect the Git history to learn more.