1- Create a common repo for example dataops
2- Create toolset/deploy_repo.yml
file within that repository with following contents:
.default-ansible-job:
image: williamyeh/ansible:ubuntu18.04
.checkout-repo:
extends:
- .default-ansible-job
only:
- master
before_script:
- eval $(ssh-agent -s)
- echo "$REMOTE_SSH_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan -H $REMOTE_HOST >> /etc/ssh/ssh_known_hosts
- |
cat <<EOF >> deploy-repo-playbook.yml
- name: Clone current repo at a remote host
hosts: all
become: no
gather_facts: no
tasks:
- name: Checkout git repository
git:
repo: "{{ repository_path }}"
update: yes
force: yes
dest: "{{ host_target_path }}"
clone: yes
remote: origin
EOF
script:
- ansible --version
- ansible-playbook -i $REMOTE_HOST, -u $REMOTE_USER -e "repository_path=$REPOSITORY_CLONE_URL host_target_path=$TARGET_PATH" deploy-repo-playbook.yml
1- Create a ssh private & public pair
ssh-keygen -t rsa -f my.key
2- Login into as a user on target machine
3- Add previously created public key into .ssh/authorized_keys
4- Make sure permissions are set correctly with chmod 700 -R ~/.ssh
4- Add previously created private key into Gitlab's variables
.gitlab-ci.yml
include:
- project: "<group>/dataops"
file: "toolset/deploy_repo.yml"
checkout-repo:
extends:
- .checkout-repo
variables:
REMOTE_SSH_KEY: <ssh private key variable>
REMOTE_HOST: <host variable>
REMOTE_USER: <user variable>
REPOSITORY_CLONE_URL: $CI_REPOSITORY_URL
TARGET_PATH: <target path at host machine>