Edit

Share via


Run Git commands in pipeline scripts

Azure DevOps Services | Azure DevOps Server 2022 | Azure DevOps Server 2020

Git commands are available for build workflows on Microsoft-hosted and self-hosted agents. For example, after a continuous integration (CI) build completes on a feature branch, you can merge the branch to main. This article explains how to run Git commands in Azure Pipelines build scripts.

Enable scripts to run Git commands

Make sure GitHub uses your Azure DevOps account's default identity. If necessary, set the GitHub user as the first step after checkout.

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

Grant permissions to the build service

The project build service must have permissions to write to the source repository. Set the required permissions as follows:

  1. In the Project Settings for your project, select Repositories under Repos.

  2. On the All repositories page, select Security to set permissions for all repos in the project. Or, select the repository you want to run Git commands on, and then select Security on that repo's page.

    Sreenshot that shows selecting Security for repositories.

  3. On the User permissions page, select the Build Service identity. Be sure to select <project name> Build Service (<organization>) under Users, not Project Collection Build Service Accounts. By default, this identity can read from the repo but can't push any changes to it.

  4. Drop down the list and select Allow next to each permission needed for the Git commands you want to run, typically Create branch, Contribute, Read, and Create tag.

    Screenshot that shows granting the identity permissions to repositories.

Allow scripts to access the system token

To allow scripts to access the GitHub OAuth token:

Add a checkout step to your YAML pipeline with persistCredentials set to true.

steps:
- checkout: self
  persistCredentials: true

For more information about the checkout step, see the steps.checkout definition.

Clean the local repo

The build pipeline doesn't automatically clean up certain changes to the local repository, such as deleting local branches or undoing local git config changes. If you run into problems using a self-hosted agent, you can clean the repo before you run the build.

In general, for faster performance of self-hosted agents, don't clean the repo. Cleaning isn't effective for Microsoft-hosted agents, because they use a new agent each time. For more information, see Clean the local repo on the agent.

To clean the repo before you run the build:

Set clean to true in the checkout step. This option runs git clean -ffdx followed by git reset --hard HEAD before fetching.

steps:
- checkout: self
  clean: true

Select Variables in the pipeline editor, create or modify the Build.Clean variable, and set its value to source.

Git command examples

The following examples run Git commands in a Command line task and a Batch script task.

List the files in your repo

To list the files in the Git repo, use the Command line task in a YAML pipeline as follows:

- task: CmdLine@2
  inputs:
    script: 'git ls-files'

Merge a feature branch to main

The following Classic pipeline example merges a CI build to main if the build succeeds.

  1. Create a file called merge.bat at the root of your repo with the following contents:

    @echo off
    ECHO SOURCE BRANCH IS %BUILD_SOURCEBRANCH%
    IF %BUILD_SOURCEBRANCH% == refs/heads/main (
       ECHO Building main branch so no merge is needed.
       EXIT
    )
    SET sourceBranch=origin/%BUILD_SOURCEBRANCH:refs/heads/=%
    ECHO GIT CHECKOUT MAIN
    git checkout main
    ECHO GIT STATUS
    git status
    ECHO GIT MERGE
    git merge %sourceBranch% -m "Merge to main"
    ECHO GIT STATUS
    git status
    ECHO GIT PUSH
    git push origin
    ECHO GIT STATUS
    git status
    
  2. On the Triggers tab in your Classic pipeline, select the checkbox to Enable continuous integration.

  3. Under Branch filters and Path filters, select branches and paths to Include or Exclude from the build.

  4. Add a Batch script as the last task in your pipeline.

  5. Under Path in the task configuration, enter the location and name of the merge.bat file.

FAQ

Can I run Git commands if my remote repo is in GitHub or another Git service such as Bitbucket Cloud?

Yes, you can run Git commands if your remote repo is in GitHub or another Git service such as Bitbucket Cloud.

Which tasks can I use to run Git commands?

You can use the following Azure Pipelines tasks to run Git commands:

How can I avoid triggering a CI build when the script pushes?

To avoid triggering a CI build when the script pushes, add [skip ci] to your commit message or description. For example:

  • git commit -m "This is a commit message [skip ci]"
  • git merge origin/features/hello-world -m "Merge to main [skip ci]"

You can also use any of the following variations for commits to Azure Repos Git, Bitbucket Cloud, GitHub, or GitHub Enterprise Server:

  • [skip ci] or [ci skip]
  • skip-checks: true or skip-checks:true
  • [skip azurepipelines] or [azurepipelines skip]
  • [skip azpipelines] or [azpipelines skip]
  • [skip azp] or [azp skip]
  • ***NO_CI***

Do I need an agent to run pipelines?

Yes, you need at least one agent to run your build or release pipeline.

How can I troubleshoot problems?

See Troubleshoot pipeline runs.

How can I fix not being able to select a default agent pool or queue my pipeline run?

See Create and manage agent pools.

How can I fix my NuGet push task failing with "Error: unable to get local issuer certificate"?

You can fix this issue by adding a trusted root certificate. Either add the NODE_EXTRA_CA_CERTS=file environment variable to your build agent, or add the NODE.EXTRA.CA.CERTS=file task variable in your pipeline.

For more information about this variable, see NODE_EXTRA_CA_CERTS=file in the Node.js documentation. For instructions on setting a variable in your pipeline, see Set variables in a pipeline.

Why don't I see some of these features in my on-premises Azure DevOps Server?

Some of these features are available only on Azure DevOps Services and not available for on-premises Azure DevOps Server. Some features are available only in the latest version of Azure DevOps Server.