Skip to content
Open

Lab09 #3287

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions .github/workflows/ansible-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: Ansible Deployment

on:
workflow_dispatch:
push:
branches:
- main
- master
- lab06

pull_request:
branches:
- main
- master

concurrency:
group: ansible-deploy-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
name: Ansible Lint
runs-on: ubuntu-latest
permissions:
contents: read
defaults:
run:
working-directory: ansible
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"

- name: Install lint dependencies
run: |
python -m pip install --upgrade pip
pip install ansible ansible-lint
ansible-galaxy collection install community.docker community.general

- name: Run ansible-lint
run: |
ansible-lint \
-x var-naming,key-order,name,yaml,command-instead-of-module \
playbooks/provision.yml playbooks/deploy.yml

deploy:
name: Deploy Application
needs: lint
runs-on: ubuntu-latest
permissions:
contents: read
defaults:
run:
working-directory: ansible
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Install deploy dependencies
run: |
python -m pip install --upgrade pip
pip install ansible
ansible-galaxy collection install community.docker community.general

- name: Configure SSH access
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H "${{ secrets.VM_HOST }}" >> ~/.ssh/known_hosts
cat > inventory/ci_hosts.ini <<EOF
[webservers]
ci-target ansible_host=${{ secrets.VM_HOST }} ansible_user=${{ secrets.VM_USER }}

[webservers:vars]
ansible_python_interpreter=/usr/bin/python3
EOF

- name: Deploy with Ansible
env:
ANSIBLE_VAULT_PASSWORD: ${{ secrets.ANSIBLE_VAULT_PASSWORD }}
run: |
echo "${ANSIBLE_VAULT_PASSWORD}" > /tmp/vault_pass
ansible-playbook playbooks/deploy.yml -i inventory/ci_hosts.ini --vault-password-file /tmp/vault_pass
rm -f /tmp/vault_pass

- name: Verify deployment
run: |
sleep 10
curl -f "http://${{ secrets.VM_HOST }}:8000/"
curl -f "http://${{ secrets.VM_HOST }}:8000/health"
98 changes: 98 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Python CI and Docker

on:
push:
branches:
- main
- master
- lab03
tags:
- "v*.*.*"
pull_request:
branches:
- main
- master

concurrency:
group: python-ci-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
name: Lint and Test
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
defaults:
run:
working-directory: app_python
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: "pip"
cache-dependency-path: |
app_python/requirements.txt
app_python/requirements-dev.txt

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements-dev.txt

- name: Lint (ruff)
run: ruff check .

- name: Run tests
run: pytest

- name: Snyk scan
uses: snyk/actions/python-3.11@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --severity-threshold=high
continue-on-error: true

docker:
name: Build and Push Docker Image
runs-on: ubuntu-latest
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/devops-info-service
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest

- name: Build and push
uses: docker/build-push-action@v6
with:
context: ./app_python
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
17 changes: 16 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
test
# Python
__pycache__/
*.py[cod]
venv/
venv.bak/
*.log

# IDE
.vscode/
.idea/

# OS
.DS_Store

# Monitoring secrets
monitoring/.env
11 changes: 11 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[defaults]
inventory = inventory/hosts.ini
roles_path = roles
host_key_checking = False
remote_user = ubuntu
retry_files_enabled = False

[privilege_escalation]
become = True
become_method = sudo
become_user = root
Loading