Created
October 10, 2019 08:05
-
-
Save monachilada/ad0431f82ea09bb29b475517524b93fd to your computer and use it in GitHub Desktop.
Gitlab pipeline for running Craft/gatsby side by side on the same server to enable live preview.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include: 'https://gitlab.com/gitlab-cd/ssh-template/raw/master/ssh.yml' | |
# Install build dependencies. | |
.build: | |
image: node:12.8 | |
variables: | |
YARN_CACHE_DIR: '.ci/yarn-cache' | |
script: | |
# Some build dependencies | |
- apt-get update && apt-get install -y libvips-dev --no-install-recommends | |
# Setup yarn and caching | |
- mkdir -pv $YARN_CACHE_DIR | |
# Install required dependencies | |
- yarn install --cache-folder $YARN_CACHE_DIR | |
cache: | |
key: global | |
paths: | |
- $YARN_CACHE_DIR | |
artifacts: | |
expire_in: 30 min | |
paths: | |
- node_modules/ | |
# Deploy the files | |
.deploy: | |
script: | |
# Install rsync | |
- apt-get update | |
- apt-get install -y rsync | |
# Create some variables | |
- export NEW_DEPLOY_ID=$CI_JOB_ID | |
- export DEPLOY_PORT=${DEPLOY_PORT:-22} | |
- export SHARED_DIR=$DEPLOY_PATH/shared | |
- export CURRENT_DIR=$DEPLOY_PATH/current | |
- export BUILD_DIR=$DEPLOY_PATH/builds | |
- export DEPLOY_DIR=$BUILD_DIR/$NEW_DEPLOY_ID | |
# Initiate SSH keys connection to deploy server | |
- ssh_init "$DEPLOY_PRIVATE_KEY" "$DEPLOY_SERVER" | |
# Rsync everything to the server including repo files, plus artifacts, but minus patterns defined in the .deployignore file | |
- rsync --rsync-path="sudo mkdir -p $DEPLOY_DIR && sudo rsync" -aqz --exclude-from .deployignore * $DEPLOY_USER@$DEPLOY_SERVER:$DEPLOY_DIR/ | |
# SSH back into the server | |
- ssh -t -p $DEPLOY_PORT $DEPLOY_USER@$DEPLOY_SERVER << EOF | |
- sudo su | |
# If it is the first deploy | |
- if [ ! -f $SHARED_DIR/.env ]; then | |
# Tell us what happened | |
- echo "Created a new deploy folder, but you'll need to setup Gatsby .env vars" | |
# End the if/then/else | |
- fi | |
# Link to shared resources in the new deploy directory | |
- ln -sfn $SHARED_DIR/.env $DEPLOY_DIR/.env | |
# Switch the current build to the latest deploy | |
- ln -sfn $DEPLOY_DIR $CURRENT_DIR | |
# Cleanup permissions | |
- chown -R $DEPLOY_PERMISSION $DEPLOY_PATH | |
- chmod -R 0644 $DEPLOY_PATH | |
# Start the Gatsby server in dev mode | |
- npx kill-port 8000 | |
- sleep 10 | |
- cd $CURRENT_DIR | |
- rm -rf ./.cache | |
- nohup npm run develop > /dev/null 2>&1 & | |
# End the session | |
- exit $? | |
- EOF | |
stages: | |
- test-build | |
- test-deploy | |
- build | |
- deploy | |
# Test build | |
build:test: | |
stage: test-build | |
extends: .build | |
when: manual | |
allow_failure: false | |
except: | |
refs: | |
- develop | |
- master | |
# Test deploy | |
deploy:test: | |
stage: test-deploy | |
extends: .deploy | |
environment: | |
name: staging | |
needs: ['build:test'] | |
when: on_success | |
except: | |
refs: | |
- develop | |
- master | |
# Generate yarn modules folder | |
build: | |
stage: build | |
extends: .build | |
only: | |
refs: | |
- develop | |
# Automatically deploy to staging environment when committing to develop branch | |
deploy:staging: | |
stage: deploy | |
dependencies: | |
- build | |
extends: .deploy | |
environment: | |
name: staging | |
only: | |
refs: | |
- develop | |
# Automatically deploy to production environment when committing to master branch | |
deploy:production: | |
stage: deploy | |
dependencies: | |
- build | |
extends: .deploy | |
environment: | |
name: production | |
only: | |
refs: | |
- master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment