Created
August 30, 2017 05:43
-
-
Save denised/bed581e6e425f644a7f6a408465db6ff to your computer and use it in GitHub Desktop.
Ansible tasklist to pull from a github repo, with different behavior in dev vs. production
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
# This is a *task-level* include | |
# Invoke it as a task like so | |
# - include: "lib/pull_from_github.yml" | |
# vars: | |
# ... | |
# where the vars are these: | |
# repo=name required. name is like "smbbeta_testing" | |
# dest=dir required. directory for the repo | |
# deploy_type=prod|dev required. deployment type. | |
# branch=name opt. branch to checkout. default: master | |
# owner=username opt. owner of the directory & files (if not root) | |
# | |
# To force a redeploy from scratch, delete the dest directory in a step before this one. | |
# make sure deploy_type has a legal value. will also error out on undefined. | |
- fail: msg="deploy_type must be prod or dev" | |
when: deploy_type != 'prod' and deploy_type != 'dev' | |
# get status of destination directory | |
- stat: path="{{dest}}" | |
register: s | |
# pieces of the commands we will build | |
- set_fact: | |
repo_exists: "{{ s.stat.isdir is defined and s.stat.isdir }}" | |
clone_args: "--depth=1 --no-single-branch" | |
pullbranch: "master" | |
# changing the commands based on supplied branch name | |
- set_fact: | |
clone_args: "{{clone_args}} -b {{branch}}" | |
pullbranch: "{{branch}}" | |
when: branch is defined and branch and branch != 'master' | |
# build command in one of three cases: | |
# case 1: update prod. overrides any changed files --- forcible reset. | |
# (note: the combination of checkout and reset in that order make sure we are on the right branch, and | |
# the right commit of that branch; doesn't seem like there's a single git command that does this, which is odd) | |
- set_fact: | |
gh_command: "cd {{dest}}; git fetch; git checkout -f {{pullbranch}}; git reset --hard origin/{{pullbranch}}" | |
when: repo_exists and deploy_type == 'prod' | |
# case 2: update dev. for sanity, this only handles the ff case. any actual merging is left to the developer. | |
# also note this case ignores branch parameter --- we don't want to override whatever the developer might be doing. | |
- set_fact: | |
gh_command: "cd {{dest}}; git pull --ff-only" | |
when: repo_exists and deploy_type == 'dev' | |
# case 3: clone (prod or dev) | |
- set_fact: | |
gh_command: "git clone git@github.com:smbmatch/{{repo}}.git {{dest}} {{clone_args}}" | |
when: repo_exists == false | |
- debug: var=gh_command | |
- shell: "{{gh_command}}" | |
# yes I know this is an ugly way to do it | |
- name: restore proper ownership | |
file: | |
path: "{{dest}}" | |
state: directory | |
owner: "{{owner}}" | |
recurse: yes | |
when: owner is defined and owner | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment