Javascript (Node.js) と Bitbucket Pipelines
このガイドでは、Bitbucket Pipelines を使用して、Docker コンテナで Node.js ソフトウェア プロジェクトをビルドおよびテストする方法について説明します。
If you'd prefer to quickly import a demo repository with a working pipeline to experiment with, have a look at our demo node repo.
手動でセットアップしたい場合、構成の大部分は Pipelines がビルドを定義するために使用する bitbucket-pipelines.yml ファイル内で行います。
Docker で Node.js のバージョンを指定する
Bitbucket Pipelines runs all your builds in Docker containers using an image that you provide at the beginning of your configuration file. You can easily use Node.js with Bitbucket Pipelines by using one of the official Node.js Docker images on Docker Hub.
たとえば、Node.js 10.15.0 を bitbucket-pipelines.yml ファイルの先頭で指定して使用できます。
image: node:22.15.0
pipelines:
default:
- step:
script:
- node -vNode.js の異なるバージョンを使用する場合は、Node.js Docker イメージのタグを変更します。次の例では、Node 4.7.2 のコンテナを開始します。
image: node:4.7.2To find a list of all supported Node.js versions and corresponding image tags, please refer to https://hub.docker.com/_/node/.
オンライン バリデーターによって bitbucket-pipelines.yml ファイルをチェックできます。
依存関係をインストールする
npm を使用して依存関係を管理する
Docker Hub の既定の Node.js イメージには、既定で npm がインストールされています。これをスクリプトで直接使用して、特定のパッケージをローカルまたはグローバルでインストールできます。
package.json ファイルを使用している場合、スクリプトの最初で npm install を実行することで、すべての依存関係を簡単にインストールできます。
image: node:22.15.0
pipelines:
default:
- step:
script:
- npm installYarn を使用して依存関係を管理する
Docker Hub の既定の Node.js イメージには、既定で yarn がインストールされています。これをスクリプトで直接使用して、特定のパッケージをローカルまたはグローバルでインストールできます。
image: node:22.15.0
pipelines:
default:
- step:
script:
- yarn依存関係をグローバルにインストールする (Gulp、Grunt、Angular CLI...)
Node パッケージはグローバルにインストールすることもできます。たとえば、パイプラインが Gulp や Grunt などのタスク ランナーに依存している場合、それらをグローバルにインストールする必要があります。パッケージ マネージャーとして npm を使用している場合、npm install -gコマンドを使用できます。
npm install -g grunt-cli
npm install
gruntYarn を使用している場合は yarn global add を使用します。
yarn global add grunt-cli
yarn
gruntモジュールをダウンロードして非公開リポジトリに公開する
公開 npm レジストリまたは独自の非公開レジストリの非公開モジュールを使用している場合、リポジトリ変数を使用して、リポジトリのルートで .npmrc ファイルで確認できます。開発時に local ~/.npmrc ファイルとの競合を防ぐには、名前を .npmrc_config と設定し、パイプライン スクリプトの一部として名前を変更できます。
.npmrc_config
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
registry=https://registry.npmjs.org非公開 npm レジストリを使用する場合、registry.npmjs.org をレジストリのアドレスで置き換えます。
Then configure the following variable in Bitbucket Pipelines:
NPM_TOKEN: レジストリの認証トークンです。レジストリにログインした後、ローカルの ~/.npmrc で見つけることができます。
bitbucket-pipelines.yml スクリプトは、非公開の依存関係をインストールするために次のようになります。
bitbucket-pipelines.yml
image: node:22.15.0
pipelines:
default:
- step:
script:
- mv .npmrc_config .npmrc
- npm installテスト
ローカルでアプリケーションをテストするために使用したコマンドと同じものを、bitbucket-pipelines.yml に追加します。たとえば、ローカルで npm test を使用してテスト スイートを実行している場合、次の例のようになります。
image: node:22.15.0
pipelines:
default:
- step:
script:
- npm install
- npm testデータベース
Use a service container to run a database in your pipeline.
デプロイメント
Now you've built and tested your project you might want to deploy it somewhere. Have a look at our deployment guides for your next steps.
この内容はお役に立ちましたか?