Automatic deploy of Laravel projects to fortrabbit, including built assets and database migrations, using Gitlab CI.
The snippet below assumes a standard Laravel setup. Gitlab CI builds the assets and uses
git add --force to add the gitignored output files, commits it to a temporary branch and force pushes to the fortrabbit remote, triggering a deploy.
You will need to populate some environment variables:
SSH_PRIVATE_KEYneeds to be the content of a private key file, whose public key is authorized to push to fortrabbit.
FORTRABBIT_APPis the user name assigned to your fortrabbit app, for example
laravel-foobar. This is usually the same as your app name and can be found under SFTP > SFTP User, or as part of the Clone URL.
FORTRABBIT_HOSTis the host name assigned to your fortrabbit host, for example
deploy.eu2.frbit.com. This can be found under SFTP > SFTP Server, or as part of the Clone URL.
Set up those three environment variables, and use this
.gitlab-ci.yml, adjusted to your liking:
image: node:latest # use the most recent node docker image cache: # cache the npm data between different jobs key: $CI_COMMIT_REF_SLUG # key the cache by the current branch paths: - .npm/ # only folders in the current project directory can be cached before_script: - npm ci --cache .npm --prefer-offline # make npm use a cache folder in the current project directory instead of home - mkdir -p ~/.ssh # create ssh config folder - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa # save the provided private key - echo "Host $FORTRABBIT_HOST \n IdentityFile ~/.ssh/id_rsa" > ~/.ssh/config # use provided ssh key for connection to fortrabbit - chmod 400 ~/.ssh/id_rsa # SSH client will complain if key file has wrong permissions - git config --global user.email '[email protected]' - git config --global user.name 'GitLab' - ssh-keyscan $FORTRABBIT_HOST >> ~/.ssh/known_hosts # fetch the current public key for the fortrabbit server, to prevent interactive confirmation prompt push_to_fortrabbit: script: - npm install # install dependencies - npm run production # build files - git checkout -b temp # check out new branch - git add public/js public/css public/mix-manifest.json --force # force staging of gitignored compiled files - git commit -m "Commit built files" # put into new commit - git remote add fortrabbit "[email protected]$FORTRABBIT_HOST:$FORTRABBIT_APP.git" # add fortrabbit as remote - git push fortrabbit temp:master --force # push new files to fortrabbit - this will trigger a composer update - ssh "[email protected]$FORTRABBIT_HOST" "php artisan migrate --force" # run migrations only: - master # only run for commits on the master branch