gitlab ci rule 详细用法
时间: 2023-08-07 19:05:52 浏览: 93
GitLab CI rules allow you to define when a job should be created and run based on a set of conditions. Here are some more detailed examples of how you can use GitLab CI rules in your pipeline:
1. Trigger a job only for specific branches:
```yaml
job:
script: echo "Hello, world!"
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
```
In this example, the job will be triggered only when a commit is made to the `master` branch.
2. Trigger a job only when a specific tag is pushed:
```yaml
job:
script: echo "Hello, world!"
rules:
- if: '$CI_COMMIT_TAG == "v1.0.0"'
```
In this example, the job will be triggered only when a tag called `v1.0.0` is pushed.
3. Trigger a job only for merge requests:
```yaml
job:
script: echo "Hello, world!"
rules:
- if: '$CI_MERGE_REQUEST_ID'
```
In this example, the job will be triggered only when a merge request is created or updated.
4. Trigger a job only when a specific file is modified:
```yaml
job:
script: echo "Hello, world!"
rules:
- changes:
- path/to/file
```
In this example, the job will be triggered only when changes are made to the `path/to/file` directory.
5. Trigger a job only when a specific environment variable is set:
```yaml
job:
script: echo "Hello, world!"
rules:
- if: '$ENV_VAR == "production"'
```
In this example, the job will be triggered only when the `ENV_VAR` environment variable is set to `production`.
6. Trigger a job only when a specific pipeline is triggered:
```yaml
job:
script: echo "Hello, world!"
rules:
- if: '$CI_PIPELINE_SOURCE == "schedule"'
```
In this example, the job will be triggered only when the pipeline is triggered based on a specific time interval.
These are just a few examples of how you can use GitLab CI rules in your pipeline. By using rules, you can create a highly customized and flexible pipeline that meets the specific needs of your project.
阅读全文