npm deployment with rsync¶
npm is a package manager for the JavaScript runtime environment Node.js, and rsync can be used to synchronise the data with the remote server.
First steps¶
Set up environment variables
DEPLOY_DIR
Directory on the remote server to which data is to be distributed..
DEPLOY_HOST
Host name or IP address of the server to be deployed to.
DEPLOY_KEY_FILE
Path to a private SSH key that is to be used for authentication against the server.
DEPLOY_USER
Name of the SSH account.
KNOWN_HOSTS_FILE
Path to a file with predefined
known_hosts
entries with which the connection is to be checked.
Setting up the CI/CD pipeline
.gitlab-ci.yml¶stages: - deploy deploy-static-assets: stage: deploy rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH environment: name: prod deployment_tier: production image: node:alpine script: - apk add --no-cache git nodejs npm openssh-client-default rsync - chmod 0600 "$DEPLOY_KEY_FILE" - ./deploy.sh
- Line 13
installs the packages required for building and uploading
- Line 14
changes the access authorisations for the ssh key
- Line 15
calls the
deploy.sh
file
Moves the static files to the server
deploy.sh¶1#!/bin/sh 2set -e 3 4# Deploy static assets to a server via rsync. 5 6# Create SSH config. 7cat >deploy.ssh_config <<-END 8 Host deploy 9 HostName $DEPLOY_HOST 10 User $DEPLOY_USER 11 IdentityFile $DEPLOY_KEY_FILE 12 UserKnownHostsFile $KNOWN_HOSTS_FILE 13END 14 15 16# Build the JavaScript application & related files. 17cd vite-app 18npm ci 19npx vite build 20 21# Deploy the application and GeoJSON data. 22rsync \ 23 -rtlv \ 24 --rsh 'ssh -F ../deploy.ssh_config' \ 25 --rsync-path "mkdir -p \"$DEPLOY_DIR\" && rsync" \ 26 ../data/cusy.geojson \ 27 cusy.html \ 28 dist/cusy-map.css \ 29 dist/cusy-map.js \ 30 dist/cusy-map.js.map \ 31 "deploy:$DEPLOY_DIR"
- Line 7–13
creates the ssh configuration file.
- Line 18
installs the dependencies of the project from the
vite-app/package.json
file.See also
- Line 19
creates the vite project for production.
- Line 24
moves the ssh configuration to the server.
- Lines 25–31
moves the application and GeoJSON data to the server.