LoginSignup
9
7

More than 3 years have passed since last update.

Nowで簡単デプロイ

Last updated at Posted at 2019-06-18

概要

サーバレスデプロイメントプラットフォームのNowによるデプロイの自動化方法を紹介します。

Nowについて

使用するNowのバージョンはv2です。

導入

npm

$ npm install -g now

yarn

$ yarn global add now

ログイン

$ now login

CLIによる手動デプロイ

now.jsonを作成し、nowコマンドを実行するとデプロイできます。

Next.jsアプリにおけるnow.jsonの例

now.json
{
    "version": 2,
    "name": "example-app",
    "builds": [{
        "src": "next.config.js",
        "use": "@now/next"
    }],
    "routes": [{
        "src": "/(.*)",
        "dest": "/$1"
    }]
}

デプロイ

$ now

本番環境に向けてデプロイする場合

now.jsonのaliasに本番環境で使用するドメインを記述することができます。
https://zeit.co/docs/v2/domains-and-aliases/aliasing-a-deployment

now.json
{
  "version": 2,
  "alias": "example-domain.com"
}

aliasで指定したURLにデプロイする場合は以下のコマンドを実行します。

$ now --target production

aliasをコマンドで直接指定してデプロイすることもできます。

$ now alias https://get-started-hwbbrak5g.now.sh example-domain.com

ドメインはNowのダッシュボードで購入できます。

GitHub連携で自動デプロイ

NowのダッシュボードのGitHub Integrationから連携することができます。
スクリーンショット 2019-06-18 23.17.07.png

連携すると、連携したレポジトリのmasterへのコミットを本番環境へ自動的にデプロイしてくれるようになります。
また、他のコミットでもデプロイが行われユニークなURLが割り当てられます。
URLを共有して、コミットごとの開発状況を簡単に共有することができるのが便利だと感じました。
これらの機能の有効/無効化の設定などもnow.jsonに記述できます。
https://zeit.co/docs/v2/deployments/configuration/?query=autoalias#github.enabled

AzurePipelinesでデプロイ

Nowは上述したようにGitHub連携の自動デプロイが強力なので、単にデプロイ自動化の用途でなら必要はほぼほぼありませんが、テストを実行してからデプロイしたい場合などはCIツールを利用すると良さそうです。
今回はAzurePipelinesを利用してみました。
AzurePipelinesでは設定をazure-pipelines.ymlに記述します。
Next.js + テストツールはJestの想定です。

azure-pipelines.yml
trigger:
- master

variables:
  CI: true

jobs:
- job: Ubuntu
  pool:
    vmImage: 'ubuntu-latest'

  steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '10.x'
    displayName: 'Install Node.js'

  - script: |
      npm install
    displayName: 'npm install'
  - script: |
      npm run test
    displayName: 'exec test'
  - script: |
      npm run build
    displayName: 'exec build'
  - script: |
      npm install --global now
      now --target production --token xxxxxxxxxxx
    displayName: 'exec deploy'

非対話モードでJestを実行するために、環境変数CI=trueを設定しています。
Nowにログインしていない状態なのでデプロイするにはtokenが必要です。
tokenはダッシュボードで作成できます。
スクリーンショット 2019-06-18 23.38.55.png
これでmasterにcommitした時、自動でテストが実行されて、本番環境にデプロイされるようになりました。

9
7
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
9
7