Ansible
image: ansible/ansible:latest
stages:
- build
- deploy
variables:
GIT_STRATEGY: fetch
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
zip_files:
stage: build
script:
- zip -r archive.zip . -x inventory.yml deploy.yml
artifacts:
paths:
- archive.zip
tags:
- caps-prod
only:
- dev
when: manual
ansible_deploy:
stage: deploy
script:
- ansible-playbook -i inventory.yml deploy.yml
dependencies:
- zip_files
tags:
- caps-prod
only:
- dev
when: manual
====================
---
- hosts: target_hosts
become: yes
vars:
src_file: ccCompressed23.3.zip
dest_dir: /opt/temp
app_dir: /opt/app/cc
backup_file: "ccCompresed_bkp_{{ ansible_date_time.date }}.zip"
tasks:
- name: Copy zip file from Ansible runner to target host
copy:
src: "{{ src_file }}"
dest: "{{ dest_dir }}"
- name: Get PID for the running service
command: pgrep -f "/opt/app/cc/node_modules/bin/gulp CC"
register: pid_output
- name: Kill the PID service
command: kill -9 {{ pid_output.stdout }}
- name: Create a backup zip file excluding node_modules
command: zip -r {{ backup_file }} . -x node_modules/\*
args:
chdir: "{{ app_dir }}"
- name: Copy the zip file from Ansible runner to the application directory
copy:
src: "{{ src_file }}"
dest: "{{ app_dir }}"
- name: Ensure unzip is installed
apt:
name: unzip
state: present
- name: Unzip the copied file
unarchive:
src: "{{ app_dir }}/{{ src_file }}"
dest: "{{ app_dir }}"
remote_src: yes
- name: Remove the copied zip file
file:
path: "{{ app_dir }}/{{ src_file }}"
state: absent
- name: Run the command
shell: nohup npm run-script gulp CC >/dev/null 2>&1 &
args:
chdir: "{{ app_dir }}"
...
Comments
Post a Comment