|
| 1 | +.. SPDX-FileCopyrightText: 2025 Veit Schiele |
| 2 | +.. |
| 3 | +.. SPDX-License-Identifier: BSD-3-Clause |
| 4 | +
|
| 5 | +npm deployment with rsync |
| 6 | +========================= |
| 7 | + |
| 8 | +`npm <https://www.npmjs.com>`_ is a package manager for the JavaScript runtime |
| 9 | +environment `Node.js <https://nodejs.org/en/>`_, and `rsync |
| 10 | +<https://rsync.samba.org>`_ can be used to synchronise the data with the remote |
| 11 | +server. |
| 12 | + |
| 13 | +First steps |
| 14 | +----------- |
| 15 | + |
| 16 | +#. Set up environment variables |
| 17 | + |
| 18 | + ``DEPLOY_DIR`` |
| 19 | + Directory on the remote server to which data is to be distributed.. |
| 20 | + ``DEPLOY_HOST`` |
| 21 | + Host name or IP address of the server to be deployed to. |
| 22 | + ``DEPLOY_KEY_FILE`` |
| 23 | + Path to a private SSH key that is to be used for authentication against |
| 24 | + the server. |
| 25 | + ``DEPLOY_USER`` |
| 26 | + Name of the SSH account. |
| 27 | + ``KNOWN_HOSTS_FILE`` |
| 28 | + Path to a file with predefined :file:`known_hosts` entries with which |
| 29 | + the connection is to be checked. |
| 30 | + |
| 31 | +#. Setting up the CI/CD pipeline |
| 32 | + |
| 33 | + .. code-block:: yaml |
| 34 | + :caption: .gitlab-ci.yml |
| 35 | + :emphasize-lines: 13, 14, 15 |
| 36 | +
|
| 37 | + stages: |
| 38 | + - deploy |
| 39 | +
|
| 40 | + deploy-static-assets: |
| 41 | + stage: deploy |
| 42 | + rules: |
| 43 | + - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH |
| 44 | + environment: |
| 45 | + name: prod |
| 46 | + deployment_tier: production |
| 47 | + image: node:alpine |
| 48 | + script: |
| 49 | + - apk add --no-cache git nodejs npm openssh-client-default rsync |
| 50 | + - chmod 0600 "$DEPLOY_KEY_FILE" |
| 51 | + - ./deploy.sh |
| 52 | +
|
| 53 | + Line 13 |
| 54 | + installs the packages required for building and uploading |
| 55 | + Line 14 |
| 56 | + changes the access authorisations for the ssh key |
| 57 | + Line 15 |
| 58 | + calls the :file:`deploy.sh` file |
| 59 | + |
| 60 | +#. Moves the static files to the server |
| 61 | + |
| 62 | + .. code-block:: sh |
| 63 | + :caption: deploy.sh |
| 64 | + :linenos: |
| 65 | + :emphasize-lines: 7-13, 18, 19, 24, 25-31 |
| 66 | +
|
| 67 | + #!/bin/sh |
| 68 | + set -e |
| 69 | +
|
| 70 | + # Deploy static assets to a server via rsync. |
| 71 | +
|
| 72 | + # Create SSH config. |
| 73 | + cat >deploy.ssh_config <<-END |
| 74 | + Host deploy |
| 75 | + HostName $DEPLOY_HOST |
| 76 | + User $DEPLOY_USER |
| 77 | + IdentityFile $DEPLOY_KEY_FILE |
| 78 | + UserKnownHostsFile $KNOWN_HOSTS_FILE |
| 79 | + END |
| 80 | +
|
| 81 | +
|
| 82 | + # Build the JavaScript application & related files. |
| 83 | + cd vite-app |
| 84 | + npm ci |
| 85 | + npx vite build |
| 86 | +
|
| 87 | + # Deploy the application and GeoJSON data. |
| 88 | + rsync \ |
| 89 | + -rtlv \ |
| 90 | + --rsh 'ssh -F ../deploy.ssh_config' \ |
| 91 | + --rsync-path "mkdir -p \"$DEPLOY_DIR\" && rsync" \ |
| 92 | + ../data/cusy.geojson \ |
| 93 | + cusy.html \ |
| 94 | + dist/cusy-map.css \ |
| 95 | + dist/cusy-map.js \ |
| 96 | + dist/cusy-map.js.map \ |
| 97 | + "deploy:$DEPLOY_DIR" |
| 98 | +
|
| 99 | + Line 7–13 |
| 100 | + creates the ssh configuration file. |
| 101 | +
|
| 102 | + Line 18 |
| 103 | + installs the dependencies of the project from the |
| 104 | + :file:`vite-app/package.json` file. |
| 105 | +
|
| 106 | + .. seealso:: |
| 107 | + `npm-ci <https://docs.npmjs.com/cli/v9/commands/npm-ci>`_ |
| 108 | +
|
| 109 | + Line 19 |
| 110 | + creates the `vite <https://vite.dev>`_ project for production. |
| 111 | +
|
| 112 | + Line 24 |
| 113 | + moves the ssh configuration to the server. |
| 114 | +
|
| 115 | + Lines 25–31 |
| 116 | + moves the application and GeoJSON data to the server. |
0 commit comments