Managing Pipeline Parameters
1. Understanding Pipeline Parameters
Pipeline parameters in CircleCI allow you to customize the behavior of your CI/CD workflows dynamically. They provide a way to pass values into your configuration at runtime, enabling flexibility in how jobs and workflows behave based on input.
| Feature | Description | Usage |
|---|---|---|
| parameters | Defines a set of parameters that can be used throughout the pipeline. | Used in config.yml under the root level or within jobs/workflows. |
| parameter values | Values passed to the pipeline at runtime. | Can be set via UI, API, or CLI. |
Example: Parameter Usage in Config
parameters:
environment:
type: string
default: "staging"
description: "Environment to deploy to"
jobs:
build:
parameters:
environment:
type: string
steps:
- run:
command: echo "Deploying to $environment"
2. Defining Pipeline Parameters (parameters key)
Pipeline parameters are defined using the parameters key in your configuration file. This key allows you to define a set of parameters that can be referenced throughout the pipeline.
| Property | Type | Description | Required |
|---|---|---|---|
| parameters | Object | Collection of parameter definitions. | Yes |
| type | String | Specifies the type of parameter (e.g., string, boolean, number). |
Yes |
| default | Any | Default value if not provided. | No |
| description | String | Description for documentation purposes. | No |
Example: Defining Parameters
parameters:
project-name:
type: string
default: "my-app"
description: "Name of the project to build"
is-production:
type: boolean
default: false
description: "Flag indicating if this is a production deployment"
3. Using Parameter Types
CircleCI supports several parameter types that you can use to define your pipeline parameters. These types ensure that values passed to the pipeline are of the correct format.
| Type | Description | Example |
|---|---|---|
| string | A sequence of characters. | environment: "staging" |
| boolean | A true or false value. | skip-tests: true |
| number | A numeric value. | timeout: 30 |
| enum | A predefined list of values. | branch: [main, develop] |
| array | A list of values. | tags: ["v1.0", "v2.0"] |
Example: Enum Parameter
parameters:
environment:
type: enum
enum:
- development
- staging
- production
default: development
4. Setting Default Values
You can set default values for pipeline parameters to provide fallback behavior when no value is explicitly provided. These defaults are used if the parameter isn't overridden during pipeline execution.
| Property | Description | Example |
|---|---|---|
| default | Value used if no input is provided. | branch: "main" |
| required | Whether the parameter must be provided. | required: true |
Example: Default Value Usage
parameters:
branch:
type: string
default: "main"
description: "Branch to build from"
timeout:
type: number
default: 60
description: "Timeout in seconds"
5. Referencing Parameters
Parameters can be referenced in various parts of your CircleCI configuration using the <parameter-name></parameter-name> syntax. This allows you to dynamically configure jobs and workflows based on input values.
| Reference Syntax | Description | Usage |
|---|---|---|
<parameter-name></parameter-name> |
Used to reference parameter values in steps, jobs, or workflows. | environment: <environment> |
<parameters.<parameter-name>></parameter-name> |
Used to reference parameters in nested structures. | steps: [run: command: echo "Building on <parameters.branch>"] |
Example: Referencing Parameters in Steps
parameters:
environment:
type: string
default: "staging"
jobs:
deploy:
steps:
- run:
command: |
echo "Deploying to $environment"
6. Using Parameters in Workflows
Pipeline parameters can be used within workflows to customize how jobs are executed. This enables you to run the same workflow with different configurations based on parameter inputs.
| Property | Description | Usage |
|---|---|---|
| workflow parameters | Parameters defined at the workflow level. | workflows: my-workflow: parameters: environment: type: string |
| job parameters | Parameters passed to specific jobs within a workflow. | jobs: build: parameters: environment: type: string |
Example: Workflow with Parameters
parameters:
environment:
type: string
default: "staging"
workflows:
deploy-workflow:
jobs:
- build:
environment: <environment>
7. Using Parameters in Jobs
Parameters can be defined within individual jobs to allow customization of job behavior. These parameters are specific to the job and can be used to configure steps or other aspects of that job.
| Property | Description | Usage |
|---|---|---|
| job parameters | Parameters defined within a job definition. | jobs: build: parameters: environment: type: string |
| step parameters | Parameters used in steps within a job. | steps: [run: command: echo "Building on $environment"] |
Example: Job-Level Parameters
jobs:
build:
parameters:
environment:
type: string
default: "staging"
steps:
- run:
command: echo "Building on $environment"
8. Validating Parameter Values
CircleCI provides mechanisms to validate parameter values to ensure they meet specific criteria. You can define validation rules for parameters to enforce acceptable inputs.
| Validation Rule | Description | Example |
|---|---|---|
| min_length | Minimum length for string parameters. | min_length: 5 |
| max_length | Maximum length for string parameters. | max_length: 20 |
| pattern | Regular expression pattern to match. | pattern: ^[a-zA-Z]+$ |
| enum | List of allowed values. | enum: [development, staging, production] |
Example: Validating String Parameters
parameters:
project-name:
type: string
min_length: 3
max_length: 20
pattern: ^[a-zA-Z0-9_-]+$
9. Passing Parameters via API
Pipeline parameters can be passed to CircleCI using the API. This allows you to trigger pipelines with specific parameter values programmatically.
| Endpoint | Description | Method |
|---|---|---|
| POST /api/v2/project/<vcs-type>/<username>/<project>/pipeline | Trigger a new pipeline with parameters. | POST |
| parameters | JSON object containing parameter values. | {"parameters": {"environment": "production"}} |
Example: API Call to Pass Parameters
curl -X POST https://circleci.com/api/v2/project/github/owner/repo/pipeline \
-H "Circle-Token: <token>" \
-d '{"parameters": {"environment": "production"}}'
10. Debugging Parameter Issues
When working with pipeline parameters, it's important to understand how to debug issues related to parameter usage. Common problems include incorrect parameter types, missing values, or invalid syntax.