Skip to content
Open

Lab06 #2847

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
82 changes: 82 additions & 0 deletions .github/workflows/ansible-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Ansible Deployment

on:
push:
branches: [main, master, lab06]
paths:
- 'ansible/**'
- '!ansible/docs/**'
- '.github/workflows/ansible-deploy.yml'
pull_request:
branches: [main, master]
paths:
- 'ansible/**'
- '!ansible/docs/**'

jobs:
lint:
name: Ansible Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

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

- name: Install ansible-lint
run: pip install ansible ansible-lint

- name: Run ansible-lint
run: |
cd ansible
ansible-lint playbooks/*.yml
continue-on-error: true

deploy:
name: Deploy Application
needs: lint
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v4

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

- name: Install Ansible
run: pip install ansible

- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H ${{ secrets.VM_HOST }} >> ~/.ssh/known_hosts

- name: Create inventory
run: |
cd ansible
mkdir -p inventory
cat > inventory/hosts.ini <<EOF
[webservers]
devops-vm ansible_host=${{ secrets.VM_HOST }} ansible_user=${{ secrets.VM_USER }} ansible_ssh_private_key_file=~/.ssh/id_ed25519
EOF

- name: Deploy with Ansible
run: |
cd ansible
echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > /tmp/vault_pass
ansible-playbook playbooks/deploy.yml \
-i inventory/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 }}:5000 || exit 1
curl -f http://${{ secrets.VM_HOST }}:5000/health || exit 1
105 changes: 105 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: Python CI

on:
push:
branches: [main, master, lab03]
paths:
- 'app_python/**'
- '.github/workflows/python-ci.yml'
pull_request:
branches: [main, master]
paths:
- 'app_python/**'
- '.github/workflows/python-ci.yml'

env:
DOCKER_IMAGE: dmitry567/devops-info-service

jobs:
test:
name: Lint & Test
runs-on: ubuntu-latest
defaults:
run:
working-directory: app_python

steps:
- uses: actions/checkout@v4

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

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

- name: Lint with ruff
run: ruff check .

- name: Run tests
run: pytest tests/ -v --cov=. --cov-report=term --cov-report=xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: app_python/coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false

security:
name: Security Scan
runs-on: ubuntu-latest
needs: test

steps:
- uses: actions/checkout@v4

- name: Run Snyk to check for vulnerabilities
uses: snyk/actions/python@master
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --file=app_python/requirements.txt --severity-threshold=high

docker:
name: Build & Push Docker
runs-on: ubuntu-latest
needs: test
if: github.event_name == 'push'

steps:
- uses: actions/checkout@v4

- name: Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_IMAGE }}
tags: |
type=raw,value=latest
type=raw,value={{date 'YYYY.MM'}}.{{sha}}
type=raw,value={{date 'YYYY.MM'}}

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

- name: Build and push
uses: docker/build-push-action@v6
with:
context: app_python
push: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=registry,ref=${{ env.DOCKER_IMAGE }}:latest
cache-to: type=inline

49 changes: 49 additions & 0 deletions .github/workflows/terraform-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Terraform CI

on:
push:
branches: [main, master, lab04]
paths:
- 'terraform/**'
- '.github/workflows/terraform-ci.yml'
pull_request:
branches: [main, master]
paths:
- 'terraform/**'
- '.github/workflows/terraform-ci.yml'

jobs:
validate:
name: Validate Terraform
runs-on: ubuntu-latest
defaults:
run:
working-directory: terraform

steps:
- uses: actions/checkout@v4

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.5.7

- name: Terraform Format Check
run: terraform fmt -check

- name: Terraform Init
run: terraform init -backend=false

- name: Terraform Validate
run: terraform validate

- name: Setup TFLint
uses: terraform-linters/setup-tflint@v4
with:
tflint_version: latest

- name: Run TFLint
run: |
tflint --init
tflint --format compact

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
test
.idea
5 changes: 5 additions & 0 deletions ansible/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.retry
.vault_pass
__pycache__/
*.pyc

13 changes: 13 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[defaults]
inventory = inventory/hosts.ini
roles_path = roles
host_key_checking = False
remote_user = ubuntu
retry_files_enabled = False
vault_password_file = .vault_pass

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

Loading