# Auto deployment

You can specify a job to automate deploy project whenever target branch has new code version.

If deploying to your servers requires SSH access, you will need to add SSH keys to Sun* CI.

  • First, go to Project Secrets and add new secret variable, whose name SSH_PRIVATE_KEY and value is your SSH Private Key that used to access your server, then pass to your key to job container.
  • Add SERVER_IP, that is your server IP address, to Project Secrets
  • Add deploy job to .sun-ci.yml then push it to target branch.

Note: On server, you may need to add the public key to ~/.ssh/authorized_keys in order to grant access for SSH keys above.

# Using Deployer

stages:
  - deploy

jobs:
- name: deploy:production
  stage: deploy
  image: sunasteriskrnd/php-workspace:7.4
  before_script:
  - composer require deployer/deployer --dev
  - apk add openssh
  - mkdir /root/.ssh
  - echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa
  - chmod 700 -R /root/.ssh
  - chmod 600 -R /root/.ssh/*
  - echo "Host *\n\tStrictHostKeyChecking no\n\tIdentityFile /root/.ssh/id_rsa\n\tAddKeysToAgent yes\n" >> /root/.ssh/config
  - ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
  script:
  - php vendor/bin/dep deploy production -vv
  only:
    branches:
    - master
    - staging
  except:
    events:
    - pull_request

Note: Job above only run when you pushed to branch staging or master.

# Using Capistrano

stages:
- deploy

jobs:
- name: deploy:staging
  stage: deploy
  image: sunci/ruby:2.5.1
  before_script:
  - bundle _2.1.4_ install --path vendor/bundle
  script:
  - bundle _2.1.4_ exec cap staging deploy
  only:
    branches:
    - staging
  except:
    events:
    - pull_request

- name: deploy:production
  stage: deploy
  image: sunci/ruby:2.5.1
  before_script:
  - bundle _2.1.4_ install --path vendor/bundle
  script:
  - bundle _2.1.4_ exec cap production deploy
  only:
    branches:
    - master
  except:
    events:
    - pull_request

# Use SSH

stages:
  - deploy

jobs:
- name: deploy:production
  stage: deploy
  image: alpine
  before_script:
  - apk add openssh
  - mkdir /root/.ssh
  - echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa
  - chmod 700 -R /root/.ssh
  - chmod 600 -R /root/.ssh/*
  - echo "Host *\n\tStrictHostKeyChecking no\n\tIdentityFile /root/.ssh/id_rsa\n\tAddKeysToAgent yes\n" >> /root/.ssh/config
  - ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
  script:
  - ssh deploy@"$SERVER_IP" "command1; command2; command3"
  only:
    branches:
    - master
    - staging
  except:
    events:
    - pull_request