Skip to content

Instantly share code, notes, and snippets.

@hyperized
Last active December 24, 2019 10:18
Show Gist options
  • Save hyperized/9057fb789f34e4bb9d0ebbda64162bc4 to your computer and use it in GitHub Desktop.
Save hyperized/9057fb789f34e4bb9d0ebbda64162bc4 to your computer and use it in GitHub Desktop.
Example of Laravel deployment over SSH via Gitlab CI
---
stages:
- pull
- npm
- push
- permissions
- composer
- migrations
- optimize
variables:
MYPATH: "/home/site/"
MYSSH: "ssh -t $SSH_USER@$SSH_IP"
.ssh: &ssh
image: hyperized/openssh-client
before_script:
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
only:
refs:
- master
.tags: &tags
tags:
- gcp
pull_on_server:
stage: pull
<<: *ssh
script:
- $MYSSH "cd $MYPATH && git reset --hard"
- $MYSSH "cd $MYPATH && git pull $CI_REPOSITORY_URL"
npm:
stage: npm
image: node
script:
- npm install
- npm run production
artifacts:
paths:
- public/css
- public/js
expire_in: 1 hour
push_to_server:
stage: push
<<: *ssh
script:
- scp -P 50 -rv public/css $SSH_USER@$SSH_IP:$MYPATH/public/
- scp -P 50 -rv public/js $SSH_USER@$SSH_IP:$MYPATH/public/
permissions:
stage: permissions
<<: *ssh
script:
- $MYSSH "sudo chown -R site:www-data $MYPATH"
- $MYSSH "sudo find $MYPATH -type f -exec chmod 664 {} \;"
- $MYSSH "sudo find $MYPATH -type d -exec chmod 775 {} \;"
- $MYSSH "sudo chgrp -R www-data $MYPATH/storage $MYPATH/bootstrap/cache"
- $MYSSH "sudo chmod -R ug+rwx $MYPATH/storage $MYPATH/bootstrap/cache"
<<: *tags
migrations:
stage: migrations
<<: *ssh
script:
- $MYSSH "cd $MYPATH && php artisan migrate"
- $MYSSH "redis-cli FLUSHALL"
<<: *tags
composer:
stage: composer
<<: *ssh
script:
- $MYSSH "cd $MYPATH && composer install --no-progress --no-suggest --optimize-autoloader --no-dev --ignore-platform-reqs"
<<: *tags
optimize:
stage: optimize
<<: *ssh
script:
- $MYSSH "cd $MYPATH && php artisan nova:publish --force"
- $MYSSH "cd $MYPATH && php artisan view:cache"
- $MYSSH "cd $MYPATH && php artisan config:cache"
- $MYSSH "cd $MYPATH && php artisan event:cache"
- $MYSSH "cd $MYPATH && php artisan route:cache"
<<: *tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment