From fb5b0fe611da953f55a18aa0408c972abf0a6c16 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 2 Nov 2021 19:02:36 -0700 Subject: [PATCH] Add task to clean up older deployed versions --- deploy/playbooks/deploy.yml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/deploy/playbooks/deploy.yml b/deploy/playbooks/deploy.yml index ce60580..6ebc57f 100644 --- a/deploy/playbooks/deploy.yml +++ b/deploy/playbooks/deploy.yml @@ -16,10 +16,14 @@ cmd: yarn build when: not skip_build - - name: Generate a version number from the current timestamp + - name: Generate a version name from the current timestamp command: date '+%Y-%m-%dT%H:%M:%S' register: new_app_version + - name: Print out the new version name + debug: + msg: "Deploying new version: {{ new_app_version.stdout }}" + - name: Save new remote folder path to a variable set_fact: remote_app_root: "/srv/impress-2020/versions/{{ new_app_version.stdout }}" @@ -51,3 +55,16 @@ command: chdir: "{{ remote_app_root }}" cmd: yarn install + + - name: Find older versions to clean up + # Print out all but the last 5 versions + command: + chdir: /srv/impress-2020/versions + cmd: bash -c 'ls | head -n -5' + register: versions_to_clean_up + + - name: Clean up older versions + file: + path: "/srv/impress-2020/versions/{{ item }}" + state: absent + with_items: "{{ versions_to_clean_up.stdout_lines }}"