home about me

Deploying Laravel to fortrabbit (Gitlab CI)

Automatic deploy of Laravel projects to fortrabbit, including built assets, 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_KEY needs to be the content of a private key file, whose public key is authorized to push to fortrabbit.
  • FORTRABBIT_ORIGIN is the remote you need to push to for deployments, for example [email protected]:your-project.git.
  • SSH_HOST is the fortrabbit host, taken from the origin, for example deploy.eu2.frbit.com.
  • SSH_KNOWN_HOSTS contains the fingerprint of the host, which you can get from your .ssh/known_hosts file or by running (for example) ssh-keyscan deploy.eu2.frbit.com.
image: node:11

  - mkdir -p ~/.ssh
  - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
  - echo "Host $SSH_HOST \n IdentityFile ~/.ssh/id_rsa" > ~/.ssh/config
  - chmod 400 ~/.ssh/id_rsa
  - git config --global user.email '[email protected]'
  - git config --global user.name 'GitLab'
  - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts

    - npm install
    - npm run production
    - git checkout -b temp
    - git add public/js public/css public/mix-manifest.json --force
    - git commit -m "Build frontend files"
    - git remote add fortrabbit $FORTRABBIT_ORIGIN
    - git push fortrabbit temp:master --force
    - master