Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - how to keep only 3 release

I have a VPS sever where I deploy frequently releases and the dir structure is that I have a current dir, what is a symlink to an actual release under a releases dir. How can I achieve, that only X ( in my case 3) releases stay in the releases dir, the rest can be deleted, to spare free HDD and because I don't need them any more. This setup is what capifony uses.

like image 624
ghostika Avatar asked Dec 16 '14 08:12

ghostika


1 Answers

Something along this lines should work, where bin would be your symlinked directory :

- name: Set timestamp
  set_fact: release_timestamp="{{ansible_date_time.epoch}}"

- name: Deploy code from repository
  action: git repo={{repo_url}} dest={{app_dir}}/releases/{{release_timestamp}} remote={{repo_remote}} version={{branch}}

- name: Create symlink to bin folder
  file: src={{app_dir}}/releases/{{release_timestamp}}  dest={{app_dir}}/bin state=link

- name: List old releases and clean them up
  shell: "ls -t {{app_dir}}/releases | tail -n +{{releases_to_keep + 1}}"
  register: ls_output

- file: name={{app_dir}}/releases/{{ item }} state=absent
  with_items: ls_output.stdout_lines

If you want to do rollback:

- name: Find the previous release
    shell: "ls -t {{app_dir}}/releases | head -2 | tail -1"
    register: ls_output

- name: Create symlink to previous release folder
    file: src={{app_dir}}/releases/{{item}}  dest={{app_dir}}/bin state=link
    with_items: ls_output.stdout_lines
like image 186
Ivan Delchev Avatar answered Sep 27 '22 23:09

Ivan Delchev