Skip to content

Commit

Permalink
Merge pull request #2233 from saucal/gh-action-provisioner
Browse files Browse the repository at this point in the history
GH Action - Run Provisioners
  • Loading branch information
tomjn committed Sep 24, 2020
2 parents 1436563 + db24fc3 commit 5acc906
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 13 deletions.
104 changes: 104 additions & 0 deletions .github/workflows/vvv-provisioning.yml
@@ -0,0 +1,104 @@
# This is a basic workflow to help you get started with Actions

name: VVV Provisioning

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the develop branch
on:
push:
branches: [ develop ]
pull_request:
branches: [ develop, stable ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
name: Run Provisioner
# The type of runner that the job will run on
runs-on: ubuntu-18.04

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

# Make Symlinks
- name: Create Vagrant Like Environment
run: |
# uninstall pre installed packages (to test if utilities work)
sudo apt-get -q --autoremove --purge remove php*
sudo apt-get -q autoclean
# create vagrant user
sudo groupadd -g 2000 vagrant
sudo useradd -u 2000 -g vagrant -m vagrant
# vvv_symlink function to sumulate synced folders
function vvv_symlink() {
if [ ! -d "${1}" ]; then
sudo mkdir -p "${1}"
fi
sudo chown -R vagrant:vagrant "${1}"
sudo ln -sf "${1}" "${2}"
}
# create srv folder
sudo -u "vagrant" mkdir -p "/srv"
# copy files provided by vagrant
sudo cp -f "$GITHUB_WORKSPACE/config/default-config.yml" "$GITHUB_WORKSPACE/config/config.yml"
sudo cp -f "$GITHUB_WORKSPACE/version" "/home/vagrant/version"
# make folders available
vvv_symlink "$GITHUB_WORKSPACE/database/sql" "/srv/database"
vvv_symlink "$GITHUB_WORKSPACE/config" "/srv/config"
vvv_symlink "$GITHUB_WORKSPACE/provision" "/srv/provision"
vvv_symlink "$GITHUB_WORKSPACE/certificates" "/srv/certificates"
vvv_symlink "$GITHUB_WORKSPACE/www" "/srv/www"
vvv_symlink "$GITHUB_WORKSPACE/log/memcached" "/var/log/memcached"
vvv_symlink "$GITHUB_WORKSPACE/log/nginx" "/var/log/nginx"
vvv_symlink "$GITHUB_WORKSPACE/log/php" "/var/log/php"
vvv_symlink "$GITHUB_WORKSPACE/log/provisioners" "/var/log/provisioners"
# Runs the provisioners in the expected order
- name: Run provison-pre.sh
run: sudo bash -c '. "/srv/provision/tests/provisioners.sh" && pre_hook'

- name: Run provison.sh
run: sudo bash -c '. "/srv/provision/tests/provisioners.sh" && provision_main'

- name: Run provison-dashboard.sh
run: sudo bash -c '. "/srv/provision/tests/provisioners.sh" && provision_dashboard'

- name: Run provison-utility-source.sh
run: sudo bash -c '. "/srv/provision/tests/provisioners.sh" && provision_utility_sources'

- name: Run provison-utility.sh
run: sudo bash -c '. "/srv/provision/tests/provisioners.sh" && provision_utilities'

- name: Run provison-site.sh
run: sudo bash -c '. "/srv/provision/tests/provisioners.sh" && provision_sites'

- name: Run provison-post.sh
run: sudo bash -c '. "/srv/provision/tests/provisioners.sh" && post_hook'

# At this point, we would run some extra tests
# TODO: Ideas
# - Add screenshots of provisioned sites
# - CURL mailhog API to see if it's working or not
# - Check VM hostfile

- name: Prepare Output
if: ${{ always() }}
run: |
sudo cp -rf "$GITHUB_WORKSPACE/log" "/vvv-logs"
MYUID=$(id -u -n)
MYGID=$(id -g -n)
sudo chown -R $MYUID:$MYGID "/vvv-logs"
- uses: actions/upload-artifact@v2
if: ${{ always() }}
with:
name: logs
path: "/vvv-logs"
8 changes: 4 additions & 4 deletions provision/provision-site.sh
Expand Up @@ -135,16 +135,16 @@ function vvv_provision_site_repo() {
if [[ -d "${VM_DIR}/.git" ]]; then
echo " * Updating ${SITE} in ${VM_DIR}..."
cd "${VM_DIR}"
git reset "origin/${BRANCH}" --hard -q
git pull origin "${BRANCH}" -q
git checkout "${BRANCH}" -q
noroot git reset "origin/${BRANCH}" --hard -q
noroot git pull origin "${BRANCH}" -q
noroot git checkout "${BRANCH}" -q
else
echo "${RED}Problem! A site folder for ${SITE} was found at ${VM_DIR} that doesn't use a site template, but a site template is defined in the config file. Either the config file is mistaken, or a previous attempt to provision has failed, VVV will not try to git clone the site template to avoid data destruction, either remove the folder, or fix the config/config.yml entry${CRESET}"
fi
else
# Clone or pull the site repository
echo -e " * Downloading ${SITE}, git cloning from ${REPO} into ${VM_DIR}"
git clone --recursive --branch "${BRANCH}" "${REPO}" "${VM_DIR}" -q
noroot git clone --recursive --branch "${BRANCH}" "${REPO}" "${VM_DIR}" -q
if [ $? -eq 0 ]; then
echo " * ${SITE} Site Template clone successful"
else
Expand Down
35 changes: 26 additions & 9 deletions provision/tests/provisioners.sh
Expand Up @@ -3,6 +3,16 @@

export VVV_LOG=""

function vvv_run_provisioner() {
local STATUS
bash $@
STATUS=$?
if [ "${STATUS}" -eq 0 ]; then
return $STATUS
fi
exit $STATUS
}

# provisioners

function pre_hook() {
Expand All @@ -13,7 +23,7 @@ function pre_hook() {
# file) should go in this script. If it does not exist, no extra provisioning will run.
if [[ -f "/srv/provision/provision-pre.sh" ]]; then
VVV_LOG="pre"
bash /srv/provision/provision-pre.sh
vvv_run_provisioner /srv/provision/provision-pre.sh
fi
}

Expand All @@ -26,7 +36,7 @@ function post_hook() {
# without having to replace the entire default provisioning script.
if [[ -f "/srv/provision/provision-post.sh" ]]; then
VVV_LOG="post"
bash /srv/provision/provision-post.sh
vvv_run_provisioner /srv/provision/provision-post.sh
fi
}

Expand All @@ -35,7 +45,7 @@ function provision_dashboard() {
local dashboard_branch=$(get_config_value "dashboard.branch" "master")

VVV_LOG="dashboard"
bash /srv/provision/provision-dashboard.sh "${dashboard_repo}" "${dashboard_branch}"
vvv_run_provisioner /srv/provision/provision-dashboard.sh "${dashboard_repo}" "${dashboard_branch}"
}

function provision_utility_sources() {
Expand Down Expand Up @@ -79,7 +89,7 @@ function provision_utility_sources() {
local i
for i in ${!name[@]}; do
VVV_LOG="utility-source-${name[$i]}"
bash /srv/provision/provision-utility-source.sh "${name[$i]}" "${repo[$i]}" "${branch[$i]}"
vvv_run_provisioner /srv/provision/provision-utility-source.sh "${name[$i]}" "${repo[$i]}" "${branch[$i]}"
done
}

Expand All @@ -99,7 +109,7 @@ function provision_utility() {
local group=$1
local utility=$2
VVV_LOG="utility-${group}-${utility}"
bash /srv/provision/provision-utility.sh "${group}" "${utility}"
vvv_run_provisioner /srv/provision/provision-utility.sh "${group}" "${utility}"
}

function provision_sites() {
Expand All @@ -114,7 +124,7 @@ function provision_site() {
local site=$1
local skip_provisioning=$(get_config_value "sites.${site}.skip_provisioning" "False")
if [[ $skip_provisioning == "True" ]]; then
continue
return
fi
local repo=$(get_config_type "sites.${site}")
if [[ "${repo}" == "str" ]]; then
Expand All @@ -127,7 +137,7 @@ function provision_site() {
local nginx_upstream=$(get_config_value "sites.${site}.nginx_upstream" "php")

VVV_LOG="site-${site}"
bash /srv/provision/provision-site.sh "${site}" "${repo}" "${branch}" "${vm_dir}" "${skip_provisioning}" "${nginx_upstream}"
vvv_run_provisioner /srv/provision/provision-site.sh "${site}" "${repo}" "${branch}" "${vm_dir}" "${skip_provisioning}" "${nginx_upstream}"
}

function provision_main() {
Expand All @@ -139,9 +149,16 @@ function provision_main() {
# of the provisioning provided by default.
if [[ -f "/srv/provision/provision-custom.sh" ]]; then
VVV_LOG="main-custom"
bash /srv/provision/provision-custom.sh
vvv_run_provisioner /srv/provision/provision-custom.sh
else
VVV_LOG="main"
bash /srv/provision/provision.sh
vvv_run_provisioner /srv/provision/provision.sh
fi

# refresh VVV_CONFIG, as the main provisioner actually creates the /vagrant/config.yml
VVV_CONFIG=/vagrant/vvv-custom.yml
if [[ -f /vagrant/config.yml ]]; then
VVV_CONFIG=/vagrant/config.yml
fi
export VVV_CONFIG
}

0 comments on commit 5acc906

Please sign in to comment.