DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

Jenkins

- name: Deploy updated files to Windows
  hosts: windows
  vars:
    code_path: 'C:\\code'
    backup_path: 'C:\\backup'
  tasks:

    - name: Ensure backup directory exists
      win_file:
        path: "{{ backup_path }}"
        state: directory

    - name: List of Python files to manage
      set_fact:
        files_to_deploy:
          - script1.py
          - script2.py

    - name: Check and sync only changed files
      block:
        - name: Compare local and remote checksum for each file
          win_stat:
            path: "{{ code_path }}\\{{ item }}"
          register: win_stat_results
          loop: "{{ files_to_deploy }}"

        - name: Get local checksums for the files
          stat:
            path: "../{{ item }}"
          register: local_stat_results
          delegate_to: localhost
          loop: "{{ files_to_deploy }}"

        - name: Set list of changed files
          set_fact:
            changed_files: "{{ changed_files | default([]) + [item.0.item] }}"
          when: 
            - item.1.stat.exists
            - item.1.stat.checksum is defined
            - item.0.stat.checksum != item.1.stat.checksum
          loop: "{{ zip(local_stat_results.results, win_stat_results.results) }}"

        - name: Move changed files to backup folder
          win_command: "move {{ code_path }}\\{{ item }} {{ backup_path }}\\{{ item }}"
          when: item in changed_files
          loop: "{{ files_to_deploy }}"

        - name: Copy changed files from GitLab clone to Windows server
          win_copy:
            src: "../{{ item }}"
            dest: "{{ code_path }}\\{{ item }}"
          when: item in changed_files
          loop: "{{ files_to_deploy }}"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)