共用方式為


Create your first pipeline

TFS 2018

Note

Microsoft Visual Studio Team Foundation Server 2018 and earlier versions have the following differences in naming:

  • Pipelines for build and release are called definitions
  • Runs are called builds
  • Service connections are called service endpoints
  • Stages are called environments
  • Jobs are called phases

We'll show you how to use the classic editor in TFS to create a build and a release that prints "Hello world".

Prerequisites

Initialize your repository

If you already have a repository in your project, you can skip to the next step: Skip to adding a script to your repo

  1. Navigate to your repository by clicking Code in the top navigation.

  2. If your project is empty, you will be greeted with a screen to help you add code to your repository. Choose the bottom choice to initialize your repo with a readme file:

    Initialize repository

Add a script to your repository

Create a PowerShell script that prints Hello world.

  1. Go to the Code hub.

  2. Add a file.


  1. In the dialog box, name your new file and create it.

    HelloWorld.ps1
    
  2. Copy and paste this script.

    Write-Host "Hello world"
    
  3. Commit (save) the file.

In this tutorial, our focus is on CI/CD, so we're keeping the code part simple. We're working in an Azure Repos Git repository directly in your web browser.

When you're ready to begin building and deploying a real app, you can use a wide range of version control clients and services with Azure Pipelines CI builds. Learn more.

Create a build pipeline

Create a build pipeline that prints "Hello world."

  1. Select Build and Release, and then choose Builds.

    Choose build tab

  2. Create a new pipeline.

    Create a new pipeline

  3. Start with an empty pipeline

  4. Select Pipeline and specify whatever Name you want to use. For the Agent pool, select Default.

  5. On the left side, select + Add Task to add a task to the job, and then on the right side select the Utility category, select the PowerShell task, and then choose Add.

    Add the task to the job

  6. On the left side, select your new PowerShell script task.

  7. For the Script Path argument, select the button to browse your repository and select the script you created.

    Select the PowerShell task

  8. Select Save & queue, and then select Save.

A build pipeline is the entity through which you define your automated build pipeline. In the build pipeline, you compose a set of tasks, each of which perform a step in your build. The task catalog provides a rich set of tasks for you to get started. You can also add PowerShell or shell scripts to your build pipeline.

Publish an artifact from your build

A typical build produces an artifact that can then be deployed to various stages in a release. Here to demonstrate the capability in a simple way, we'll simply publish the script as the artifact.

  1. On the Tasks tab, select Add Task.

  2. Select the Utility category, select the Publish Build Artifacts task, and then select Add.

    Select add to add the publish artifact task

    Path to Publish: Select the button to browse and select the script you created.

    Artifact Name: Enter drop.

    Artifact Type: Select Server.

Artifacts are the files that you want your build to produce. Artifacts can be nearly anything your team needs to test or deploy your app. For example, you've got a .DLL and .EXE executable files and .PDB symbols file of a C# or C++ .NET Windows app.

To enable you to produce artifacts, we provide tools such as copying with pattern matching, and a staging directory in which you can gather your artifacts before publishing them. See Artifacts in Azure Pipelines.

Enable continuous integration (CI)

  1. Select the Triggers tab.

  2. Enable Continuous integration.

A continuous integration trigger on a build pipeline indicates that the system should automatically queue a new build whenever a code change is committed. You can make the trigger more general or more specific, and also schedule your build (for example, on a nightly basis). See Build triggers.

Save and queue the build

Save and queue a build manually and test your build pipeline.

  1. Select Save & queue, and then select Save & queue.

  2. On the dialog box, select Save & queue once more.

    This queues a new build on the Microsoft-hosted agent.

  3. You see a link to the new build on the top of the page.

    Go to the build console

    Choose the link to watch the new build as it happens. Once the agent is allocated, you'll start seeing the live logs of the build. Notice that the PowerShell script is run as part of the build, and that "Hello world" is printed to the console.


  1. Go to the build summary.

    build console link to build summary

  2. On the Artifacts tab of the build, notice that the script is published as an artifact.

    artifacts explorer

You can view a summary of all the builds or drill into the logs for each build at any time by navigating to the Builds tab in Azure Pipelines. For each build, you can also view a list of commits that were built and the work items associated with each commit. You can also run tests in each build and analyze the test failures.

Add some variables and commit a change to your script

We'll pass some build variables to the script to make our pipeline a bit more interesting. Then we'll commit a change to a script and watch the CI pipeline run automatically to validate the change.

  1. Edit your build pipeline.

  2. On the Tasks tab, select the PowerShell script task.

  3. Add these arguments.


Arguments

-greeter "$(Build.RequestedFor)" -trigger "$(Build.Reason)"

Finally, save the build pipeline.

Next you'll add the arguments to your script.

  1. Go to your Files in Azure Repos (the Code hub in the previous navigation and TFS).

  2. Select the HelloWorld.ps1 file, and then Edit the file.

  3. Change the script as follows:

    Param(
    [string]$greeter,
    [string]$trigger
    )
    Write-Host "Hello world" from $greeter
    Write-Host Trigger: $trigger
    
  4. Commit (save) the script.

Now you can see the results of your changes. Go to the Build and Release page and select Queued. Notice under the Queued or running section that a build is automatically triggered by the change that you committed.

  1. Select the new build that was created and view its log.

  2. Notice that the person who changed the code has their name printed in the greeting message. You also see printed that this was a CI build.

    build summary powershell script log

