Skip to content

Commit

Permalink
#147: Allow to run Ansible pre/post playbooks during CI build
Browse files Browse the repository at this point in the history
  • Loading branch information
BR0kEN- committed Jul 5, 2018
1 parent 8e6e0d1 commit d10d2ce
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 45 deletions.
19 changes: 19 additions & 0 deletions cmf/all/.cikit/ci/post-deploy.yml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 3 additions & 2 deletions scripts/roles/cikit-gitlab-ci/templates/.gitlab-ci.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
56 changes: 29 additions & 27 deletions scripts/roles/cikit-jenkins/templates/jobs/builder.xml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -81,47 +81,49 @@ Pull - do not reinstall an application and just grab latest changes from reposit
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>#!/usr/bin/env bash -e
<command>#!/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 ./
</command>
</hudson.tasks.Shell>
Expand Down
43 changes: 27 additions & 16 deletions scripts/roles/cikit-jenkins/templates/jobs/pr_builder.xml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -93,35 +93,46 @@
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>#!/usr/bin/env bash -e
<command>#!/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 ./
</command>
</hudson.tasks.Shell>
Expand Down

0 comments on commit d10d2ce

Please sign in to comment.