5

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.

3 Answers 3

7

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
1
  • I came across your suggestion. Thank you for that one. One thing to add: Variables in with_items should be surrounded by brackets {{}}. with_items: {{ ls_output.stdout_lines }}
    – Phillip
    May 3, 2018 at 6:49
5

This is an old topic but I think this answer will be helpful.

- name: Find all directories in releases folder
  find:
    paths="{{ app_dir }}/{{ releases }}"
    file_type=directory
  register: dirs

- name: Only last three releases
  file:
    path="{{ item.path }}"
    state=absent
  with_items:
    - "{{ (dirs.files | sort(attribute='ctime'))[:-3] }}"
0

Perhaps a bit off-topic, but it will give you a place to start:

There is an Ansible role in the galaxy specifically pointed at replacing Capistrano/Capifony. With an example for a Symfony2 project even :-)

Example usage:

To answer your question more specifically:

The loop required to clean the releases folder would be either a shell script or a composition of Ansible tasks (saving ls output, sorting and removing). To make this a smoother process, we decided to place this logic in an Ansible module. This module is used in the project_deploy role linked above. But if you prefer to wrote your own role, the module was extracted and placed in ot's own galaxy role:

Finally, if you're interested in a deeper look at why we came to the current construction:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.