We just introduced the concept of build variables in these steps. We printed the value of a variable that is automatically predefined and initialized by the system. You can also define custom variables and use them either in arguments to your tasks, or as environment variables within your scripts. To learn more about variables, see Build variables.

You've got a build pipeline. What's next?

You've created a build pipeline that automatically builds and validates whatever code is checked in by your team. At this point, you can continue to the next section to learn about release pipelines. Or, if you prefer, you can skip ahead to create a build pipeline for your app.

Create a release pipeline

Define the process for running the script in two stages.

  1. Go to the Build and Release tab, and then select Releases.

  2. Select the action to create a New pipeline. If a release pipeline is already created, select the plus sign ( + ) and then select Create a release definition.

  3. Select the action to start with an Empty definition.

  4. Name the stage QA.

  5. In the Artifacts panel, select + Add and specify a Source (Build pipeline). Select Add.

  6. Select the Lightning bolt to trigger continuous deployment and then enable the Continuous deployment trigger on the right.


  1. Select the Tasks tab and select your QA stage.

  2. Select the plus sign ( + ) for the job to add a task to the job.

  3. On the Add tasks dialog box, select Utility, locate the PowerShell task, and then select its Add button.

  4. On the left side, select your new PowerShell script task.

  5. For the Script Path argument, select the button to browse your artifacts and select the script you created.

  6. Add these Arguments:

    -greeter "$(Release.RequestedFor)" -trigger "$(Build.DefinitionName)"
    
  7. On the Pipeline tab, select the QA stage and select Clone.

    clone the release environment

  8. Rename the cloned stage Production.

  9. Rename the release pipeline Hello world.

    rename the release pipeline

  10. Save the release pipeline.

A release pipeline is a collection of stages to which the application build artifacts are deployed. It also defines the actual deployment pipeline for each stage, as well as how the artifacts are promoted from one stage to another.

Also, notice that we used some variables in our script arguments. In this case, we used release variables instead of the build variables we used for the build pipeline.

Deploy a release

Run the script in each stage.

  1. Create a new release.

create release - TFS 2018

When Create new release appears, select Create (TFS 2018.2) or Queue (TFS 2018 RTM).

  1. Open the release that you created.

    release created - TFS 2018

  2. View the logs to get real-time data about the release.

    release logs - TFS 2018

You can track the progress of each release to see if it has been deployed to all the stages. You can track the commits that are part of each release, the associated work items, and the results of any test runs that you've added to the release pipeline.

Change your code and watch it automatically deploy to production

We'll make one more change to the script. This time it will automatically build and then get deployed all the way to the production stage.

  1. Go to the Code hub, Files tab, edit the HelloWorld.ps1 file, and change it as follows:

    Param(
    [string]$greeter,
    [string]$trigger
    )
    Write-Host "Hello world" from $greeter
    Write-Host Trigger: $trigger
    Write-Host "Now that you've got CI/CD, you can automatically deploy your app every time your team checks in code."
    
  2. Commit (save) the script.

  3. Select the Builds tab to see the build queued and run.

  4. After the build is completed, select the Releases tab, open the new release, and then go to the Logs.

Your new code automatically is deployed in the QA stage, and then in the Production stage.

release script step final log - - TFS 2018

In many cases, you probably would want to edit the release pipeline so that the production deployment happens only after some testing and approvals are in place. See Approvals and gates overview.

Next steps

You've learned the basics of creating and running a pipeline. Now you're ready to configure your build pipeline for the programming language you're using. Go ahead and create a new build pipeline, and this time, use one of the following templates.

Language Template to use
.NET ASP.NET
.NET Core ASP.NET Core
C++ .NET Desktop
Go Go
Java Gradle
JavaScript Node.js
Xcode Xcode

FAQ

Where can I read articles about DevOps and CI/CD?

What is Continuous Integration?

What is Continuous Delivery?

What is DevOps?

What version control system can I use?

When you're ready to get going with CI/CD for your app, you can use the version control system of your choice:

Screenshot showing how to replicate a pipeline.

After you clone a pipeline, you can make changes and then save it.

After you export a pipeline, you can import it from the All pipelines tab.

After you create a template, your team members can use it to follow the pattern in new pipelines.

Tip

If you're using the New Build Editor, then your custom templates are shown at the bottom of the list.

How do I work with drafts?

If you're editing a build pipeline and you want to test some changes that are not yet ready for production, you can save it as a draft.

Screenshot that shows saving as draft.

You can edit and test your draft as needed.

When you're ready, you can publish the draft to merge the changes into your build pipeline.

publish draft - TFS 2018

Or, if you decide to discard the draft, you can delete it from the All Pipeline tab shown above.

How can I delete a pipeline?

To delete a pipeline, navigate to the summary page for that pipeline, and choose Delete from the ... menu in the top-right of the page. Type the name of the pipeline to confirm, and choose Delete.

What else can I do when I queue a build?

You can queue builds automatically or manually.

When you manually queue a build, you can, for a single run of the build:

Where can I learn more about pipeline settings?

To learn more about build pipeline settings, see:

How do I programmatically create a build pipeline?

REST API Reference: Create a build pipeline

Note

You can also manage builds and build pipelines from the command line or scripts using the Azure Pipelines CLI.