Skip to content

Deploy with Ansible playbook #6

Deploy with Ansible playbook

Deploy with Ansible playbook #6

name: Deploy to prod server with Ansible playbook
on:
workflow_run:
workflows: ["Build and Push Docker Image"]
types:
- completed
branches:
- dev
- main
jobs:
deploy:
runs-on: ubuntu-latest
environment: deployment
concurrency:
group: deploy-group
cancel-in-progress: true
steps:
- name: Checkout Repository
uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'
- name: Install Ansible
run: |
python -m pip install --upgrade pip
pip install ansible
- name: Install ssh keys
# For reference: https://stackoverflow.com/a/70447517
run: |
install -m 600 -D /dev/null ~/.ssh/galaxy
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/galaxy
echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
- name: Run Ansible Playbook
run: |
if [ $GITHUB_REF == "refs/heads/dev" ]; then
PLAYBOOK=dev.yml
else
PLAYBOOK=prod.yml
fi
cd ansible
ansible-playbook -i hosts $PLAYBOOK --tags update
env:
ANSIBLE_HOST_KEY_CHECKING: 'False'
ANSIBLE_REMOTE_USER: ${{ secrets.SSH_USER }}
ANSIBLE_PRIVATE_KEY_FILE: ~/.ssh/galaxy