Skip to content

Instantly share code, notes, and snippets.

@htuscher
Last active December 3, 2019 14:44
Show Gist options
  • Save htuscher/4a48d31ce79d0e054ed6e067284ac34f to your computer and use it in GitHub Desktop.
Save htuscher/4a48d31ce79d0e054ed6e067284ac34f to your computer and use it in GitHub Desktop.
Root360 Deployment Shopware Gitlab CI

Repository structure

.
├── README.md
├── _dev-ops
│   └── deploy
|       ├── install.sh
│       └── post-hook.d
│           └── 10_upgrade.sh
├── autoload.php
├── bin
├── composer.json
├── composer.lock
├── config.deploy.php
├── config.php
├── engine
├── eula.txt
├── eula_en.txt
├── favicon.ico
├── files
├── googlec9307798f1e0189d.html
├── license.txt
├── media
├── recovery
├── shopware.php
├── templates
├── themes
├── var
├── vendor
└── web

config.deploy.php

<?php

# showare fix to replace remote_addr variable to match incorrect handling for x-forwarded-for (e.g order creation, request->getClientIp(), ...)
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && $_SERVER["HTTP_X_FORWARDED_FOR"] != null) {
    if (stripos($_SERVER["HTTP_X_FORWARDED_FOR"], ",")) {
        $_SERVER["REMOTE_ADDR"] = substr($_SERVER["HTTP_X_FORWARDED_FOR"], 0,
            (int)stripos($_SERVER["HTTP_X_FORWARDED_FOR"], ","));
    } else {
        $_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
}

$config = [
    'db' => [
        'host'     => '%DATABASE_HOST%',
        'port'     => '3306',
        'username' => '%DATABASE_USER%',
        'password' => '%DATABASE_PASSWORD%',
        'dbname'   => '%DATABASE_NAME%',
    ],
];

return $config;

Gitlab-CI

Environment variables (secrets from Gitlab):

  • SSH_PRIVATE_KEY
  • S3_ACCESS_KEY
  • S3_SECRET_KEY
deploy:live:
  image: 1drop/php-utils:7.3
  stage: deploy
  before_script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - composer install --no-progress --no-interaction -o -a --no-scripts
  environment:
    name: Production
    url: https://www.livedomain.de/
  variables:
    LIVE_SERVER: some-ip
    LIVE_USER: some-ssh-user
    S3_CMD_VER: 2.0.2
    S3_BUCKET: some-s3-bucket
  only:
    - tags
    - /^[0-9]+\.[0-9]+\.[0-9]+$/
  except:
    - branches
  script:
    - apt-install python-dateutil
    - wget -O /tmp/s3cmd.tar.gz https://github.com/s3tools/s3cmd/releases/download/v${S3_CMD_VER}/s3cmd-${S3_CMD_VER}.tar.gz
    - tar xzf /tmp/s3cmd.tar.gz
    - mv ./s3cmd-${S3_CMD_VER}/* /usr/local/bin/
    - mkdir -p /tmp/release/public
    - mv _dev-ops/deploy/* /tmp/release/
    - rm -rf ./s3cmd-${S3_CMD_VER}/ _dev-ops .gitlab-ci.yml .git files media web
    - rsync -r . /tmp/release/public/
    - tar czf /tmp/shopware-${CI_COMMIT_TAG}.tgz -C /tmp/release/ .
    - s3cmd --access_key=${S3_ACCESS_KEY} --secret_key=${S3_SECRET_KEY} put /tmp/shopware-${CI_COMMIT_TAG}.tgz s3://${S3_BUCKET}/releases/shopware-${CI_COMMIT_TAG}.tgz
    #- ssh -t ${LIVE_USER}@${LIVE_SERVER} "r3 deploy --post-hook -r shopware --rev ${CI_COMMIT_TAG}"

This builds the folder structure:

.
├── install.sh
├── post-hook.d
│   └── 10_upgrade.sh
└── public
    ├── autoload.php
    ├── bin
    └── ...

Install.sh

The install.sh is executed during the internal build process of Root360. It runs in /srv/something to prepare the artifact that's about to be served to the instances.

#!/usr/bin/env bash

# shellcheck disable=SC2164
cd public

sed "s#%DATABASE_USER%#${DATABASE_USER}#g;
      s#%DATABASE_NAME%#${DATABASE_NAME}#g;
      s#%DATABASE_PASSWORD%#${DATABASE_PASSWORD}#g;
      s#%DATABASE_HOST%#${DATABASE_HOST}#g;
      s#%ENV%#${ENV}#g;
      " config.deploy.php > config.php || exit $?

# install CRON if role "backend" is installed
if [[ "${ROLE}" == "backend" ]]
then
  # Register CRONs
  echo "*/15 * * * * date >> /var/log/application/cron.log && cd /var/www/${ROLE}/public && php bin/console sw:cron:run 2>&1 >> /var/log/application/cron.log" >> project-crontab
  register-log -k "/var/log/application/cron.log"
  crontab project-crontab || exit $?
  rm project-crontab
fi

register-log -k "/var/www/${ROLE}/public/var/log/*.log"

Post deploy hook

The post deploy hook is executed on the bastion host, therefore you must SSH into the instances yourself.

10_upgrade.sh

#!/bin/bash

source /usr/local/lib/helper.sh

function usage() {
  echo "${SCRIPT} [-r role] [-p project] [-e environment] [-q|-d] [-h]"
  echo "  -h                  : print this help"
  echo "  -r role             : server role"
  echo "  -p project          : project name"
  echo "  -e environment      : environment name"
  exit 1
}

# iterate options
while getopts ':hr:p:e:' opt; do
  case "${opt}" in
    "r")
      role="${OPTARG}"
      ;;
    "p")
      project="${OPTARG}"
      ;;
    "e")
      environment="${OPTARG}"
      ;;
    "h")
      usage
      ;;
    ":")
      log error "Missing argument for option ${OPTARG}"
      usage
      ;;
    *)
      log error "Unknown option ${opt}"
      usage
      ;;
  esac
done

# iterate all servers with given role in current project and run a command
for target in $(get-instances-by-role "${role}" --output text | awk -F ' ' '{print$2}' | grep -v 'ip'); do
  if [[ "${role}" == "backend" ]]
  then
    ssh -t "${target}" "cd /var/www/${role}/public; php bin/console sw:cache:clear"
    ssh -t "${target}" "cd /var/www/${role}/public; php bin/console sw:plugin:refresh"
    ssh -t "${target}" "cd /var/www/${role}/public; php bin/console sw:plugin:update --batch=active"
    ssh -t "${target}" "cd /var/www/${role}/public; php bin/console sw:cache:clear"
    ssh -t "${target}" "cd /var/www/${role}/public; php bin/console sw:generate:attributes"
    ssh -t "${target}" "cd /var/www/${role}/public; php bin/console sw:theme:cache:generate"
  fi
  if [[ "${role}" == "web" ]]
  then
    ssh -t "${target}" "cd /var/www/${role}/public; php bin/console sw:cache:clear"
    ssh -t "${target}" "cd /var/www/${role}/public; php bin/console sw:generate:attributes"
  fi
done

# do some more stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment