Python と Bitbucket Pipelines

このガイドでは、Bitbucket Pipelines を使用して、Docker コンテナで Python を使うソフトウェア プロジェクトをビルドおよびテストする方法について説明します。

実際に動作するパイプラインを持つデモ リポジトリをインポートしたい場合、デモ用の python リポジトリをご参照ください。

手動でセットアップしたい場合、構成の大部分は Pipelines がビルドを定義するために使用する bitbucket-pipelines.yml ファイル内で行います。

Docker で Python のバージョンを指定する

Bitbucket Pipelines は、構成ファイルの最初に指定したイメージを使用し、すべてのビルドを Docker コンテナで実行します。Docker Hub にあるいずれかの公式 Python Docker イメージを使用して、Bitbucket Pipelines で簡単に Python を使用できます。デフォルトの Python イメージを使用する場合、依存関係の解決に役立つ pip がデフォルトでインストールされています。

For instance, you can use Python 3.7.2 by specifying it at the beginning of your bitbucket-pipelines.yml file.

image: python:3.7.2 pipelines: default: - step: script: - python --version

Python の異なるバージョンを使用する場合、Python Docker イメージのタグを変更します。次の例は、Python 3.5.1 のコンテナを開始します。

image: python:3.5.1

サポートされる Python バージョンと対応する image タグの一覧については、https://hub.docker.com/r/library/python/ をご参照ください。

前述の標準 Python イメージのため、Django の Docker イメージは更新が停止されています。

You can check your bitbucket-pipelines.yml file with our online validator.

依存関係をインストールする

If you are using a requirements.txt file, you can simply run pip at the beginning of your script to install all the dependencies.

image: python:3.7.2 pipelines: default: - step: script: - pip install -r requirements.txt

You can also install dependencies with the pip install command.

image: python:3.7.2 pipelines: default: - step: script: - pip install django

データベース

Bitbucket Pipelines では、サービスを定義して適切な段階でインスタンス化することで、パイプラインの実行中に追加のサービスを起動できます。

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.

 

さらにヘルプが必要ですか?

アトラシアン コミュニティをご利用ください。