SCP を使用したデプロイ
このガイドでは、Bitbucket Pipelines で SCP-deploy パイプを使用してリモート ホストにファイルをデプロイする方法を説明します。
この例では、シンプルな ReactApp を作成し、SCP-deploy パイプを使用して本番環境にデプロイしました。
Pipelines と Pipes を使用した自身のホストへのデプロイをハンズオンで確認したい場合、このリポジトリで完全なエンドツーエンドのサンプルを利用できます。
前提条件:
ssh アクセスが可能なリモート ホスト
Bitbucket リポジトリ
ステップ 1: プロジェクト用に Bitbucket リポジトリを構成する
To define the following two variables, in the repository, select More actions (…) next to the name of your repository in the left sidebar, and then select Settings. Under the Pipelines section, select Repository variables.
名前 | 値 |
|---|---|
| リモート ホストで構成されているユーザー。 |
| ファイルの転送先のリモート ホスト。 |
これらの変数は、デプロイ環境、リポジトリ、またはワークスペース レベルで定義できます。
ステップ 2: SSH キーを構成する
Set up an SSH key in Bitbucket Pipelines or provide it as a secured variable: SSH_KEY (see using multiple ssh keys). By default, the SCP-deploy pipe will use your configured SSH key and known_hosts file.
ステップ 3: サーバーを構成する
リモート ホスト (本番環境ノード) でを次のように構成します。
ユーザーによる HTTP (ポート 80) への接続を許可します。
アクセスを許可するように ssh キーを構成します。
サーバーで NGINX を使用する場合の構成例を示します。注: この構成はデモのみを目的としています。
server {
listen 80;
listen [::]:80;
root /var/www/scp-deploy/html;
index index.html index.htm;
server_name <your_host_public_DNS_name>, <example.com>;
location / {
try_files $uri $uri/ =404;
}
}ステップ 4: CI/CD 構成を設定する
bitbucket-pipelines.yml ファイルを編集します。SCP-deploy パイプの詳細を調べるには、エディターの右側でそれを選択します。
次のステップを bitbucket-pipelines.yml に追加します。
image: node:10.15.3
pipelines:
default:
- step:
name: Build and test
caches:
- node
script:
- npm install
- npm test
- npm run build
artifacts:
- build/**
- step:
name: Deploy artifacts using SCP to PROD
deployment: production
script:
- pipe: atlassian/scp-deploy:0.3.3
variables:
USER: $USER
SERVER: $SERVER
REMOTE_PATH: '/var/www/scp-deploy/html'
LOCAL_PATH: 'build/*'この設定では、Bitbucket リポジトリにプッシュするたびに SCP-deploy パイプを使用して、テストを実行し、ビルドして、リモート ホストにファイルをデプロイできます。「Bitbucket Pipelines 設定を編集および設定する方法」の詳細をご覧ください。
注意: LOCAL_PATH でファイル グロブを使用して、デプロイするファイルを指定できます。
たとえば、$BITBUCKET_CLONE_DIR デフォルト変数を使用できます。これは、Docker コンテナ内でリポジトリがクローンされるディレクトリの絶対パスです。
LOCAL_PATH: '${BITBUCKET_CLONE_DIR}' # copy the current directory and all included files. Keeps the parent directory structure.
LOCAL_PATH: '${BITBUCKET_CLONE_DIR}/*' # copy the contents from the current directory.または、'custom/local/path'を指定することもできます。
LOCAL_PATH: 'custom/local/path' # copy your custom directory and all included files. Keeps the parent directory structure.
LOCAL_PATH: 'custom/local/path/*' # copy the contents from your custom directory.LOCAL_PATH 変数として '/' (スラッシュ) を指定すると、ルート ビルド コンテナー ファイルシステムからすべてのファイルがコピーされます。ビルド ディレクトリからすべてのファイルをコピーする場合は、相対ディレクトリ custom/local/path または ${BITBUCKET_CLONE_DIR}/* を使用することをお勧めします。
ステップ 5: アプリケーションを本番環境にデプロイする
アプリケーションのコードを Bitbucket リポジトリにプッシュし、それによってパイプラインをトリガーします。[Pipelines] を選択して、パイプラインの進行状況を確認できます。
これで、ユーザーは優れたアプリケーションのビルドに集中して取り組みながら、その他の作業を Bitbucket Pipelines で処理できるようになりました。
この内容はお役に立ちましたか?