Skip to content

Instantly share code, notes, and snippets.

@nsciacca
Last active May 5, 2023 22:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsciacca/66d65856548c2a581e9ea43d64d5eb2c to your computer and use it in GitHub Desktop.
Save nsciacca/66d65856548c2a581e9ea43d64d5eb2c to your computer and use it in GitHub Desktop.
GitHub to Acquia workflow recipe and helper scripts
Project structure:
.github/workflows/deploy-acquia.yml
scripts/ci/push.sh
scripts/composer/ScriptHandler.php
.gitignore
composer.json
###
### GitHub repository .gitignore section
###
# Ignore files only needed on CI
.bashrc
# Ignore directories generated by Composer
/drush/contrib/
/drush/Commands/
/vendor/
/docroot/libraries
/docroot/core/
/docroot/modules/contrib/
/docroot/modules/README.txt
/docroot/themes/contrib/
/docroot/themes/README.txt
/docroot/profiles/contrib/
/docroot/profiles/README.txt
/docroot/private/scripts/quicksilver
/console/
# Ignore scaffold files
.drush-lock-update
.gitattributes
pantheon.upstream.yml
docroot/.eslintrc.json
docroot/INSTALL.txt
docroot/README.txt
docroot/example.gitignore
docroot/.csslintrc
docroot/.editorconfig
docroot/.eslintignore
docroot/.eslintrc
docroot/.ht.router.php
docroot/autoload.php
docroot/index.php
docroot/robots.txt
docroot/sites/default/default.services.pantheon.preproduction.yml
docroot/sites/default/default.services.yml
docroot/sites/default/default.settings.php
docroot/sites/default/settings.pantheon.php
docroot/sites/development.services.yml
docroot/sites/example.settings.local.php
docroot/sites/example.sites.php
docroot/sites/README.txt
docroot/update.php
docroot/web.config
# Add directories containing build assets below.
# Keep all additions above the "cut" line.
# This distinction is only important when using this
# repository as a custom upstream. The .gitignore file
# is not modified in the GitHub PR workflow.
# :::::::::::::::::::::: cut ::::::::::::::::::::::
###
### Server site .gitignore section
###
### Items below the "cut" line are still ignored when building the artifact.
### Items above the "cut" line are ignored in the GitHub repository, but
### committed to the deployed server repository.
###
# Docksal local settings.
.docksal/docksal-local.env
.docksal/docksal-local.yml
# Ignore Drupal's file directory
docroot/sites/default/files
private-files/*
!private-files/.htaccess
# Place local settings in settings.local.php
docroot/sites/*/settings.local.php
docroot/sites/*/services*.yml
docroot/sites/*/default.*.*
docroot/sites/example.*.*
docroot/sites/local.services.yml
!docroot/sites/*/services.yml
# Ignore SimpleTest multi-site environment.
docroot/sites/simpletest
# Ignore files generated by PhpStorm
.idea
# Packages #
############
*.7z
*.dmg
*.gz
*.bz2
*.iso
*.jar
*.rar
*.tar
*.zip
*.tgz
node_modules/
# Logs and databases #
######################
*.log
*.sql
# OS generated files #
######################
.DS_Store*
ehthumbs.db
Thumbs.db
._*
# Vim generated files #
######################
*.un~
# SASS #
##########
.sass-cache
cghooks.lock
## Ignore phpdotenv files
.env
## Simplesaml config ignores
**/simplesamlphp/**/*.local.php
## Theme compiling assets
docroot/themes/custom/**/node_modules
{
"name": "mysite/mysite",
"description": "Example to show revelant script stuff below",
"autoload": {
"classmap": [
"scripts/composer/ScriptHandler.php"
]
},
"scripts": {
"prepare-for-deploy": "DrupalProject\\composer\\ScriptHandler::prepareForDeploy"
}
}
name: Deploy - Acquia
on:
push:
branches:
- main
workflow_dispatch:
inputs:
git-ref:
description: Branch, tag or SHA (optional)
required: false
jobs:
build:
name: Build artifact & deploy
runs-on: ubuntu-latest
steps:
- name: Setup PHP with composer v2
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
tools: composer:v2
- name: Clone Repository (latest)
uses: actions/checkout@v3
if: github.event.inputs.git-ref == ''
with:
fetch-depth: 0
- name: Clone Repository (custom ref)
uses: actions/checkout@v3
if: github.event.inputs.git-ref != ''
with:
ref: ${{ github.event.inputs.git-ref }}
fetch-depth: 0
- name: Get Composer Cache Directory
id: get-composer-cache-dir
run: |
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache Composer
uses: actions/cache@v3
id: composer-cache
with:
path: ${{ steps.get-composer-cache-dir.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: '16.14.0'
- name: Build artifact
run: |
composer install --optimize-autoloader --no-dev
cd ./docroot/themes/custom/mytheme
npm install -g gulp-cli
npm install
gulp spritesvg
gulp cideploy
- name: SSH connect to Acquia
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.ACQUIA_SSH_KEY }}
config: ${{ secrets.SSH_CONFIG }}
known_hosts: ${{ secrets.KNOWN_HOSTS }}
- name: Push code to Acquia repo
env:
KNOWN_HOSTS: ${{ secrets.KNOWN_HOSTS }}
DEPLOY_REPO: mysite@svn-xxxxx.prod.hosting.acquia.com:mysite.git
run: |
scripts/ci/push.sh
drush:
name: Run drush commands
runs-on: ubuntu-latest
needs: build
steps:
- name: SSH connect to Acquia
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.ACQUIA_SSH_KEY }}
config: ${{ secrets.SSH_CONFIG }}
known_hosts: ${{ secrets.KNOWN_HOSTS }}
- name: Update Drupal (replace with cloud hooks)
run: |
ssh mysite.dev@mysitedev.ssh.prod.acquia-sites.com "cd /var/www/html/mysite.dev/docroot && drush cr && drush cim -y && drush updb -y && drush cr"
#!/bin/bash
set -e
#
# This script pushes the artifact to the remote git repo.
#
# Checkout the main-build branch
git checkout -b main-build
# Prepare for Deploy
composer -n prepare-for-deploy
# If the module is -dev, a .git file comes down.
find docroot -name .git -print0 | xargs -0 rm -rf
find vendor -name .git -print0 | xargs -0 rm -rf
find vendor -name .gitignore -print0 | xargs -0 rm -rf
# Add everything
git add --force --all
# Print changes
# git status
if [[ `git status --porcelain` ]]; then
GIT_MESSAGE="Deploying ${GITHUB_SHA}: $(git log -1 --pretty=%B)"
echo ${KNOWN_HOSTS}
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git remote add deploy ${DEPLOY_REPO}
git commit -qm "${GIT_MESSAGE}" --no-verify
git push deploy -v --force main-build;
else
# No changes
echo "No code changes, deploy complete."
fi
<?php
namespace DrupalProject\composer;
use Composer\Script\Event;
use DrupalFinder\DrupalFinder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
/**
* Provides the scripts for composer.
*/
class ScriptHandler {
// This is called by the deploy hook to remove .git directories.
public static function prepareForDeploy() {
// Get rid of any .git directories that Composer may have added.
// n.b. Ideally, there are none of these, as removing them may
// impair Composer's ability to update them later. However, leaving
// them in place prevents us from pushing to Pantheon.
$dirsToDelete = [];
$finder = new Finder();
foreach (
$finder
->directories()
->in(getcwd())
->ignoreDotFiles(false)
->ignoreVCS(false)
->depth('> 0')
->name('.git')
as $dir) {
$dirsToDelete[] = $dir;
}
$fs = new Filesystem();
$fs->remove($dirsToDelete);
// Fix up .gitignore: remove everything above the "::: cut :::" line
$gitignoreFile = getcwd() . '/.gitignore';
$gitignoreContents = file_get_contents($gitignoreFile);
$gitignoreContents = preg_replace('/.*::: cut :::*/s', '', $gitignoreContents);
file_put_contents($gitignoreFile, $gitignoreContents);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment