diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1995cfb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +.idea/ + +.DS_Store diff --git a/defaults/main.yml b/defaults/main.yml index c2f58ec..2a0e81c 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -7,4 +7,7 @@ npm_install: true delete_env_after_test: true bitbucket_consumer_key: "" -bitbucket_consumer_secret: "" \ No newline at end of file +bitbucket_consumer_secret: "" + +minimum_laravel_version: "5.0.0" +minimum_craftcms_version: "3.0.0" \ No newline at end of file diff --git a/tasks/craftcms.yml b/tasks/craftcms.yml new file mode 100644 index 0000000..dfb0e8a --- /dev/null +++ b/tasks/craftcms.yml @@ -0,0 +1,31 @@ +- name: Print Craft CMS framework version + debug: + var: fact_craftcms_version + verbosity: 1 + +- name: Check for outdated Craft CMS framework + debug: + msg: Craft CMS version ({{ fact_craftcms_version }}) is too old to deploy. Please update framework to v{{ minimum_craftcms_version }} or request a Platform override. + verbosity: 0 + when: fact_craftcms_version | regex_search('\d+[.]?\d*[.]?\d*') is not version(minimum_craftcms_version, '>=', strict=true) + +- include_tasks: platform-override.yml + when: fact_craftcms_version | regex_search('\d+[.]?\d*[.]?\d*') is not version(minimum_craftcms_version, '>=', strict=true) + +- name: Check for codeception.yml + stat: + path: "{{ app_directory }}/codeception.yml" + register: codeception_conf + failed_when: not codeception_conf.stat.exists + +- name: Check for testing .env + stat: + path: "{{ app_directory }}/tests/.env" + register: codeception_env + failed_when: not codeception_env.stat.exists + +- name: Install mysql-client + apt: + name: mysql-client + state: present + install_recommends: no diff --git a/tasks/laravel.yml b/tasks/laravel.yml new file mode 100644 index 0000000..b2e7406 --- /dev/null +++ b/tasks/laravel.yml @@ -0,0 +1,20 @@ +- name: Print Laravel framework version + debug: + var: fact_laravel_version + verbosity: 1 + +- name: Check for outdated Laravel framework + debug: + msg: Laravel version ({{ fact_laravel_version }}) is too old to deploy. Please update laravel framework to v{{ minimum_laravel_version }} or request a Platform override. + verbosity: 0 + when: fact_laravel_version | regex_search('\d+[.]?\d*[.]?\d*') is not version(minimum_laravel_version, '>=', strict=true) + +- include_tasks: platform-override.yml + when: fact_laravel_version | regex_search('\d+[.]?\d*[.]?\d*') is not version(minimum_laravel_version, '>=', strict=true) + +- name: Check for .env.testing + stat: + path: "{{ app_directory }}/.env.testing" + register: testing_env + failed_when: not testing_env.stat.exists + \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index a2970bc..ea8a28d 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,3 +1,8 @@ +- name: Install ACL package + apt: + name: acl + state: present + - name: Install application. apt: deb: "{{ app_deb_package }}" @@ -8,76 +13,91 @@ comment: App user system: yes +- name: Chown Apps directory + file: + dest: "{{ app_directory }}" + owner: "{{ app_user }}" + group: "{{ app_group }}" + recurse: yes + +- name: Chown npm cache directory + file: + path: /var/www/ + state: directory + recurse: yes + owner: "{{ app_user }}" + group: "{{ app_group }}" + mode: '0755' + - name: Check if composer.json exists stat: path: "{{ app_directory }}/composer.json" register: run_composer -- include: env.yml - -- name: Set bitbucket auth for private composer packages. - shell: "composer config --global --auth bitbucket-oauth.bitbucket.org {{ bitbucket_consumer_key }} {{ bitbucket_consumer_secret }}" - when: - - bitbucket_consumer_key != "" - - bitbucket_consumer_secret != "" - - run_composer.stat.exists - -- name: Composer Install. - composer: - command: install - no_dev: no - working_dir: "{{ app_directory }}" - environment: - COMPOSER_ALLOW_SUPERUSER: "1" - when: run_composer.stat.exists - -- name: Check if npm package.json exists - stat: - path: "{{ app_directory }}/package.json" - register: run_npm - -- name: npm install - command: "npm install --unsafe-perm" - args: - chdir: "{{ app_directory }}" - when: npm_install and run_npm.stat.exists - -- name: npm run prod - command: "npm run prod" - args: - chdir: "{{ app_directory }}" - when: npm_install and run_npm.stat.exists - -- include: testing.yml -- include: behat.yml - -- name: Composer install to remove dev dependecies. - composer: - command: install - working_dir: "{{ app_directory }}" - environment: - COMPOSER_ALLOW_SUPERUSER: "1" - when: run_composer.stat.exists - -- include: env.yml - -- name: Key Generate. - shell: 'php artisan key:generate' - args: - chdir: "{{ app_directory }}" - when: laravel_app and app_env.APP_KEY is not defined +- name: Validate PHP framework + include_tasks: validation.yml -- name: Chown Apps directory - file: - dest: "{{ app_directory }}" - owner: "{{ app_user }}" - group: "{{ app_group }}" - recurse: yes +- become: true + become_user: "{{ app_user }}" + block: + - name: Set bitbucket auth for private composer packages. + shell: "composer config --global --auth bitbucket-oauth.bitbucket.org {{ bitbucket_consumer_key }} {{ bitbucket_consumer_secret }}" + when: + - bitbucket_consumer_key != "" + - bitbucket_consumer_secret != "" + - run_composer.stat.exists + + - name: Composer Install. + composer: + command: install + no_dev: no + working_dir: "{{ app_directory }}" + when: run_composer.stat.exists + + - include: testing.yml + + - name: Check if npm package.json exists + stat: + path: "{{ app_directory }}/package.json" + register: run_npm + + - name: npm install + command: "npm install" + args: + chdir: "{{ app_directory }}" + when: npm_install and run_npm.stat.exists + + - name: npm run prod + command: "npm run prod" + args: + chdir: "{{ app_directory }}" + when: npm_install and run_npm.stat.exists + + - include: behat.yml + + - name: Composer install to remove dev dependecies. + composer: + command: install + working_dir: "{{ app_directory }}" + when: run_composer.stat.exists + + - include: env.yml + + - name: Craft Security Key Generate. + shell: 'php craft setup/security-key' + args: + chdir: "{{ app_directory }}" + when: fact_craftcms_version | length > 0 and app_env.APP_KEY is not defined -- name: Configure laravel cronjob - cron: - name: Laravel Cron - user: "{{ app_user }}" - job: "cd {{ app_directory }} && php artisan schedule:run >> /dev/null 2>&1" - when: laravel_app + - name: Artisan Key Generate. + shell: 'php artisan key:generate' + args: + chdir: "{{ app_directory }}" + when: fact_laravel_version | length > 0 and app_env.APP_KEY is not defined + - name: Configure laravel cronjob + cron: + name: Laravel Cron + user: "{{ app_user }}" + job: "cd {{ app_directory }} && php artisan schedule:run >> /dev/null 2>&1" + when: fact_laravel_version | length > 0 diff --git a/tasks/platform-override.yml b/tasks/platform-override.yml new file mode 100644 index 0000000..d3dcb2c --- /dev/null +++ b/tasks/platform-override.yml @@ -0,0 +1,12 @@ +- name: Awaiting Platform override... + wait_for: + path: /tmp/platform + search_regex: ok + state: present + timeout: 3600 + msg: Platform override timeout. + +- name: Platform override post actions + file: + path: /tmp/platform + state: absent \ No newline at end of file diff --git a/tasks/testing.yml b/tasks/testing.yml index baf3f14..2de52ee 100644 --- a/tasks/testing.yml +++ b/tasks/testing.yml @@ -1,29 +1,11 @@ -- name: Check testing env - stat: - path: "{{ app_directory }}/.env.testing" - register: run_tests - -- name: Check testing directory - stat: - path: "{{ app_directory }}/tests" - register: has_tests - -- name: Run PHPUnit tests. - block: - - name: Copy testing env file - copy: - src: "{{ app_directory }}/.env.testing" - dest: "{{ app_directory }}/.env" - - - name: Exec PHPUnit - shell: './vendor/bin/phpunit' - args: - chdir: "{{ app_directory }}" - when: run_tests.stat.exists - - - name: Remove testing env. - file: - path: "{{ app_directory }}/.env" - state: absent - when: delete_env_after_test - when: run_tests.stat.exists and has_tests.stat.exists +- name: Run PHPUnit + shell: './vendor/bin/phpunit' + args: + chdir: "{{ app_directory }}" + when: fact_laravel_version | length > 0 + +- name: Run Codeception + shell: './vendor/bin/codecept run Unit' + args: + chdir: "{{ app_directory }}" + when: fact_craftcms_version | length > 0 \ No newline at end of file diff --git a/tasks/validation.yml b/tasks/validation.yml new file mode 100644 index 0000000..4692995 --- /dev/null +++ b/tasks/validation.yml @@ -0,0 +1,32 @@ +- name: Load composer file + shell: "cat {{ app_directory }}/composer.json" + register: composer + +- name: Save composer output + set_fact: + jsondata: "{{ composer.stdout | from_json }}" + +- name: Check for Laravel framework + set_fact: + fact_laravel_version: "{{ jsondata | json_query(jmesquery) }}" + vars: + jmesquery: 'require."laravel/framework"' + +- name: Check for Craft CMS framework + set_fact: + fact_craftcms_version: "{{ jsondata | json_query(jmesquery) }}" + vars: + jmesquery: 'require."craftcms/cms"' + +- name: Check for unknown framework + fail: + msg: Could not recognise PHP framework. + when: fact_laravel_version | length == 0 and fact_craftcms_version | length == 0 + +- name: Include Laravel tasks + include_tasks: laravel.yml + when: fact_laravel_version | length > 0 + +- name: Include Craft CMS tasks + include_tasks: craftcms.yml + when: fact_craftcms_version | length > 0