Skip to content

Instantly share code, notes, and snippets.

@veysiertekin
Last active November 2, 2020 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veysiertekin/0edb0c0e846fbc34f766195f4552309a to your computer and use it in GitHub Desktop.
Save veysiertekin/0edb0c0e846fbc34f766195f4552309a to your computer and use it in GitHub Desktop.
Gitlab

Gitlab Ansible integration ( sample git checkout deployment on a machine )

Define Ansible base template & deployment playbook

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

SSH integration

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

Usage in a repository

.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>

Cache dependencies

SBT

variables:
  SBT_OPTS: "-Dsbt.global.base=./.sbt/1.0 -Dsbt.boot.directory=./.sbt/boot -Dsbt.ivy.home=./.ivy2"
  COURSIER_CACHE: "./.coursier/cache/v1"

cache:
  untracked: false
  paths:
    - "./.sbt/1.0"
    - "./.sbt/boot"
    - "./.ivy2/cache"
    - "./.coursier/cache"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment