GitLab CI private server PHP project deployment


#1

Hi all,

I’m just starting with CI within my PHP (Laravel) projects. I got everything up and running uptille the deployment of the project. To give you an idea of what I’m trying to achieve: Every project has two main braches: master and staging. When something changes in these two branches I want the changes to be pulled to my staging or production server IF the PHPUnit tests were succesful.

I find it hard to find an exact answer to this problem in the GitLab CI deployment, so I hope someone can help me out.

This is my .gitlab-ci.yml:

`image: php:5.6

before_script:
- bash ci/docker_install.sh > /dev/null
- apt-get update
- apt-get install -y unzip
- apt-get install -y php5-xdebug
- apt-get install -y ruby-dev
- apt-get install -y rubygems
- apt-get install -y sshpass
- gem install dpl
- curl -sS https://getcomposer.org/installer | php
- php composer.phar install

staging:
script:
- echo Staging environement.
- php -v
- cp .env.test .env
- php artisan key:generate
- php vendor/bin/phpunit --colors --debug --coverage-text
- dpl --provider=script --script=./ci/deploy_staging.sh
only:

  • staging

production:
script:
- echo Production environement.
- php -v
- cp .env.test .env
- php artisan key:generate
- php vendor/bin/phpunit --colors --debug --coverage-text
- dpl --provider=script --script=./ci/deploy_production.sh
- php -v
only:
- master`

Provisioning of the build environement seems to work ok.
Composer gets installed, PHPUnit gets executed etc. This also seems the right way to execute specific code for specific branches (the “only” statement).

I added “dpl” from the examples, but I can’t seem to find a way to get this to work:

  1. What should the deploy scripts contain (the server should do a git pull and some Laravel actions to set things up);
  2. The staging/production server have IP restrictions in a hardware firewall, should I add a GitLab IP (and which one?);
  3. How do I ensure myself the deployment to staging or production only takes place when the PHPUnit run was succesfull?

Thank you for your time and if I can help out with any extra details, please let me know.


#2

Hi, did you solve this task? If yes, than how? I have same question (w/o ip restrictions and docker).


#3

As an aside, doing an apt-get update and updating php and ruby and unzip during every build seems crazy to me.

Running your CI runner as root seems crazy to me. You are inviting a series of catastrophes.

Cheers.

W


#4

First its three back ticks for multi-line code :slight_smile:

Aside from addressing the issues the other users posted…you can use stages:, if a job fails, the pipeline stops. If you want a job to not to stop the pipeline you would use allow_failure: true.

Just define stages: like the following;

image: php:5.6

before_script:
  - bash ci/docker_install.sh > /dev/null
  - apt-get update
  - apt-get install -y unzip
  - apt-get install -y php5-xdebug
  - apt-get install -y ruby-dev
  - apt-get install -y rubygems
  - apt-get install -y sshpass
  - gem install dpl
  - curl -sS https://getcomposer.org/installer 5 | php
  - php composer.phar install

stages:
  - test
  - production

test:
  script:
    - echo Staging environment.
    - php -v
    - cp .env.test .env
    - php artisan key:generate
    - php vendor/bin/phpunit --colors --debug --coverage-text
    - dpl --provider=script --script=./ci/deploy_staging.sh
  only:
    - staging

production:
  script:
    - echo Production environment.
    - php -v
    - cp .env.test .env
    - php artisan key:generate
    - php vendor/bin/phpunit --colors --debug --coverage-text
    - dpl --provider=script --script=./ci/deploy_production.sh
    - php -v
  only:
    - master