summaryrefslogtreecommitdiff
path: root/.gitlab-ci.yml
blob: 70e1d59b650c4d5ce9da392dcbe34ea6cf774c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
image: php

cache:
  paths:
    - .composer

services:
  - mariadb:10.2

variables:
  MYSQL_DATABASE: engelsystem
  MYSQL_USER: engel
  MYSQL_PASSWORD: engelsystem
  MYSQL_HOST: mariadb
  MYSQL_RANDOM_ROOT_PASSWORD: "yes"
  COMPOSER_HOME: .composer

before_script:
  # Fix permissions after gitlab messed them up
  - &before_fix_permissions |-
    find . -type f -exec chmod 644 {} \;
    find . -type d -exec chmod 755 {} \;
  # Install required Packages
  - &before_install_packages |-
    apt update -yqq
    apt install -yqq git unzip
    docker-php-ext-install pdo pdo_mysql gettext
  # Install xdebug
  - &before_install_xdebug |-
    pecl install xdebug
    docker-php-ext-enable xdebug
  # MySQL DB
  - &before_setup_mysql |-
    apt install -yqq mariadb-client
    mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/install.sql
    mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/update.sql
  # Install Composer
  - &before_install_composer |-
    curl -sS https://getcomposer.org/installer | php -- --no-ansi --install-dir /usr/local/bin/ --filename composer
    composer --no-ansi install
  # Install Node.js and Yarn
  - &before_install_yarn |-
    apt -yqq install gnupg2 apt-transport-https
    curl -sL https://deb.nodesource.com/setup_8.x | bash -
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
    apt -yqq update && apt -yqq install nodejs yarn
    yarn install
    yarn build

.test_template: &test_definition
  stage: test
  artifacts:
    name: "${CI_JOB_NAME}_${CI_JOB_ID}"
    expire_in: 1 week
    paths:
      - ./coverage/
  coverage: '/^\s*Lines:\s*(\d+(?:\.\d+)?%)/'
  script: vendor/bin/phpunit --colors=never --coverage-text --coverage-html ./coverage/

test:7.0:
  <<: *test_definition
  image: php:7.0

test:7.1:
  <<: *test_definition
  image: php:7.1

.deploy_template: &deploy_definition
  services: []
  stage: deploy
  only:
    - master
  before_script:
    - *before_fix_permissions
    - *before_install_packages
    - *before_install_composer
    - *before_install_yarn

.deploy_template_script:
  # Configure SSH
  - &deployment_ssh |-
    mkdir -p ~/.ssh
    echo "$SSH_PRIVATE_KEY" | sed -e 's/\r//g' > ~/.ssh/id_ed25519
    chmod 600 ~/.ssh/id_ed25519

  # Install project and dependencies
  - &deployment_dependencies |-
    chmod +x ./bin/deploy.sh
    apt update && apt install -yqq rsync openssh-client
    composer --no-ansi install --no-dev
    composer --no-ansi dump-autoload --optimize

build_release_file:
  <<: *deploy_definition
  artifacts:
    name: "release_${CI_COMMIT_REF_SLUG}_${CI_JOB_ID}_${CI_COMMIT_SHA}"
    expire_in: 1 week
    paths:
      - ./release/
  script:
    - *deployment_dependencies
    - rsync -vAax --exclude '.git*' --exclude .composer/ --exclude coverage/ --exclude node_modules/ --exclude release/ ./ release/

deploy_staging:
  <<: *deploy_definition
  environment:
    name: staging
  script:
    # Check if deployment variables where set
    - |-
      if [ -z "${SSH_PRIVATE_KEY}" ] || [ -z "${STAGING_REMOTE}" ] || [ -z "${STAGING_REMOTE_PATH}" ]; then
        echo "Skipping deployment";
        exit
      fi
    - *deployment_ssh
    - *deployment_dependencies
    # Deploy to server
    - ./bin/deploy.sh -r "${STAGING_REMOTE}" -p "${STAGING_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}"

deploy_production:
  <<: *deploy_definition
  environment:
    name: production
  when: manual
  script:
    # Check if deployment variables where set
    - |-
      if [ -z "${SSH_PRIVATE_KEY}" ] || [ -z "${PRODUCTION_REMOTE}" ] || [ -z "${PRODUCTION_REMOTE_PATH}" ]; then
        echo "Skipping deployment";
        exit
      fi
    - *deployment_ssh
    - *deployment_dependencies
    # Deploy to server
    - ./bin/deploy.sh -r "${PRODUCTION_REMOTE}" -p "${PRODUCTION_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}"