next.js rsync 部署方案

12 min read
# Using the node image provided by bitbucket
image: node:10.15.3

pipelines:
  branches:
    # run this on every commit to the branch "staging"
    staging:
      - step:
          name: buildAndDeploy
          # define which caches to use
          caches:
            - node # provided by bitbucket to cache node_modules
            - nextcache # see definitions section below
          script:
            # install rsync
            - apt-get update && apt-get -qq install rsync
            # install node modules
            - npm install
            # build Next.js app
            - npx next build
            # create deploy directory (to contain .next folder, package.json, node_modules, public)
            - mkdir deploy
            - cp -a .next ./deploy
            - cp package.json ./deploy
            - cp -a node_modules ./deploy
            - cp -a public ./deploy
            # rsync to a temp directory on remote server
            - rsync -arz --delete $BITBUCKET_CLONE_DIR/deploy/ $USER@$REMOTE_IP:/var/www/staging-temp
            # clear current serving directory, sync from temp directory to serving directory, restart next server
            - ssh $USER@$REMOTE_IP "rsync -ar --delete /var/www/staging-temp/ /var/www/staging && pm2 restart next-staging && rm -r /var/www/staging-temp"
definitions:
  caches:
    nextcache: .next/cache