gitlab-auto-deploy

Gitlab auto-deploy

This is a basic guide explaining how to configure auto-deploy in Gitlab to Heroku using Laravel and Vuejs project.

Configuring API_KEY

Having a Heroku account with a deployed project (see how to deploy a project to heroku here).

First, we need the Heroku API_KEY, you can find it in Manage Account -> Account -> API Key. We also need the app name, you can find it by accessing Home page and then App -> Settings -> App Name.

Now, we gonna give access to Heroku to our project, so it can do the deploy automatically. Access the project page in Gitlab, then Settings -> CI/CD. Now, expand the Variables section and create the Heroku API Key:

# HEROKU_API_KEY
Key:
HEROKU_API_KEY

Value:
PAST HERE THE HEROKU API KEY

About the options in the bottom, check the “Mask variable” option and uncheck the “Protect variable” option. Then click in Add Variable and its done.

Just do the same thing with Heroku app name and all right:

# HEROKU_APP
Key:
HEROKU_APP

Value:
PAST HERE THE HEROKU APP NAME

Creating a .gitlab-ci.yml file

Like a docker-compose.yml file, in .gitlab-ci.yml we also need to say which image we will use, and the structure its iqual.

The .gitlab-ci.yml file in this repo is more simple, so you can use it to deploy both the backend and frontend.

Example of .gitlab-ci.yml to php:

image: php:7.3
before_script:
    - apt-get update -qq
    - apt-get install -y -qq git
    - apt-get install -y software-properties-common
    - apt-get update -qq
    - apt-get install -y libxml2-dev
    - apt-get update -qq
    - apt-get install libsodium-dev
    - apt-get update -qq
    - apt-get install -y libpng-dev
    - apt-get install -y libzip-dev
    - docker-php-ext-install pdo_mysql
    - docker-php-ext-install sodium
    - docker-php-ext-install gd
    - docker-php-ext-install zip
    - docker-php-ext-install mbstring

phpunit tests:
    stage: test
    script:
    - apt-get update
    - apt-get install curl
    - curl -s https://getcomposer.org/installer | php
    - mv composer.phar /usr/local/bin/composer
    - composer install
    - cp .env.example .env
    - php artisan key:generate
    - vendor/bin/phpunit tests
    only:
        - develop

deploy to heroku:
    stage: deploy
    script:
    - apt-get update -qq
    - apt-get install -y -qq git
    - apt-get install -y ruby
    - gem install dpl
    - dpl --provider=heroku --app=$HEROKU_APP --api-key=$HEROKU_API_KEY
    only:
        - develop

The description of each label of the php yml:

To Nuxtjs/Vuejs:

stages:
    - build
    - deploy

build:
    stage: build
    image: node:13
    script:
    - npm install --progress=false
    - npm run build

deploy to heroku:
    stage: deploy
    image: "ruby:2.5"
    script:
    # install dpl:
    - apt-get update -qy
    - apt-get install rubygems ruby-dev -y
    - gem install dpl
    # deploy to heroku:
    - dpl --provider=heroku --app=$HEROKU_APP --api-key=$HEROKU_API_KEY --skip_cleanupenvironment

The description of each label of the Nuxtjs/Vuejs yml:

You will also edit the Procfile file, so Heroku can migrate and install passport again in every deploy automatically, just replace your file by the one in this repo or just paste this:

web: vendor/bin/heroku-php-apache2 public/
release: php artisan migrate:fresh --seed --force && php artisan passport:install

Now, just commit this to Gitlab:

$ git add .
$ git commit -m ".gitlab-ci.yml and Procfile added - Auto deploy"
$ git push

Checking if success

To check if the auto deploy was right, just go to the Gitlab project page and the, in the left panel, click o CI/CD. In this page you can see the deploy process status.

Leonardo Zanotti