Commit 665ce5e1 authored by Marcel Amirault's avatar Marcel Amirault Committed by Amy Qualls

Update CI/CD examples to use rules

parent 7e5275f9
...@@ -99,8 +99,8 @@ build: ...@@ -99,8 +99,8 @@ build:
KANIKOCFG="${KANIKOCFG} }" KANIKOCFG="${KANIKOCFG} }"
echo "${KANIKOCFG}" > /kaniko/.docker/config.json echo "${KANIKOCFG}" > /kaniko/.docker/config.json
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile $KANIKOPROXYBUILDARGS --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile $KANIKOPROXYBUILDARGS --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
only: rules:
- tags - if: $CI_COMMIT_TAG
``` ```
## Using a registry with a custom certificate ## Using a registry with a custom certificate
......
...@@ -99,10 +99,10 @@ deploy_review: ...@@ -99,10 +99,10 @@ deploy_review:
environment: environment:
name: review/$CI_COMMIT_REF_NAME name: review/$CI_COMMIT_REF_NAME
url: https://$CI_ENVIRONMENT_SLUG.example.com url: https://$CI_ENVIRONMENT_SLUG.example.com
only: rules:
- branches - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
except: when: never
- master - if: $CI_COMMIT_BRANCH
``` ```
In this example: In this example:
...@@ -158,8 +158,8 @@ deploy_prod: ...@@ -158,8 +158,8 @@ deploy_prod:
name: production name: production
url: https://example.com url: https://example.com
when: manual when: manual
only: rules:
- master - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
``` ```
The `when: manual` action: The `when: manual` action:
...@@ -200,8 +200,8 @@ deploy: ...@@ -200,8 +200,8 @@ deploy:
url: https://example.com url: https://example.com
kubernetes: kubernetes:
namespace: production namespace: production
only: rules:
- master - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
``` ```
When you use the GitLab Kubernetes integration to deploy to a Kubernetes cluster, When you use the GitLab Kubernetes integration to deploy to a Kubernetes cluster,
......
...@@ -349,12 +349,14 @@ variable entry. ...@@ -349,12 +349,14 @@ variable entry.
GitLab does support a [`when` keyword](../yaml/README.md#when) which is used to indicate when a job should be GitLab does support a [`when` keyword](../yaml/README.md#when) which is used to indicate when a job should be
run in case of (or despite) failure, but most of the logic for controlling pipelines can be found in run in case of (or despite) failure, but most of the logic for controlling pipelines can be found in
our very powerful [`only/except` rules system](../yaml/README.md#only--except) our very powerful [`rules` system](../yaml/README.md#rules):
(see also our [advanced syntax](../yaml/README.md#only--except)):
```yaml ```yaml
my_job: my_job:
only: [branches] script:
- echo
rules:
- if: $CI_COMMIT_BRANCH
``` ```
## Additional resources ## Additional resources
......
...@@ -38,7 +38,7 @@ set of concurrently running child pipelines, but within the same project: ...@@ -38,7 +38,7 @@ set of concurrently running child pipelines, but within the same project:
Child pipelines work well with other GitLab CI/CD features: Child pipelines work well with other GitLab CI/CD features:
- Use [`only: changes`](yaml/README.md#onlychanges--exceptchanges) to trigger pipelines only when - Use [`rules: changes`](yaml/README.md#ruleschanges) to trigger pipelines only when
certain files change. This is useful for monorepos, for example. certain files change. This is useful for monorepos, for example.
- Since the parent pipeline in `.gitlab-ci.yml` and the child pipeline run as normal - Since the parent pipeline in `.gitlab-ci.yml` and the child pipeline run as normal
pipelines, they can have their own behaviors and sequencing in relation to triggers. pipelines, they can have their own behaviors and sequencing in relation to triggers.
......
...@@ -8,9 +8,6 @@ type: reference, howto ...@@ -8,9 +8,6 @@ type: reference, howto
# Pipeline schedules **(FREE)** # Pipeline schedules **(FREE)**
> - Introduced in GitLab 9.1 as [Trigger Schedule](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/10533).
> - [Renamed to Pipeline Schedule](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/10853) in GitLab 9.2.
Pipelines are normally run based on certain conditions being met. For example, when a branch is pushed to repository. Pipelines are normally run based on certain conditions being met. For example, when a branch is pushed to repository.
Pipeline schedules can be used to also run [pipelines](index.md) at specific intervals. For example: Pipeline schedules can be used to also run [pipelines](index.md) at specific intervals. For example:
...@@ -54,31 +51,29 @@ is installed on. ...@@ -54,31 +51,29 @@ is installed on.
### Using variables ### Using variables
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/12328) in GitLab 9.4.
You can pass any number of arbitrary variables. They are available in You can pass any number of arbitrary variables. They are available in
GitLab CI/CD so that they can be used in your [`.gitlab-ci.yml` file](../../ci/yaml/README.md). GitLab CI/CD so that they can be used in your [`.gitlab-ci.yml` file](../../ci/yaml/README.md).
![Scheduled pipeline variables](img/pipeline_schedule_variables.png) ![Scheduled pipeline variables](img/pipeline_schedule_variables.png)
### Using only and except ### Using `rules`
To configure a job to be executed only when the pipeline has been To configure a job to be executed only when the pipeline has been
scheduled (or the opposite), use scheduled, use the [`rules`](../yaml/README.md#rules) keyword.
[only and except](../yaml/README.md#only--except) configuration keywords.
In the example below `make world` runs in scheduled pipelines, and `make build` runs in pipelines that are not scheduled: In this example, `make world` runs in scheduled pipelines, and `make build`
runs in branch and tag pipelines:
```yaml ```yaml
job:on-schedule: job:on-schedule:
only: rules:
- schedules - if: $CI_PIPELINE_SOURCE == "schedule"
script: script:
- make world - make world
job: job:
except: rules:
- schedules - if: $CI_PIPELINE_SOURCE = "push"
script: script:
- make build - make build
``` ```
......
...@@ -57,8 +57,8 @@ trigger_pipeline: ...@@ -57,8 +57,8 @@ trigger_pipeline:
stage: deploy stage: deploy
script: script:
- curl --request POST --form "token=$CI_JOB_TOKEN" --form ref=main "https://gitlab.example.com/api/v4/projects/9/trigger/pipeline" - curl --request POST --form "token=$CI_JOB_TOKEN" --form ref=main "https://gitlab.example.com/api/v4/projects/9/trigger/pipeline"
only: rules:
- tags - if: $CI_COMMIT_TAG
``` ```
Pipelines triggered that way also expose a special variable: Pipelines triggered that way also expose a special variable:
...@@ -83,8 +83,8 @@ build_submodule: ...@@ -83,8 +83,8 @@ build_submodule:
- apt update && apt install -y unzip - apt update && apt install -y unzip
- curl --location --output artifacts.zip "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/main/download?job=test&job_token=$CI_JOB_TOKEN" - curl --location --output artifacts.zip "https://gitlab.example.com/api/v4/projects/1/jobs/artifacts/main/download?job=test&job_token=$CI_JOB_TOKEN"
- unzip artifacts.zip - unzip artifacts.zip
only: rules:
- tags - if: $CI_COMMIT_TAG
``` ```
This allows you to use that for multi-project pipelines and download artifacts This allows you to use that for multi-project pipelines and download artifacts
...@@ -163,8 +163,8 @@ trigger_pipeline: ...@@ -163,8 +163,8 @@ trigger_pipeline:
stage: deploy stage: deploy
script: script:
- 'curl --request POST --form token=TOKEN --form ref=main "https://gitlab.example.com/api/v4/projects/9/trigger/pipeline"' - 'curl --request POST --form token=TOKEN --form ref=main "https://gitlab.example.com/api/v4/projects/9/trigger/pipeline"'
only: rules:
- tags - if: $CI_COMMIT_TAG
``` ```
This means that whenever a new tag is pushed on project A, the job runs and the This means that whenever a new tag is pushed on project A, the job runs and the
......
...@@ -193,7 +193,6 @@ my-job: ...@@ -193,7 +193,6 @@ my-job:
name: review/$CI_JOB_STAGE/deploy name: review/$CI_JOB_STAGE/deploy
script: script:
- 'deploy staging' - 'deploy staging'
only: rules:
variables: - if: $STAGING_SECRET == 'something'
- $STAGING_SECRET == 'something'
``` ```
...@@ -991,8 +991,8 @@ but you can use as many as eleven. The following example has two levels of inher ...@@ -991,8 +991,8 @@ but you can use as many as eleven. The following example has two levels of inher
```yaml ```yaml
.tests: .tests:
only: rules:
- pushes - if: $CI_PIPELINE_SOURCE == "push"
.rspec: .rspec:
extends: .tests extends: .tests
...@@ -1028,9 +1028,9 @@ levels. For example: ...@@ -1028,9 +1028,9 @@ levels. For example:
variables: variables:
URL: "http://my-url.internal" URL: "http://my-url.internal"
IMPORTANT_VAR: "the details" IMPORTANT_VAR: "the details"
only: rules:
- main - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- stable - if: $CI_COMMIT_BRANCH == "stable"
tags: tags:
- production - production
script: script:
...@@ -1061,9 +1061,9 @@ rspec: ...@@ -1061,9 +1061,9 @@ rspec:
URL: "http://docker-url.internal" URL: "http://docker-url.internal"
IMPORTANT_VAR: "the details" IMPORTANT_VAR: "the details"
GITLAB: "is-awesome" GITLAB: "is-awesome"
only: rules:
- main - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- stable - if: $CI_COMMIT_BRANCH == "stable"
tags: tags:
- docker - docker
image: alpine image: alpine
...@@ -2333,8 +2333,8 @@ To protect a manual job: ...@@ -2333,8 +2333,8 @@ To protect a manual job:
name: production name: production
url: https://example.com url: https://example.com
when: manual when: manual
only: rules:
- main - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
``` ```
1. In the [protected environments settings](../environments/protected_environments.md#protecting-environments), 1. In the [protected environments settings](../environments/protected_environments.md#protecting-environments),
...@@ -3281,8 +3281,8 @@ Create artifacts only for tags (`default-job` doesn't create artifacts): ...@@ -3281,8 +3281,8 @@ Create artifacts only for tags (`default-job` doesn't create artifacts):
default-job: default-job:
script: script:
- mvn test -U - mvn test -U
except: rules:
- tags - if: $CI_COMMIT_BRANCH
release-job: release-job:
script: script:
...@@ -3290,8 +3290,8 @@ release-job: ...@@ -3290,8 +3290,8 @@ release-job:
artifacts: artifacts:
paths: paths:
- target/*.war - target/*.war
only: rules:
- tags - if: $CI_COMMIT_TAG
``` ```
You can use wildcards for directories too. For example, if you want to get all the files inside the directories that end with `xyz`: You can use wildcards for directories too. For example, if you want to get all the files inside the directories that end with `xyz`:
...@@ -4370,7 +4370,10 @@ job: ...@@ -4370,7 +4370,10 @@ job:
description: 'Release description' description: 'Release description'
``` ```
It is also possible to create any unique tag, in which case `only: tags` is not mandatory. It is also possible for the release job to automatically create a new unique tag. In that case,
do not use [`rules`](#rules) or [`only`](#only--except) to configure the job to
only run for tags.
A semantic versioning example: A semantic versioning example:
```yaml ```yaml
...@@ -4626,8 +4629,8 @@ pages: ...@@ -4626,8 +4629,8 @@ pages:
artifacts: artifacts:
paths: paths:
- public - public
only: rules:
- main - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
``` ```
View the [GitLab Pages user documentation](../../user/project/pages/index.md). View the [GitLab Pages user documentation](../../user/project/pages/index.md).
......
...@@ -111,8 +111,8 @@ production: ...@@ -111,8 +111,8 @@ production:
environment: environment:
name: production name: production
url: https://$CI_PROJECT_PATH_SLUG.$KUBE_INGRESS_BASE_DOMAIN url: https://$CI_PROJECT_PATH_SLUG.$KUBE_INGRESS_BASE_DOMAIN
only: rules:
- main - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
``` ```
Content of `.gitlab-ci.yml`: Content of `.gitlab-ci.yml`:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment