LoginSignup
3
2

More than 5 years have passed since last update.

BitBucket Pipelinesのテスト環境とデプロイ環境に違うイメージを使用する

Last updated at Posted at 2017-01-26

BitBucket Pipelinesが便利そうだったので、いろいろ遊んでた。
その中で、作ってるアプリケーションがNodeだったのでテストはNodeのDockerイメージ使いたいんだけど、デプロイはPythonが必要なのでPythonのDockerイメージ使いたい。
っていうわがままを実現するためにやってみたことをまとめておく。
(もっとまとも?な方法を知ってる方がいたら教えてください)

やりたいこと

  • node:7.4.0のイメージでテストを実行したい
  • テストが成功したら、python:3.5.1のイメージでデプロイを実行したい

結論

できました。

以下のことをやります。

script/run_custom_pipelines.sh を作成

パイプラインのstepの中で、別のパイプラインをキックするためにcurlを実行するスクリプトを用意します。

script/run_custom_pipelines.sh
#!/bin/bash
###
# set variables:
#
# $PIPELINES_REQ_USER
# $PIPELINES_REQ_PASSWORD
# $CUSTOM_PATTERN
###

curl -X POST -is -u "$PIPELINES_REQ_USER":"$PIPELINES_REQ_PASSWORD" \
  -H 'Content-Type: application/json' \
  https://api.bitbucket.org/2.0/repositories/"$BITBUCKET_REPO_OWNER"/"$BITBUCKET_REPO_SLUG"/pipelines/ \
  -d '
  {
    "target": {
      "commit": {
        "hash": "'$BITBUCKET_COMMIT'",
        "type": "commit"
      },
      "selector": {
        "type": "custom",
        "pattern": "'$CUSTOM_PATTERN'"
      },
      "type": "pipeline_commit_target"
    }
  }'

bitbucket-pipelines.yml を作成

masterブランチのパイプラインの最後で先ほどのスクリプトを実行しています。
(ここでは実際にデプロイするスクリプトではなく、pythonのバージョン出力しているだけです)

bitbucket-pipelines.yml
# This is a sample build configuration for Javascript.
# Check our guides at https://confluence.atlassian.com/x/VYk8Lw for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: node:7.4.0

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          - npm install --global mocha
          - npm install
          - npm test
  branches:
    master:
      - step:
          script:
            - npm install --global mocha
            - npm install
            - npm test
            - export CUSTOM_PATTERN=deploy_to_production
            - bash script/run_custom_pipelines.sh
  custom:
    deploy_to_production:
      - step:
          image: python:3.5.1
          script:
            - python -V

リポジトリの環境変数を設定

PIPELINES_REQ_USERPIPELINES_REQ_PASSWORDを設定します。
curl実行時に認証情報として使用するユーザー名とパスワードです。

スクリーンショット 2017-01-27 0.png

 確認

masterブランチにpushされるとテストが走って、成功したらdeploy_to_productionも実行されます。

masterブランチ以外にpushすると、テストをするだけです。

スクリーンショット 2017-01-27 0.16.22.png

スクリーンショット 2017-01-27 0.17.12.png

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2