> For the complete documentation index, see [llms.txt](https://jadelab.gitbook.io/jadegit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jadelab.gitbook.io/jadegit/0.18.0/ci-cd/pipeline.md).

# Pipeline

The pipeline templates described in this section illustrate one approach to orchestrating automated testing and deployment. More advanced implementations may be needed to support complex requirements or environments, but the patterns described here provide a starting point that can be adapted as needed.

## Stages

A pipeline run may consist of two stages — testing and deployment. Testing runs first, and deployment follows on success. The deployment stage applies changes to the appropriate environment based on the trigger.

For `v*` tags, deployment proceeds in sequence — Staging first, followed by Production only if Staging succeeds. This ensures that what is tested in Staging is identical to what will be deployed to Production.

## Triggers

The following triggers are used, each mapping directly to the branching strategy:

| Trigger         | Target Environment       |
| --------------- | ------------------------ |
| `main`          | Test                     |
| `release/*`     | Preview                  |
| `environment/*` | The named environment    |
| `v*`            | Staging, then Production |

## Example

The following examples are for Azure DevOps. The pipeline templates are added to the shared admin repository under a `ci` folder, alongside those used to [prepare database images](https://jadelab.gitbook.io/jadegit/admin/database-images).

```
├── ci/
│   ├── deploy.yml
│   ├── pipeline.yml
│   ├── test.yml
│   └── variables.yml
├── images/
│   └── ...
├── database/
│   └── ...
```

The `pipeline.yml` template defines the full pipeline, accepting a list of databases as a parameter. Each database is specified by its canonical name, which uniquely identifies it as a resource registered within the context of an environment in ADO. While one database per repository is most common, multiple databases are supported for scenarios where code is shared, or where two or more variants of the same database exist within each environment for architectural reasons, like load balancing.

```yaml
parameters:
- name: databases
  type: object

variables:
- template: variables.yml

stages:
- stage: tests
  displayName: "Run Tests"
  jobs:
  - job: test_current
    displayName: "Current Platform"
    steps:
    - template: test.yml
      parameters:
        database: ${{ parameters.databases[0] }}
        platform:
          version: $(jade.version.current)
          charset: $(jade.charset)

  - job: test_latest
    displayName: "Latest Platform"
    condition: and(succeeded(), ne(variables['jade.version.latest'], variables['jade.version.current']))
    steps:
    - template: test.yml
      parameters:
        database: ${{ parameters.databases[0] }}
        platform:
          version: $(jade.version.latest)
          charset: $(jade.charset)

- ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/environment/') }}:
  - stage: deploy_environment
    displayName: "Deploy - ${{ replace(variables['Build.SourceBranch'], 'refs/heads/environment/', '') }}"
    dependsOn: tests
    jobs:
    - template: deploy.yml
      parameters:
        environment: ${{ replace(variables['Build.SourceBranch'], 'refs/heads/environment/', '') }}
        databases: ${{ parameters.databases }}

- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
  - stage: deploy_test
    displayName: "Deploy - Test"
    dependsOn: tests
    jobs:
    - template: deploy.yml
      parameters:
        environment: Test
        databases: ${{ parameters.databases }}

- ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/release/') }}:
  - stage: deploy_preview
    displayName: "Deploy - Preview"
    dependsOn: tests
    jobs:
    - template: deploy.yml
      parameters:
        environment: Preview
        databases: ${{ parameters.databases }}

- ${{ if startsWith(variables['Build.SourceBranch'], 'refs/tags/v') }}:
  - stage: deploy_staging
    displayName: "Deploy - Staging"
    dependsOn: tests
    jobs:
    - template: deploy.yml
      parameters:
        environment: Staging
        databases: ${{ parameters.databases }}

  - stage: deploy_production
    displayName: "Deploy - Production"
    dependsOn: deploy_staging
    jobs:
    - template: deploy.yml
      parameters:
        environment: Production
        databases: ${{ parameters.databases }}
```

Each code repository defines a minimal pipeline definition that extends the shared template, specifying its associated databases to be deployed to within each environment:

```yaml
trigger:
  batch: true
  branches:
    include:
    - main
    - release/*
    - environment/*
  tags:
    include:
    - v*

resources:
  repositories:
  - repository: admin
    type: git
    name: Jade.Admin

extends:
  template: ci/pipeline.yml@admin
  parameters:
    databases:
    - delorean
```
