Python と Bitbucket Pipelines
このガイドでは、Bitbucket Pipelines を使用して、Docker コンテナで Python を使うソフトウェア プロジェクトをビルドおよびテストする方法について説明します。
If you'd prefer to quickly import a demo repository with a working pipeline to experiment with, have a look at our demo python repo.
手動でセットアップしたい場合、構成の大部分は Pipelines がビルドを定義するために使用する bitbucket-pipelines.yml ファイル内で行います。
Docker で Python のバージョンを指定する
Bitbucket Pipelines runs all your builds in Docker containers using an image that you specify at the beginning of your configuration file. You can easily use Python with Bitbucket Pipelines by using one of the official Python Docker images on Docker Hub. If you use the default Python image it will come with pip installed by default to help you manage your dependencies.
たとえば、bitbucket-pipelines.yml ファイルの先頭で指定して、Python 3.7.2 を使用できます。
image: python:3.7.2
pipelines:
default:
- step:
script:
- python --versionPython の異なるバージョンを使用する場合、Python Docker イメージのタグを変更します。次の例は、Python 3.5.1 のコンテナを開始します。
image: python:3.5.1You can find a list of all supported Python versions and corresponding image tags, refer to https://hub.docker.com/r/library/python/.
Note that the Django Docker images have been deprecated in favor of the standard Python images mentioned above.
You can check your bitbucket-pipelines.yml file with our online validator.
依存関係をインストールする
requirements.txt ファイルを使用している場合、スクリプトの最初で pip を実行することで、すべての依存関係を簡単にインストールできます。
image: python:3.7.2
pipelines:
default:
- step:
script:
- pip install -r requirements.txtpip install コマンドで依存関係をインストールすることもできます。
image: python:3.7.2
pipelines:
default:
- step:
script:
- pip install djangoデータベース
Bitbucket Pipelines allows you to launch extra services during the execution of your pipeline by defining the service, and instantiating it on the appropriate step.
We've compiled a list of of bitbucket-pipeline.yml examples to help get started with your favorite database.
テスト
ローカルでアプリケーションをテストするために使用したコマンドと同じものを、bitbucket-pipelines.yml に追加します。具体的な Python ツールの例を示します。
PyUnit
PYUnit テストはシンプルに実行できます。
image: python:3.7.2
pipelines:
default:
- step:
script:
- python -m unittest discover tests/DJango
Django プロジェクトのテストは、ローカルで Django をテストしたときと同じように実行できます。Pipelines 環境にも Django がインストールされていることを確認してください。
image: python:3.7.2
pipelines:
default:
- step:
script:
- pip install django
- python manage.py test
Remember, you can check your bitbucket-pipelines.yml file with our online validator.
この内容はお役に立ちましたか?