SCP を使用したデプロイ
このガイドでは、Bitbucket Pipelines で SCP-deploy パイプを使用してリモート ホストにファイルをデプロイする方法を説明します。
For this example we created simple ReactApp and deployed it to production with the SCP-deploy pipe.
Pipelines とパイプを使用した自身のホストへのデプロイをハンズオンで確認したい場合、このリポジトリで完全なエンドツーエンドのサンプルを利用できます。
前提条件:
ssh アクセスが可能なリモート ホスト
Bitbucket リポジトリ
ステップ 1: プロジェクト用に Bitbucket リポジトリを構成する
To define the following two variables, in the repository, select Repository settings on the left sidebar. Under the Pipelines section, select Repository variables.
名前 | 値 |
---|---|
| リモート ホストで構成されているユーザー。 |
| ファイルの転送先のリモート ホスト。 |
You can define these variables at the deployment environment, repository, or workspace level.
ステップ 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.
Note: You can use file globbing in the LOCAL_PATH
to specify the files you want to deploy.
For example, you can use $BITBUCKET_CLONE_DIR
default variable - the absolute path of the directory that the repository is cloned into within the Docker container:
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.
Or you can specify your '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.
If you specify the '/'
(slash) as the LOCAL_PATH
variable, it will copy all files from the root build container filesystem. We recommend using a relative directory custom/local/path
or ${BITBUCKET_CLONE_DIR}/*
if you want to copy all files from the build directory.
ステップ 5: アプリケーションを本番環境にデプロイする
アプリケーションのコードを Bitbucket リポジトリにプッシュし、それによってパイプラインをトリガーします。[Pipelines] を選択して、パイプラインの進行状況を確認できます。
これで、ユーザーは優れたアプリケーションのビルドに集中して取り組みながら、その他の作業を Bitbucket Pipelines で処理できるようになりました。
この内容はお役に立ちましたか?