kovalhome/.github/workflows/infra.yaml

79 lines
2.5 KiB
YAML
Raw Normal View History

name: Infrastructure
on:
pull_request:
branches:
- main
paths:
2023-09-04 18:59:57 +00:00
- infra/**playbook.yaml
- .github/workflows/infra.yaml
2023-09-03 00:13:27 +00:00
push:
branches:
- main
2023-09-02 19:18:21 +00:00
env:
2023-09-02 20:35:37 +00:00
DEPLOY: ${{ github.ref == 'refs/heads/main' && ((startsWith(github.event.head_commit.message, '[deploy-all]') && 'all') || ('some')) || 'none' }}
2023-09-02 19:18:21 +00:00
jobs:
2023-09-02 20:35:37 +00:00
ansible-playbooks:
name: Check and run Ansbile playbooks
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
2023-09-02 20:35:37 +00:00
with:
fetch-depth: ${{ env.DEPLOY == 'some' && 2 || 1 }}
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
run: |
pip3 install -r requirements.txt
ansible-galaxy collection install community.general
2023-09-02 18:12:34 +00:00
- name: Check playbooks
run: |
2023-09-04 18:59:57 +00:00
for file in $(find . -wholename "*/infra/*playbook.yaml" -type f); do
2023-09-04 19:09:33 +00:00
ansible-playbook --inventory ./inventory --syntax-check "$file"
2023-09-02 19:11:44 +00:00
done
2023-09-02 20:35:37 +00:00
- name: Get changed playbooks
id: files
2023-09-02 20:35:37 +00:00
if: env.DEPLOY == 'some'
uses: tj-actions/changed-files@v38
2023-09-02 18:12:34 +00:00
with:
2023-09-04 18:59:57 +00:00
files: infra/**/*playbook.yaml
2023-09-02 18:12:34 +00:00
2023-09-02 21:40:18 +00:00
- name: Get playbooks
id: playbooks
if: env.DEPLOY != 'none'
run: |
if [[ "${{ env.DEPLOY }}" == "some" ]]; then
export TO_RUN="${{ steps.files.outputs.all_changed_files }}"
else
2023-09-04 18:59:57 +00:00
export TO_RUN="$(find . -wholename './infra/*playbook.yaml' -type f)"
2023-09-02 21:40:18 +00:00
fi
2023-09-02 23:52:40 +00:00
export TO_RUN="$( echo -n $TO_RUN | tr ' ' '\n' | sort | tr '\n' ' ' )" # run things in order :)
2023-09-02 21:40:18 +00:00
echo "will run playbooks: $TO_RUN"
echo "to_run=$TO_RUN" >> "$GITHUB_OUTPUT"
2023-09-04 18:59:57 +00:00
- name: Setup environment
if: env.DEPLOY != 'none' && steps.playbooks.outputs.to_run != ''
run: |
mkdir ~/.ssh
chmod 700 ~/.ssh
echo "${{ secrets.SSH_PRIVATE }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
2023-09-02 21:43:46 +00:00
- name: Run playbooks
2023-09-02 21:56:50 +00:00
if: env.DEPLOY != 'none' && steps.playbooks.outputs.to_run != ''
2023-09-02 21:43:46 +00:00
env:
PROXMOX_HOST: ${{ secrets.PROXMOX_HOST }}
2023-09-03 00:01:05 +00:00
PROXMOX_USER: ${{ secrets.PROXMOX_USER }}
2023-09-02 21:43:46 +00:00
PROXMOX_TOKEN_ID: ${{ secrets.PROXMOX_TOKEN_ID }}
PROXMOX_TOKEN_SECRET: ${{ secrets.PROXMOX_TOKEN_SECRET }}
2023-09-04 18:59:57 +00:00
SSH_PUBLIC: ${{ secrets.SSH_PUBLIC }}
2023-09-02 21:43:46 +00:00
run: ansible-playbook --inventory ./inventory ${{ steps.playbooks.outputs.to_run }}