shell-deployer
v1.5.0
Published
Simple bash shell deployer for nodejs and laravel applications
Downloads
311
Readme
Shell Deployer
Shell Deployer is a simple bash shell based deployment tool designed to streamline the deployment process of Laravel and Node.js projects via GitLab CI/CD pipelines. It simplifies the deployment steps, making it easy to deploy projects to various environments with minimal configuration.
Getting Started
To use deployer in your project, add npx deployment script call in your .gitlab-ci.yml
file with the necessary configurations. Below are examples for both Laravel and Node.js projects.
Node.js GitLab CI Example
stages:
- deploy
.deploy: &deploy
image: node:20-bullseye
stage: deploy
allow_failure: false
when: manual
tags:
- deploy
script:
- npx -y -p [email protected] deploy-nodejs dist/spa/ --with-build
deploy to staging:
<<: *deploy
environment:
name: staging
url: https://nodejs-project.example.com
variables:
DEPLOY_HOST: staging.example.com
DEPLOY_USER: example_user
DEPLOY_PATH: /home/example_user/app
DEPLOY_SSH_KNOWN_HOSTS: $STAGING_DEPLOY_KNOWN_HOSTS
DEPLOY_SSH_PRIVATE_KEY: $STAGING_DEPLOY_PRIVATE_KEY
Laravel GitLab CI Example
.deploy: &deploy
stage: deploy
allow_failure: false
when: manual
tags:
- deploy
script:
- npx --yes -p [email protected] deploy-laravel app.tgz --with-build
deploy to staging:
<<: *deploy
environment:
name: staging
url: https://laravel-project.example.com
variables:
DEPLOY_HOST: staging.example.com
DEPLOY_USER: example_user
DEPLOY_PHP_COMMAND: php8.3
DEPLOY_PATH: /home/example_user/app
DEPLOY_SSH_KNOWN_HOSTS: $STAGING_DEPLOY_KNOWN_HOSTS
DEPLOY_SSH_PRIVATE_KEY: $STAGING_DEPLOY_PRIVATE_KEY
Configuration
Environment Variables
Required Environment Variables
- DEPLOY_HOST: The hostname or IP address of the server where the project will be deployed.
- DEPLOY_USER: The username used to log into the server.
- DEPLOY_PATH: The path on the server where the project should be deployed. This script uses this path to create a
releases
directory where each release will be stored in a timestamped subdirectory. Latest release will be symlinked tocurrent
directory.
Optional Environment Variables
- DEPLOY_SSH_KNOWN_HOSTS: Use this variable to specify custom SSH known hosts when deploying to servers that are not already in your
known_hosts
file, or when there is no existing and/or persistentknown_hosts
file, such as when deploying from a continuous integration (CI) environment. When defined, the script will append the specified known hosts information to theknown_hosts
file located at$DEPLOY_SSH_PATH/known_hosts
. This ensures secure SSH connections to new servers or under customized deployment conditions. - DEPLOY_SSH_PRIVATE_KEY: The actual SSH key as a string. This script dynamically creates an SSH key file at
DEPLOY_SSH_PRIVATE_KEY_PATH
and sets the correct permissions (chmod 600
). This is useful for CI/CD environments where you might not want to store the private key on the filesystem or in the image. - DEPLOY_SSH_PORT: The port to use for SSH connections. If not set, the default SSH port
22
is used. This allows flexibility for deployments to servers configured to use non-standard SSH ports. - DEPLOY_SSH_PRIVATE_KEY_PATH: The path where the deployment script should store the SSH key used for the deployment. If not set, the script defaults to using
$HOME/.ssh/id_rsa
. This is critical if you're using a specific SSH key for deployment that isn't the default key.
Optional Laravel Environment Variables
- DEPLOY_PHP_COMMAND: PHP binary name on remote server. Either name (ex.
php8.3
) or full path (ex/usr/bin/php8.3
) can be specified. If not set, the default name ofphp
is used. - DEPLOY_SHARED_FILES: List of
;
separated files inDEPLOY_PATH/shared
directory to be symlinked against deployed release. If not set, the default value of.env
is used. - DEPLOY_SHARED_DIRECTORIES: List of
;
separated directories inDEPLOY_PATH/shared
directory to be symlinked against deployed release. If not set, the default value ofstorage
is used. - DEPLOY_SHARED_STORAGE_DIRECTORIES: List of
;
separated directories inDEPLOY_PATH/shared/storage
directory to be created. If not set, the default value ofapp/public;framework/cache/data;framework/views;framework/sessions;logs
is used. - DEPLOY_ARTISAN_COMMANDS: List of
;
separated artisan commands to be called to complete deployment process. If not set, the default value ofstorage:link;config:cache;migrate --force;view:cache;queue:restart
is used. IfDEPLOY_CUSTOM_COMMANDS
is set, noDEPLOY_ARTISAN_COMMANDS
are executed. - DEPLOY_CUSTOM_COMMANDS: List of
;
separated commands to be called to complete deployment process. If not set, no commands are executed.
Laravel configuration
Deployment archive building
When building a Laravel deployment archive, the list of files and directories to exclude from the build package is specified in .buildignore, which can be found here: https://github.com/cube-agency/shell-deployer/blob/master/.buildignore.
If you need a customized list of files and directories to ignore, create a file named .buildignore at the root of your project.
.env configuration
The deployment process assumes that DEPLOY_PATH/shared/.env
exists. If it does not, an error will be thrown.
Script Parameters
- DEPLOYMENT_SOURCE_PATH: This is a required positional argument that specifies the source directory or file of the deployment. It must be passed when invoking the script.
- --with-build: This optional flag can be added as the last argument to the script to indicate that a build process should occur as part of the deployment. If not passed, see build process for manual build process.
Build process
The build process can also be executed by the deployer script.
Before initiating the deploy process, you can invoke a dedicated build script.
The build process will execute npm i && npm run build
to construct the project as specified in package.json for both Node.js and Laravel builds.
Node.js
Use the command npx -y -p [email protected] build-nodejs
to build.
The build result is the directory specified by npm run build
.
Laravel
Use the command npx -y -p [email protected] build-laravel
to build.
In addition to the Node.js npm run build
, composer install
will be executed to install all required dependencies, excluding dev packages.
The build result is an app.tgz
file in the local directory, containing all files and directories from the current directory, except those specified in .buildignore
.
Miscellaneous
Retrieving DEPLOY_SSH_KNOWN_HOSTS
Run the following command with the actual host or IP to output the content of known hosts to the console:ssh-keyscan -p 22 HOST_OR_IP 2>/dev/null