Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Created January 8, 2017 15:55
Show Gist options
  • Save TechplexEngineer/ff95f0b3e3384643c211f0b121d2aad3 to your computer and use it in GitHub Desktop.
Save TechplexEngineer/ff95f0b3e3384643c211f0b121d2aad3 to your computer and use it in GitHub Desktop.
---
- name: Deploy code
hosts: appservers
# become: true
remote_user: '{{username}}'
vars:
install_dir: /home/{{username}}/software #no trailing slash
vars_prompt:
- name: "tag"
prompt: "What tag do you want to deploy"
private: no
tasks:
- name: ensure {{install_dir}} exists
file:
path: "{{install_dir}}"
state: directory
mode: 0744
owner: '{{username}}'
group: '{{username}}'
- name: Clone Repo
git:
repo: https://<username>:<token>@github.com/<username>/<reponame>.git
dest: "{{install_dir}}/repo"
force: yes
version: "{{tag}}"
accept_hostkey: yes
- name: install npm deps before build -- REQUIRED!
shell: meteor npm install
args:
chdir: "{{install_dir}}/repo"
- name: ensure {{install_dir}}/dist exists
file:
path: "{{install_dir}}/dist"
state: directory
mode: 0744
owner: '{{username}}'
group: '{{username}}'
- name: test if {{install_dir}}/dist/{{tag}} exists
stat:
path: "{{install_dir}}/dist/{{tag}}"
register: tagdir_stat
- name: Meteor build (This may take a while)
shell: "meteor build --directory {{install_dir}}/dist/{{tag}}"
args:
chdir: "{{install_dir}}/repo"
creates: "{{install_dir}}/dist/{{tag}}"
# ignore_errors: yes
when: tagdir_stat.stat.exists == False
- name: install npm deps
shell: npm install
args:
chdir: "{{install_dir}}/dist/{{tag}}/bundle/programs/server"
when: tagdir_stat.stat.exists == False
- name: copy pm2 process file
# note this file contains the port that meteor will run on
template:
src: ./run.json.j2
dest: "{{install_dir}}/dist/{{tag}}/bundle/run.json"
owner: '{{username}}'
group: '{{username}}'
- name: stop previous process
shell: sudo pm2 delete instance1
ignore_errors: yes
- name: start new process
shell: sudo pm2 start run.json
args:
chdir: '{{install_dir}}/dist/{{tag}}/bundle/'
- name: save process runlist for reboot
shell: sudo pm2 save
{
"apps" : [
{
"name": "instance1",
"script": "./main.js",
"env": {
"MONGO_URL": "mongodb://{{db.user}}:{{db.password}}@{{db.hosts}}/{{db.name}}?replicaSet={{db.rs}}",
"MONGO_OPLOG_URL": "mongodb://{{db.oplog.user}}:{{db.oplog.password}}@{{db.hosts}}/local?replicaSet={{db.rs}}&authSource={{db.oplog.authsource}}",
"MAIL_URL": "smtp://{{email.user}}:{{email.pass}}@{{email.host}}:{{email.port}}",
"PORT": 3000,
"ROOT_URL": "http://{{hostname}}",
"NODE_ENV": "PRODUCTION",
"KADIRA_APP_ID": "{{kadira.id}}",
"KADIRA_APP_SECRET": "{{kadira.secret}}",
"METEOR_SETTINGS": "{\"public\":{\"APP_VERSION\":\"{{tag}}\",\"IS_TESTING\":\"{% if 'stage' in group_names %}true{% else %}false{% endif %}\"}}"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment