Azure Pipelines 用に Python をカスタマイズする
Azure Pipelines を使用すると、独自のインフラストラクチャを設定しなくても Python アプリを構築できます。 python アプリのビルド、テスト、実行に一般的に使用するツール (pip など) は、Azure Pipelines の Microsoft でホストされているエージェント にプレインストールされます。
Python を使用して最初のパイプラインを作成するには、 Python のクイックスタートを参照してください。
特定の Python バージョンを使用する
パイプラインで特定のバージョンの Python を使用するには、Azure-pipelines.yml に Python バージョンの使用タスクを追加します。 次のスニペットは、Python 3.11 を使用するようにパイプラインを設定します。
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.11'
複数の Python バージョンを使用する
複数の Python バージョンでパイプラインを実行する (たとえば、それらのバージョンに対してパッケージをテストする) には、Python バージョンの を使用して をjob
matrix
定義します。 次に、変数を UsePythonVersion
参照するようにタスクを matrix
設定します。
jobs:
- job: 'Test'
pool:
vmImage: 'ubuntu-latest' # other options: 'macOS-latest', 'windows-latest'
strategy:
matrix:
Python38:
python.version: '3.8'
Python39:
python.version: '3.9'
Python310:
python.version: '3.10'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(python.version)'
マトリックス内の各 Python バージョンを使用して実行するタスクを追加できます。
Python スクリプトを実行する
リポジトリで Python スクリプトを実行するには、 要素を script
使用し、ファイル名を指定します。 次に例を示します。
- script: python src/example.py
Python スクリプト タスクを使用してインライン Python スクリプトを実行することもできます。
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
print('Hello world 1')
print('Hello world 2')
スクリプトの実行をパラメーター化するには、タスクを PythonScript
値と共 arguments
に使用して、実行中のプロセスに引数を渡します。 または より高度なargparse
ライブラリを使用sys.argv
して、引数を解析できます。
- task: PythonScript@0
inputs:
scriptSource: inline
script: |
import sys
print ('Executing script file is:', str(sys.argv[0]))
print ('The arguments are:', str(sys.argv))
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--world", help="Provide the name of the world to greet.")
args = parser.parse_args()
print ('Hello ', args.world)
arguments: --world Venus
依存関係のインストール
スクリプトを使用して、 を使用して特定の PyPI パッケージを pip
インストールできます。 たとえば、この YAML では、 と パッケージがインストールまたはアップグレードpip
されますwheel
setuptools
。
- script: python -m pip install --upgrade pip setuptools wheel
displayName: 'Install tools'
インストール要件
と フレンドを更新 pip
した後、一般的な次の手順は、 から依存関係 をインストールrequirements.txt。
- script: pip install -r requirements.txt
displayName: 'Install requirements'
テストの実行
スクリプトを使用して、パイプライン内のさまざまなテストをインストールして実行します。
フレーク8を使用してリントテストを実行する
lint テストをインストールまたはアップグレード flake8
して実行するには、次の YAML を使用します。
- script: |
python -m pip install flake8
flake8 .
displayName: 'Run lint tests'
pytest を使用してテストし、pytest-cov を使用してカバレッジ メトリックを収集する
この YAML を使用して、テストをインストール pytest
して実行し pytest-cov
、テスト結果を JUnit 形式で出力し、コード カバレッジの結果を Cobertura XML 形式で出力します。
- script: |
pip install pytest pytest-azurepipelines
pip install pytest-cov
pytest --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml
displayName: 'pytest'
Tox を使用してテストを実行する
Azure Pipelines では、並列の Tox テスト ジョブを実行して作業を分割できます。 開発用コンピューターでは、テスト環境をシリーズで実行する必要があります。 このサンプルでは、 を使用 tox -e py
して、現在のジョブでアクティブな Python のバージョンを実行します。
- job:
pool:
vmImage: 'ubuntu-latest'
strategy:
matrix:
Python38:
python.version: '3.8'
Python39:
python.version: '3.9'
Python310:
python.version: '3.10'
steps:
- task: UsePythonVersion@0
displayName: 'Use Python $(python.version)'
inputs:
versionSpec: '$(python.version)'
- script: pip install tox
displayName: 'Install Tox'
- script: tox -e py
displayName: 'Run Tox'
テスト結果を公開する
テスト結果の発行タスクを追加して、JUnit または xUnit のテスト結果をサーバーに発行します。
- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testResultsFiles: '**/test-*.xml'
testRunTitle: 'Publish test results for Python $(python.version)'
コード カバレッジの結果を発行する
コード カバレッジ結果の発行タスクを追加して、コード カバレッジの結果をサーバーに発行します。 ビルドの概要でカバレッジ メトリックを確認し、HTML レポートをダウンロードして詳細な分析を行うことができます。
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
コードのパッケージ化と配信
で twine
認証するには、 Twine Authentication タスク を使用して、環境変数に認証資格情報を PYPIRC_PATH
格納します。
- task: TwineAuthenticate@0
inputs:
artifactFeed: '<Azure Artifacts feed name>'
pythonUploadServiceConnection: '<twine service connection from external organization>'
次に、 を使用twine
してパッケージを発行するカスタム スクリプトを追加します。
- script: |
twine upload -r "<feed or service connection name>" --config-file $(PYPIRC_PATH) <package path/files>
Azure Pipelines を使用して Python アプリの イメージをビルド し、 コンテナー レジストリにプッシュすることもできます。
関連する拡張機能
- PyCharm 用 Azure DevOps プラグイン (IntelliJ) (Microsoft)
- Visual Studio Code の Python (Microsoft)
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示