Skip to content

Commit

Permalink
Add GitLab CI support
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainweb committed Jun 13, 2020
1 parent d2ae2d7 commit b459afc
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 3 deletions.
10 changes: 8 additions & 2 deletions axltempl/drupal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import click

from . import gitlab
from . import lando
from . import util

Expand Down Expand Up @@ -66,6 +67,7 @@
type=click.Choice(["redis", "memcache"], case_sensitive=False),
)
@click.option("--lando", "add_lando", help="Add Lando support", is_flag=True)
@click.option("--gitlab", "add_gitlab", help="Add GitLab support", is_flag=True)
@click.option(
"--force",
"-f",
Expand All @@ -82,6 +84,7 @@ def main(
no_install,
cache,
add_lando,
add_gitlab,
force,
):
"""
Expand Down Expand Up @@ -126,6 +129,10 @@ def main(
util.write_info("Adding Lando support...")
lando.generate_lando_files(name, docroot, cache)

if add_gitlab:
util.write_info("Adding GitLab support...")
gitlab.generate_gitlab_files(docroot)

os.chdir("..")
return 0

Expand Down Expand Up @@ -169,8 +176,7 @@ def generate_drupal_files(
util.copy_package_file("files/drupal/load.environment.php", "load.environment.php")
util.copy_package_file("files/drupal/.env.example", ".env.example")

os.mkdir("drush")
os.mkdir("drush/sites")
os.makedirs("drush/sites", mode=0o755, exist_ok=True)
util.copy_package_file("files/drupal/drush.yml", "drush/drush.yml")
util.copy_package_file("files/drupal/self.site.yml", "drush/sites/self.site.yml")

Expand Down
13 changes: 13 additions & 0 deletions axltempl/files/gitlab/ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

dir=$(dirname $0)
docroot={_docroot_}

set -ex

cp ${dir}/settings.local.php ${dir}/../${docroot}/sites/default/settings.local.php

sed -ri -e "s!/var/www/html/${docroot}!$CI_PROJECT_DIR/${docroot}!g" /etc/apache2/sites-available/*.conf
sed -ri -e "s!/var/www/html/${docroot}!$CI_PROJECT_DIR/${docroot}!g" /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

service apache2 start
52 changes: 52 additions & 0 deletions axltempl/files/gitlab/gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
stages:
- build
- test
- deploy

.default-pipelines: &default-pipelines
only:
- master
- tags
- merge_requests

drupal_codequality:
<<: *default-pipelines
image: hussainweb/drupalqa:php7.4
stage: test
script:
- phpcs --standard=phpcs.xml.dist --extensions=php,module,inc,install,test,profile,theme --ignore=/node_modules/ {_docroot_}/modules/custom
- phpmd {_docroot_}/modules/custom/ text phpmd.xml
tags:
- docker
except:
- schedule

drupal_tests:
<<: *default-pipelines
image: hussainweb/drupal-base:php7.4
services:
- name: bitnami/mariadb:10.4
alias: mariadb
stage: test
tags:
- docker
variables:
SITE_BASE_URL: 'http://localhost'
ALLOW_EMPTY_PASSWORD: "yes"

before_script:
- ./.gitlab/ci.sh

script:
- composer install -o

# Clearing drush cache and importing configs
- ./vendor/drush/drush/drush cr
- ./vendor/drush/drush/drush -y updatedb
- ./vendor/drush/drush/drush -y config-import

# Run PHPUnit tests
- ./vendor/bin/phpunit --testsuite unit
- ./vendor/bin/phpunit --bootstrap=./vendor/weitzman/drupal-test-traits/src/bootstrap-fast.php --configuration ./phpunit.xml --testsuite existing-site
except:
- schedule
19 changes: 19 additions & 0 deletions axltempl/files/gitlab/settings.local.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$databases['default']['default'] = [
'driver' => 'mysql',
'database' => 'drupal8',
'username' => 'drupal8',
'password' => 'drupal8',
'host' => 'mariadb'
];

/**
* Fix for Hash salt error on drush cr
*
* @ref https://github.com/drush-ops/drush/issues/1050
*
*/
$settings['hash_salt'] = 'CHANGE_THIS';

$settings['trusted_host_patterns'][] = '^localhost$';
82 changes: 82 additions & 0 deletions axltempl/gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
Add Gitlab support to a Drupal codebase
"""

import json
import os

from . import util
from . import drupal


def main():
"""
Main entrypoint for init-gitlab
"""
if not os.path.exists("composer.json"):
util.write_error("Could not find composer.json in the current directory")
return 2

composer = json.loads(util.read_file("composer.json"))
try:
scaffold_opts = composer["extra"]["drupal-scaffold"]
docroot = scaffold_opts["locations"]["web-root"].strip("/")
except KeyError:
docroot = "." if composer["name"] == "drupal/drupal" else ""

if docroot == "":
util.write_error(
"Could not determine docroot. Make sure your composer.json is valid."
)
return 3

generate_gitlab_files(docroot)
return 0


def generate_gitlab_files(docroot):
"""
Generate gitlab files in the docroot
"""

yml = util.read_package_file("files/gitlab/gitlab-ci.yml")
yml = yml.replace("{_docroot_}", docroot)
util.write_file(".gitlab-ci.yml", yml)

os.makedirs(".gitlab", mode=0o755, exist_ok=True)
util.copy_package_file(
"files/gitlab/settings.local.php", ".gitlab/settings.local.php"
)
ci_sh = util.read_package_file("files/gitlab/ci.sh")
ci_sh = ci_sh.replace("{_docroot_}", docroot)
util.write_file(".gitlab/ci.sh", ci_sh)
os.chmod(".gitlab/ci.sh", 0o755)

# Generate gitlab development override configuration.
dir_default = f"{docroot}/sites/default"
if not os.path.exists(dir_default):
util.write_error(
f'The "{dir_default}" directory is missing. '
+ "Unable to generate GitLab CI configuration files."
)
util.write_info(
"This is probably due to composer installation failure "
+ "(or you specified --no-install). "
+ "Run init-gitlab after running composer install."
)
return 2

settings_file = drupal.ensure_settings_file(docroot)
drupal.modify_settings_file(
settings_file,
"""if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
include $app_root . '/' . $site_path . '/settings.local.php';
}""",
)

util.write_info("Successfully created GitLab CI files.")
util.write_important(
"Change the database image from mariadb to your desired image in .gitlab-ci.yml."
)

return 0
9 changes: 9 additions & 0 deletions init-gitlab
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3

import sys

from axltempl import gitlab


if __name__ == "__main__":
sys.exit(gitlab.main())
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"console_scripts": [
"init-drupal = axltempl.drupal:main",
"init-lando = axltempl.lando:main",
"init-renovate = gitlabrenovate.main:scaffold",
"init-gitlab = axltempl.gitlab:main",
]
},
python_requires=">=3.6",
Expand Down

0 comments on commit b459afc

Please sign in to comment.