SCP を使用したデプロイ
このガイドでは、Bitbucket Pipelines で SCP-deploy パイプを使用してリモート ホストにファイルをデプロイする方法を説明します。
For this example we created simple ReactApp and deployed it to production with the SCP-deploy pipe.
A full end-to-end example is available in this repository if you prefer a more hands-on experimentation with deploying to your host using Pipelines and Pipes.
前提条件:
Remote host with SSH access
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 構成を設定する
Edit your bitbucket-pipelines.yml file. To find out more about the SCP-deploy pipe, select it in the right of the editor.
次のステップを 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/*'This configuration allows you to run tests, build, and deploy your files to the remote host using the SCP-deploy pipe after each push to the Bitbucket repository. Learn more about how to edit and configure your Bitbucket pipelines configuration.
注意: 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 で処理できるようになりました。
この内容はお役に立ちましたか?