I wanted to make the process of deploying code to server easier on each push to repository. I tried almost all suggested ways to do it but they didn't work. So I created this package. It supports Ubuntu servers and GitHub/GitLab/Bitbucket repos.
Nodeployed uses Fastify as Node.js server, Execa to run such commands to deploy code and Minimist to parse command arguments.
To be able to use it sucessfully, you need:
- Install Node.js and npm on your server
-
Setup your Apache/NGINX server to reverse proxy all requests to
http://localhost:YOUR_PORT
inlocation
block: -
For
NGINX
change your server block config to something like this:
# Your server in /etc/nginx/sites-available/example.com
server {
...
location /deploy/ {
proxy_pass http://localhost:YOUR_PORT;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
...
}
- For
Apache2
:
<VirtualHost *:*>
ProxyPass /deploy/ http://0.0.0.0:3000/
ServerName localhost
</VirtualHost>
- Optionally: Install LetsEncrypt on that domain that you will use.
- Clone repository that you want to work with with
git clone
- Run
sudo chown -R yourusername:webserverusername you-repo-dir-name/
for your repository directory to make script able to fully usegit
commands.
Install package globally from NPM repo
via npm
and start the process with pm2:
$ npm i -g nodeployed pm2
$ pm2 start nodeployed -- --port 8234 --dir /absolute/path/to/git/dir/ --command "command1_to_run && command2_to_run ..."
Go to http://example.com/deploy
.
If script is properly working then you should see this message:
Nodeployed is working! You should set this link in your Webhooks configuration for your repository!
We will configure our repository later in Configure repository for nodeployed
section.
Also if you want to run nodeployed
as system service pm2
can help you. For that run next commands:
$ pm2 startup systemd
And then copy the command from the output
and run it, example:
$ sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u user --hp /home/user
You can save current configuration of running applications if you run multiple instances of nodeployed
:
$ pm2 save
Then you can see the status of pm2 system service
by running:
$ systemctl status pm2-user
or start it:
$ sudo systemctl start pm2-user
or stop it by running next command:
$ sudo systemctl stop pm2-user
where user β username that gave you pm2
from running the pm2 startup systemd
command.
- Go to the directory for which you would like to setup autodeploy to your server.
- Go to
Settings
page. - Go to
Webhooks
page =>Add webhook
. - Type in your GitHub password if prompted
- Setup webhook:
- Payload: http://example.com/deploy/?token=YOUR_TOKEN_FROM_ARGUMENT
- Content Type: application/json
- Which events would you like to trigger this webhook? Just the push event.
- Active: check it
- Press
Add webhook
- Now on any push to this GitHub repository
nodeployed
should pull changes on server and run deploy commands.
- Go to the directory for which you would like to setup autodeploy to your server.
- Go to
Settings
=>Integrations
. - Setup webhook:
- URL: http://example.com/deploy/?token=YOUR_TOKEN_FROM_ARGUMENT
- Secret Token:
leave it empty
- Trigger events: Push events
- Enable SSL verification: check it if you have installed
LetsEncrypt
- Press
Add webhook
- Now on any push to this GitLab repository
nodeployed
should pull changes on server and run deploy commands.
- Go to the directory for which you would like to setup autodeploy to your server.
- Go to
Settings
=>Webhooks
=>Add webhook
. - Setup webhook:
- Title: any value, e.g.
nodeployed
to recognize it - URL: http://example.com/deploy/?token=YOUR_TOKEN_FROM_ARGUMENT
- Skip certificate verification: check it if you haven't installed
LetsEncrypt
- Enable request history collection: check it
- Triggers: Repository push
- Press
Save
- Now on any push to this Bitbucket repository
nodeployed
should pull changes on server and run deploy commands.
Default: 9500
Maximum: 65535
Type: number
Sets IPv4 port on which server will be running and listening for requests.
Type: string:required
Sets the token to validate server request.
Type: string:required
Sets the absoulte
path to target deploy GIT repository.
Default: bash ./deploy.sh
Type: string
Pass commands that needs to run after pull from repository/to deploy, e.g. --command "npm install && npm run build"
.
If nothing passed to --command
it will fallback to command bash ./deploy.sh
so you need to manually create this script with all needed commands.
Default: master
Type: string
Sets the working branch for target deploy GIT repository.
MIT
Thanks goes to these wonderful people (emoji key):
Paul Ekshmidt π» π π§ π€ | Vladislav π» π |
This project follows the all-contributors specification. Contributions of any kind welcome!