Skip to content

Instantly share code, notes, and snippets.

@onlime
Last active Aug 18, 2021
Embed
What would you like to do?
Simple SSH/rsync based continuous deployment of a Nuxt.js static frontend project with GitLab CI/CD
image: node:current
variables:
# In case you did not set up $SSH_KNOWN_HOSTS variable:
# SSH_OPTS: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
SSH_OPTS: ''
BASE_DIR: public_html/blog
KEEP: 5
default:
before_script:
- mkdir -p ~/.ssh && chmod 700 ~/.ssh
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- chmod 600 ${SSH_KEY}
- export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)
cache:
paths:
- node_modules
# combined build and deploy step to speed it up
build_deploy:
stage: deploy
script:
# build
- cp .env.prod .env
- yarn install
- yarn generate
# deploy
- apt-get update -y && apt-get install -y rsync
- ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} "cd ${BASE_DIR} && test ! -h current || cp -r \$(ls -td */ | grep -v current | head -n1) deploy_tmp"
- rsync -aHv --delete -e "ssh -i ${SSH_KEY} ${SSH_OPTS}" dist/ ${SSH_USER}@${DEPLOY_HOST}:${BASE_DIR}/deploy_tmp
- ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} "cd ${BASE_DIR} && mv deploy_tmp ${COMMIT_TIME} && rm -f current && ln -s ${COMMIT_TIME} current"
- ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} "cd ${BASE_DIR} && rm -rf \$(ls -dt */ | grep -v current | tail -n +$(($KEEP+1)))"
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- if: $CI_PIPELINE_SOURCE == "web"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment