touchifyapp / vsts-changed-files

Azure DevOps pipeline task to get changed files since last succeeded build
MIT License
12 stars 6 forks source link

Azure DevOps Task: Changed Files

CI

Pipeline task to get changed files and apply conditions according to those changes.

Installation

Installation can be done using Visual Studio MarketPlace.

Source Code

Source code can be found on Github.

Simple usage

jobs: 
  - job: check
    displayName: Check changed files
    pool:
        vmImage: ubuntu-latest
    steps:
      - task: ChangedFiles@1
        name: CheckChanges
        inputs:
          rules: src/**/*.ts
          variable: HasChanged

  - job: build
    displayName: Build only when code changes
    dependsOn: check
    condition: eq(dependencies.check.outputs['CheckChanges.HasChanged'], 'true')
    steps:
        - # Add your build steps here

Multi-variable usage

jobs: 
  - job: check
    displayName: Check changed files
    pool:
        vmImage: ubuntu-latest
    steps:
      - task: ChangedFiles@1
        name: CheckChanges
        inputs:
          rules: |
            [CodeChanged]
            src/**/*.ts
            src/**/*.html

            [TestsChanged]
            tests/**/*.ts

  - job: build
    displayName: Build only when code changes
    dependsOn: check
    condition: eq(dependencies.check.outputs['CheckChanges.CodeChanged'], 'true')
    steps:
        - # Add your build steps here

  - job: tests
    displayName: Tests only when code changes or tests changes
    dependsOn: check
    condition: or(eq(dependencies.check.outputs['CheckChanges.CodeChanged'], 'true'), eq(dependencies.check.outputs['CheckChanges.TestsChanged'], 'true'))
    steps:
        - # Add your build steps here

Multi-branch usage

jobs: 
  - job: check
    displayName: Check changed files
    pool:
        vmImage: ubuntu-latest
    steps:
      - task: ChangedFiles@1
        name: CheckChanges
        inputs:
          refBranch: master
          rules: |
            [CodeChanged]
            src/**/*.ts
            src/**/*.html

            [TestsChanged]
            tests/**/*.ts

  - job: build
    displayName: Build only when code changes
    dependsOn: check
    condition: eq(dependencies.check.outputs['CheckChanges.CodeChanged'], 'true')
    steps:
        - # Add your build steps here

  - job: tests
    displayName: Tests only when code changes or tests changes
    dependsOn: check
    condition: or(eq(dependencies.check.outputs['CheckChanges.CodeChanged'], 'true'), eq(dependencies.check.outputs['CheckChanges.TestsChanged'], 'true'))
    steps:
        - # Add your build steps here

Multi-stage usage

stages:
  - stage: pre
    jobs:
      - job: check
        displayName: Check changed files
        pool:
          vmImage: ubuntu-latest
        steps:
          - task: ChangedFiles@1
            name: CheckChanges
            inputs:
              refBranch: main 
              rules: |
                [BarChanged]
                bar/**

                [FooChanged]
                foo/**

  - stage: bar_has_changed
    dependsOn: ["pre"]
    displayName: This stage runs only when the `BarChanged` variable is true
    condition: eq(dependencies.pre.outputs['check.CheckChanges.BarChanged'], 'true')
    jobs: 
      - job: run
        steps:
           - # Add your build steps here

  - stage: foo_has_changed
    dependsOn: ["pre"]
    displayName: This stage runs only when the `FooChanged` variable is true
    condition: eq(dependencies.pre.outputs['check.CheckChanges.FooChanged'], 'true')
    jobs: 
      - job: run
        steps:
           - # Add your build steps here

  - stage: stage_with_conditional_job
    dependsOn: ["pre"]
    displayName: The stage always runs but contains a job that runs only when `FooChanged` is true
    jobs: 
      - job: job_when_foo_has_changed
        displayName: This job runs only when `FooChanged` is true
        condition: eq(stageDependencies.pre.check.outputs['CheckChanges.FooChanged'], 'true')
        steps:
          - # Add your build steps here

Options

Troubleshooting

If you encounter the error: fatal: ambiguous argument 'origin/{branch}...': unknown revision or path not in the working tree.

It means that you should fetch the full depth of your git history to be sure to include all necessaries artifacts:

jobs: 
  - job: check
    displayName: Check changed files
    pool:
        vmImage: ubuntu-latest
    steps:
      - checkout: self
        fetchDepth: "0"

      - task: ChangedFiles@1
        name: CheckChanges
        inputs:
          rules: src/**/*.ts
          variable: HasChanged

License

MIT

Git tested changes