Zero downtime deployment Laravel with Capistrano
APACHE-2.0 License
Zero downtime deployment Laravel with Capistrano
Ruby Environment (RBENV) or Ruby Version Manager (RVM)
Clone this repo
git clone [email protected]:zeroc0d3/deploy-laravel.git
Running bundle
cd deploy-laravel
bundle install
Edit Laravel source target in config/deploy.rb
set :repo_url, "[YOUR_LARAVEL_REPO]"
Edit target server environment in config/deploy/[environment].rb
server '[YOUR_TARGET_SERVER]'
Upload your shared files & folders in your server / VPS
/data/[laravel-project]/[environment]/shared/src
---
.env
composer.json
composer.lock
node_modules
package.json
storage/debugbar
storage/framework
storage/logs
vendor
yarn.lock
Setup number of release folder in config/deploy.rb
set :keep_releases, 5 ## keep 5 release folder
append :linked_files, "#{fetch(:source)}/.env", "#{fetch(:source)}/composer.json", "#{fetch(:source)}/composer.lock", "#{fetch(:source)}/package.json", "#{fetch(:source)}/yarn.lock",
---
.env
composer.json
composer.lock
package.json
yarn.lock
append :linked_dirs, "#{fetch(:source)}/vendor", "#{fetch(:source)}/node_modules", "#{fetch(:source)}/storage"
---
vendor
node_modules
storage
[laravel-project]
├── staging
│ ├── current -> /data/[laravel-project]/staging/releases/20200823134641/
│ ├── releases
│ │ ├── 20200823081119
│ │ └── 20200823134641 ## the latest symlink
│ │ ├── .env -> /data/[laravel-project]/staging/shared/src/.env
│ │ ├── composer.json -> /data/[laravel-project]/staging/shared/src/composer.json
│ │ ├── composer.lock -> /data/[laravel-project]/staging/shared/src/composer.lock
│ │ ├── vendor -> /data/[laravel-project]/staging/shared/src/vendor/
│ │ ├── node_modules -> /data/[laravel-project]/staging/shared/src/node_modules/
│ │ ├── storage -> /data/[laravel-project]/staging/shared/src/storage/
│ │ └── yarn.lock -> /data/[laravel-project]/staging/shared/src/yarn.lock
│ ├── shared
│ │ ├── log
│ │ └── src
│ │ ├── .env
│ │ ├── composer.json
│ │ ├── vendor
│ │ ├── node_modules
│ │ └── storage
│ │ ├── debugbar
│ │ ├── framework
│ │ │ ├── cache
│ │ │ ├── sessions
│ │ │ └── views
│ │ ├── logs
│ │ └── users
│ └── tmp
└── [environments]
cap staging deploy
cap production deploy
cap [environment] nginx:[manual_reload|manual_restart]
---
cap staging nginx:manual_reload
cap staging nginx:manual_restart
cap [environment] phpfpm:[manual_reload|manual_restart]
---
cap staging phpfpm:manual_reload
cap staging phpfpm:manual_restart
cap [environment] composer:[install|dumpautoload|initialize]
---
cap staging composer:install
cap staging composer:dumpautoload
cap staging composer:initialize ## (will run install & dumpautoload)
cap [environment] artisan:[clear_view|clear_cache|clear_all]
---
cap staging artisan:clear_view
cap staging artisan:clear_cache
cap staging artisan:clear_all ## (will run clear_view & clear_cache)
cap [environment] npm:[install|update|cleanup|reinstall]
---
cap staging npm:install
cap staging npm:update
cap staging npm:cleanup
cap staging npm:reinstall ## (will run cleanup & install)
cap [environment] yarn:[install|update|cleanup|reinstall]
---
cap staging yarn:install
cap staging yarn:update
cap staging yarn:cleanup
cap staging yarn:reinstall ## (will run cleanup & install)
.env
cp .env.example .env
./run-docker.sh
./run-deploy.sh
make run-docker
make run-deploy
make stop-docker
make stop-deploy
make remove-docker
make remove-deploy
docker-compose -f compose/app-compose.yml down
--- or ---
make remove
docker-compose -f compose/app-compose-deploy.yml down
--- or ---
make remove-deploy
Create secure PEM file for deployment
openssl rsa -in id_rsa -outform pem > ~/.ssh/id_rsa.pem
--- or ---
./run-encode.sh
Added PEM file (myapp.pem) in keys
folder
cp ~/.ssh/id_rsa.pem keys/myapp.pem
GitLab Configuration Variable : .gitlab-ci.yml
MYAPP_SSH_PRIVATE_KEY=
MYAPP_SSH_PUBLIC_KEY=
MYAPP_KNOWN_HOSTS=
BitBucket Configuration Variable (base64 encode) : bitbucket-pipelines.yml
MYAPP_SSH_PRIVATE_KEY=
MYAPP_SSH_PUBLIC_KEY=
MYAPP_KNOWN_HOSTS=
CircleCI Configuration Variable : .circleci/config.yml
MYAPP_SSH_PRIVATE_KEY=
MYAPP_SSH_PUBLIC_KEY=
MYAPP_KNOWN_HOSTS=
OpenShift Configuration : .openshift/action_hooks
Jenkins Configuration : jenkins/staging/Jenkinsfile for Staging and jenkins/production/Jenkinsfile for Production, detail documentation refer to this link
Trigger Pipeline CI/CD
- Commit
git commit -m "[messages]"
- Merge / Checkout Branch `dev-master` for production
git checkout dev-master
git merge [from_branch]
--- or ---
git branch -D dev-master # delete dev-master branch
git branch dev-master
- Merge / Checkout Branch `dev-staging` for staging
git checkout dev-staging
git merge [from_branch]
--- or ---
git branch -D dev-staging # delete dev-staging branch
git branch dev-staging
- Push all branch & tags
git push --all -u && git push --tags
Docker version 19.03.13, build 4484c46d9d
docker-compose version 1.27.4, build 40524192
Jenkins 2.249.2