Tag Archives: ci

gitlab CI

Getting into continuous integration can be a little intimidating, here’s a good gitlab-ci.yml that I’ve adjusted over time and is pretty much drop in for my development cycle. You’ll have to add your own variables in a projects CI settings and do some SSH setup for rsync to work, I’ll cover that later.

stages:
  - test
  - build
  - user-test
  - deploy

test:
  stage: test
  before_script:
  # Find the SSH Agent
  - 'which ssh-agent'
  # Run The Agent
  - eval $(ssh-agent -s)
  # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
  - ssh-add <(echo "$SSH_PRIVATE_KEY")

  script:
    - echo "Make Sure Commit SHA Exists, Use for revision hashing"
    - echo $CI_COMMIT_SHA
    - echo "Test Deploy To Preview"
    - rsync --update --dry-run --checksum -rlvzub --exclude ".*" --exclude "*.tgz" --exclude "Makefile.*" --backup-dir="backups/$CI_JOB_ID" -e "ssh -i ~/.ssh/id_rsa.pub" _src/ gitlab-runner@linux3:/var/www/mc-dev/$DEPLOYMENT_PREVIEW_DESTINATION/_preview
    - echo "Dry Run Deployment On Linux3"
    # - ssh gitlab-runner@linux3 -tt
    - rsync --update --dry-run --checksum -rlvzub --exclude ".*" --exclude "*.tgz*" --exclude "Makefile.*" --backup-dir="backups/$CI_JOB_ID" -e "ssh -i ~/.ssh/id_rsa.pub" _src/ gitlab-runner@linux3:/var/www/$DEPLOYMENT_DESTINATION

build:
  stage: build
  script: 
    - echo "Building The App"
    - echo $CI_COMMIT_SHA

user-test: 
  stage: user-test
  before_script:
    # Find SSH Agent
    - 'which ssh-agent'
    # Run the Agent
    - eval $(ssh-agent -s)
    #Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
  script:
    # - echo "Update Local Preview"
    # - rsync --update --checksum -rlvzu --exclude ".*" --exclude "*.tgz" --exclude "Makefile.*" -e "ssh -i ~/.ssh/id_rsa.pub" _src/ _preview
    - echo "Create Revision Hashes"
    - sed -i "s/@@hash/$CI_COMMIT_SHA/g" _src/index.php
    - echo "Deploying to the preview folder"
    - rsync --update --checksum -rlvzub --exclude ".*" --exclude "*.tgz" --exclude "Makefile.*" --backup-dir="backups/$CI_JOB_ID" -e "ssh -i ~/.ssh/id_rsa.pub" _src/ gitlab-runner@linux3:/var/www/mc-dev/$DEPLOYMENT_PREVIEW_DESTINATION/_preview
    
  when: manual
  allow_failure: true

deploy_prod:
  stage: deploy
  before_script:
    # Find the SSH Agent
    - 'which ssh-agent'
    # Run The Agent
    - eval $(ssh-agent -s)
    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo "$SSH_PRIVATE_KEY")

  script:
    - echo "Deploy App to Linux 3"
    # - ssh gitlab-runner@linux3 -tt
    - echo "Create Revision Hashes"
    - sed -i "s/@@hash/$CI_COMMIT_SHA/g" _src/index.php
    - echo "Copying Files"
    - rsync --update --checksum -rlvzub --exclude ".*" --exclude "*.tgz*" --exclude "Makefile.*" --backup-dir="backups/$CI_JOB_ID" -e "ssh -i ~/.ssh/id_rsa.pub" _src/ gitlab-runner@linux3:/var/www/$DEPLOYMENT_DESTINATION

  environment:
    name: production
    url: http://linux3/$DEPLOYMENT_DESTINATION
  only:
    - master
    - merge-requests
  # Uncomment below to turn on manual deployment
  # when: manual
  allow_failure: false

 

Keep an eye out for more coverage of setting up rsync over ssh.