แก้ไข

แชร์ผ่าน


Build and publish a Python app

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

In this quickstart, you create a pipeline that builds and tests a Python app. You see how to use Azure Pipelines to build, test, and deploy Python apps and scripts as part of your continuous integration and continuous delivery (CI/CD) system.

Prerequisites

Python is preinstalled on Microsoft-hosted agents for Linux, macOS, and Windows. You don't have to set up anything more to build Python projects. To see which Python versions are preinstalled, see Use a Microsoft-hosted agent.

Fork the sample code

Fork the sample Python repository to your GitHub account.

  1. Go to the python-sample-vscode-flask-tutorial repository.
  2. Select Fork in the upper-right corner of the page.
  3. Select your GitHub account. By default, the fork is named the same as the parent repository, but you can name it something different.

Important

During the following procedures, you might be prompted to create a GitHub service connection or redirected to GitHub to sign in, install Azure Pipelines, or authorize Azure Pipelines. Follow the onscreen instructions to complete the process. For more information, see Access to GitHub repositories.

Create your pipeline

  1. In your Azure DevOps project, select Pipelines > Create Pipeline, and then select GitHub as the location of your source code.
  2. On the Select a repository screen, select your forked sample repository.
  3. On the Configure your pipeline screen, select Starter pipeline.

Customize your pipeline

On the Review your pipeline YAML screen, replace the contents of the generated azure-pipelines.yml file with the following code. The code:

  • Installs required Python versions and dependencies.
  • Packages build artifacts to a ZIP archive.
  • Publishes the archive to your pipeline.
  • Runs tests.
trigger:
- main

pool:
  vmImage: ubuntu-latest

strategy:
  matrix:
    Python310:
      python.version: '3.10'
    Python311:
      python.version: '3.11'
    Python312:
      python.version: '3.12'

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'
    displayName: 'Use Python $(python.version)'

  - script: |
      python -m pip install --upgrade pip
      pip install -r requirements.txt
    displayName: 'Install dependencies'

  - task: ArchiveFiles@2
    displayName: 'Archive files'
    inputs:
      rootFolderOrFile: $(System.DefaultWorkingDirectory)
      includeRootFolder: false
      archiveType: zip
      archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId)-$(python.version).zip
      replaceExistingArchive: true

  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'drop'
      publishLocation: 'Container'

  - script: |
      pip install pytest pytest-azurepipelines
      pytest
    displayName: 'pytest'

Customize azure-pipelines.yml to match your project configuration.

  • If you have a different agent pool, change the pool name parameter.
  • If necessary, change the Python version to a version installed on your self-hosted agent.
  trigger:
  - main

  pool: 
    name: '<your-pool-name or default>'

  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.12'
    displayName: 'Use Python 3.12'  

  - script: |
      python -m pip install --upgrade pip
      pip install -r requirements.txt
    displayName: 'Install dependencies'


  - task: ArchiveFiles@2
    displayName: 'Archive files'
    inputs:
      rootFolderOrFile: $(System.DefaultWorkingDirectory)
      includeRootFolder: false
      archiveType: zip
      archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      replaceExistingArchive: true

  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'drop'
      publishLocation: 'Container'

  - script: |
      pip install pytest pytest-azurepipelines
      pytest
    displayName: 'pytest'

Run your pipeline

Select Save and run, and then select Save and run again.

The Summary tab shows the status of your pipeline run.

To view your build artifact, select the published link in the Summary tab.

Screenshot of published build artifacts link.

The Artifacts page shows the published build artifacts. Screenshot of published build artifacts.

Screenshot of completed Python job.

The Artifacts page shows the published build artifacts. Screenshot of published build artifacts link.

To view the test results, select the Tests tab.

Screenshot of pipeline test results.

Select Run.

The build number is displayed at the top of the page. Select the build number to see the details of the build.

Screenshot of pipeline build link.

The Summary tab shows the status of your pipeline run.

To download your build artifact, select the drop link from the Build artifacts published section.

Screenshot of completed Python job.

To view the test results, select the Tests tab.

Screenshot of pipeline test results.

Clean up

When you finish this quickstart, you can delete the Azure DevOps project you created.

  1. In your project, select the Project settings gear icon in the lower left corner of the page.
  2. At the bottom of the Project overview page, select Delete.
  3. Enter the project name and select Delete.

Congratulations, you successfully created and ran a pipeline that built and tested a Python app. Now you can use Azure Pipelines to build, test, and deploy Python apps and scripts as part of your continuous integration and continuous delivery (CI/CD) system.

Next steps