diff --git a/cmf/all/.cikit/ci/post-deploy.yml b/cmf/all/.cikit/ci/post-deploy.yml new file mode 100644 index 00000000..f3bc663e --- /dev/null +++ b/cmf/all/.cikit/ci/post-deploy.yml @@ -0,0 +1,19 @@ +# This is a playbook that runs after a successful build of the application. +# To do something before the build create the "pre-deploy.yml" that'll have +# the same set of input variables but will run early, before any other tasks. +# +# Available variables: +# - dist: an absolute path to the project's destination; +# - env: a name of the environment being built; +# - site-url: a URL of a website being built; +# - build-id: an ID of CI build; +# - workspace: an absolute path to the sandbox directory on CI server. +--- +- hosts: localhost + gather_facts: no + connection: local + become: yes + + tasks: + - name: Move reports to the website directory + shell: "cp -r {{ workspace }}/docroot/reports/ {{ dist }}/docroot" diff --git a/scripts/roles/cikit-gitlab-ci/templates/.gitlab-ci.yml.j2 b/scripts/roles/cikit-gitlab-ci/templates/.gitlab-ci.yml.j2 index 3fc5e113..63041481 100644 --- a/scripts/roles/cikit-gitlab-ci/templates/.gitlab-ci.yml.j2 +++ b/scripts/roles/cikit-gitlab-ci/templates/.gitlab-ci.yml.j2 @@ -44,14 +44,15 @@ build/run: - export BUILD_ACTIONS="$(php -r "echo json_encode(array_map('trim', array_filter(explode(PHP_EOL, '$(git log -n1 --pretty=%B | awk -vRS="]" -vFS="[" '{print $2}')'))));")" # Dump all available variables for debugging purposes. - env + # @todo Unify hooks triggers as it's done for Jenkins. + - [ -f "${CI_PROJECT_DIR}/.cikit/ci/pre-deploy.yml" ] && cikit "${CI_PROJECT_DIR}/.cikit/ci/pre-deploy.yml" --site-url="${CI_ENVIRONMENT_URL}" --build-id="${CI_ENVIRONMENT_NAME}" --workspace="${CI_PROJECT_DIR}" --dist="${DESTINATION}" # Reinstall a project. - CIKIT_PROJECT_DIR="${CI_PROJECT_DIR}" cikit reinstall --actions="${BUILD_ACTIONS}" --site-url="${CI_ENVIRONMENT_URL}" --build-id="${CI_ENVIRONMENT_NAME}" --workspace="${CI_PROJECT_DIR}" # Copy codebase to the "web" directory. - sudo rsync -ra --delete --chown=www-data:www-data ./ "${DESTINATION}" # Run sniffers. - CIKIT_PROJECT_DIR="${DESTINATION}" cikit sniffers --site-url="${CI_ENVIRONMENT_URL}" --build-id="${CI_ENVIRONMENT_NAME}" --workspace="${DESTINATION}" - # Copy artifacts. - - cp -r "${DESTINATION}/docroot/reports/" "${CI_PROJECT_DIR}/artifacts/" + - [ -f "${CI_PROJECT_DIR}/.cikit/ci/post-deploy.yml" ] && cikit "${CI_PROJECT_DIR}/.cikit/ci/post-deploy.yml" --site-url="${CI_ENVIRONMENT_URL}" --build-id="${CI_ENVIRONMENT_NAME}" --workspace="${CI_PROJECT_DIR}" --dist="${DESTINATION}" artifacts: paths: - artifacts/ diff --git a/scripts/roles/cikit-jenkins/templates/jobs/builder.xml.j2 b/scripts/roles/cikit-jenkins/templates/jobs/builder.xml.j2 index 71abca39..d081cad0 100644 --- a/scripts/roles/cikit-jenkins/templates/jobs/builder.xml.j2 +++ b/scripts/roles/cikit-jenkins/templates/jobs/builder.xml.j2 @@ -81,47 +81,49 @@ Pull - do not reinstall an application and just grab latest changes from reposit false - #!/usr/bin/env bash -e + #!/usr/bin/env bash -export PYTHONUNBUFFERED=1 -export PROJECT="$(echo ${JOB_NAME//_BUILDER/} | tr '[:upper:]' '[:lower:]')" +set -e + +export PROJECT="$(echo "${JOB_NAME//_BUILDER/}" | tr '[:upper:]' '[:lower:]')" export BUILD_NAME="${PROJECT}_${BUILD_ENV}" -export SITE_URL="https://${BUILD_NAME}.$(php -r "echo parse_url('${JOB_URL}')['host'];")" -export DESTINATION="/var/www/${BUILD_NAME}" -export CIKIT_PROJECT_DIR="${WORKSPACE}" +export SITE_URL="https://$BUILD_NAME.$(php -r "echo parse_url('${JOB_URL}')['host'];")" +export DESTINATION="/var/www/$BUILD_NAME" +export CIKIT_PROJECT_DIR="$WORKSPACE" +export PYTHONUNBUFFERED=1 export ANSIBLE_VERBOSITY=2 -cikit reinstall \ - --env="${BUILD_ENV}" \ - --site-url="${SITE_URL}" \ - --build-id="${BUILD_NAME}" \ - --workspace="${WORKSPACE}" \ - --reinstall-mode="${BUILD_MODE}" +ARGS=( + "--env=$BUILD_ENV" + "--site-url=$SITE_URL" + "--build-id=$BUILD_NAME" + "--workspace=$WORKSPACE" +) + +deploy_hook() { + local HOOK_PLAYBOOK="$WORKSPACE/.cikit/ci/$1.yml" + + if [ -f "$HOOK_PLAYBOOK" ]; then + cikit "$HOOK_PLAYBOOK" "${ARGS[@]}" --dist="$DESTINATION" + fi +} + +deploy_hook pre-deploy +cikit reinstall "${ARGS[@]}" --reinstall-mode="$BUILD_MODE" # Copy codebase to directory, accessible from web. -sudo rsync --delete -ra ./ "${DESTINATION}/" +sudo rsync --delete -ra ./ "$DESTINATION/" sudo chown -R www-data:jenkins $_ if ${RUN_SNIFFERS}; then - cikit sniffers \ - --env="${BUILD_ENV}" \ - --site-url="${SITE_URL}" \ - --build-id="${BUILD_NAME}" \ - --workspace="${WORKSPACE}" + cikit sniffers "${ARGS[@]}" fi if ${RUN_TESTS}; then - cikit tests \ - --run \ - --headless \ - --env=${BUILD_ENV} \ - --site-url=${SITE_URL} \ - --build-id=${BUILD_NAME} \ - --workspace=${WORKSPACE} + cikit tests "${ARGS[@]}" --run --headless fi -# Move reports to the website directory. -sudo cp -r ./docroot/reports/ "${DESTINATION}/docroot/" +deploy_hook post-deploy sudo chown -R jenkins:jenkins ./ diff --git a/scripts/roles/cikit-jenkins/templates/jobs/pr_builder.xml.j2 b/scripts/roles/cikit-jenkins/templates/jobs/pr_builder.xml.j2 index f74e4f73..a854f6e2 100644 --- a/scripts/roles/cikit-jenkins/templates/jobs/pr_builder.xml.j2 +++ b/scripts/roles/cikit-jenkins/templates/jobs/pr_builder.xml.j2 @@ -93,35 +93,46 @@ false - #!/usr/bin/env bash -e + #!/usr/bin/env bash -export PROJECT=$(echo ${JOB_NAME//_PR_BUILDER/} | tr '[:upper:]' '[:lower:]') +set -e + +export PROJECT="$(echo "${JOB_NAME//_BUILDER/}" | tr '[:upper:]' '[:lower:]')" export BUILD_NAME="${PROJECT}_build_${BUILD_NUMBER}" -export SITE_URL="https://${BUILD_NAME}.$(php -r "echo parse_url('${JOB_URL}')['host'];")" -export DESTINATION="/var/www/${BUILD_NAME}" -export CIKIT_PROJECT_DIR="${WORKSPACE}" +export SITE_URL="https://$BUILD_NAME.$(php -r "echo parse_url('${JOB_URL}')['host'];")" +export DESTINATION="/var/www/$BUILD_NAME" +export CIKIT_PROJECT_DIR="$WORKSPACE" +export PYTHONUNBUFFERED=1 export ANSIBLE_VERBOSITY=2 export BUILD_ACTIONS=$(php -r "echo json_encode(array_map('trim', array_filter( explode(PHP_EOL, '$(git log --format=%B -n1 ${ghprbActualCommit} | awk -vRS="]" -vFS="[" '{print $2}')') )));") -cikit reinstall \ - --actions="${BUILD_ACTIONS}" \ - --site-url="${SITE_URL}" \ - --build-id="${BUILD_NAME}" \ - --workspace="${WORKSPACE}" +ARGS=( + "--site-url=$SITE_URL" + "--build-id=$BUILD_NAME" + "--workspace=$WORKSPACE" +) + +deploy_hook() { + local HOOK_PLAYBOOK="$WORKSPACE/.cikit/ci/$1.yml" + + if [ -f "$HOOK_PLAYBOOK" ]; then + cikit "$HOOK_PLAYBOOK" "${ARGS[@]}" --dist="$DESTINATION" + fi +} + +deploy_hook pre-deploy +cikit reinstall "${ARGS[@]}" --actions="$BUILD_ACTIONS" # Copy codebase to directory, accessible from web. -sudo rsync --delete -ra ./ "${DESTINATION}/" +sudo rsync --delete -ra ./ "$DESTINATION/" sudo chown -R www-data:jenkins $_ -cikit sniffers \ - --site-url=${SITE_URL} \ - --build-id=${BUILD_NAME} \ - --workspace=${WORKSPACE} +cikit sniffers "${ARGS[@]}" # Move reports to the website directory. -sudo cp -r ./docroot/reports/ "${DESTINATION}/docroot/" +deploy_hook post-deploy sudo chown -R jenkins:jenkins ./