Skip to content
Open

Lab06 #2749

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

on:
push:
branches: [main, master]
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:
- name: Checkout code
uses: actions/checkout@v4

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

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

- name: Run ansible-lint
env:
ANSIBLE_VAULT_PASSWORD: ${{ secrets.ANSIBLE_VAULT_PASSWORD }}
run: |
cd ansible
echo "$ANSIBLE_VAULT_PASSWORD" > .vault_pass
ansible-lint playbooks/*.yml
rm -f .vault_pass

deploy:
name: Deploy Application
needs: lint
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- name: Checkout code
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_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.VM_HOST }} >> ~/.ssh/known_hosts 2>/dev/null || true

- name: Create vault password file
run: echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > /tmp/vault_pass

- name: Update inventory with CI host
run: |
cd ansible
echo "[webservers]" > inventory/hosts.ini
echo "myvm ansible_host=${{ secrets.VM_HOST }} ansible_port=${{ secrets.VM_PORT }} ansible_user=${{ secrets.VM_USER }} ansible_ssh_private_key_file=~/.ssh/id_rsa ansible_ssh_common_args='-o StrictHostKeyChecking=no'" >> inventory/hosts.ini

- name: Run Ansible playbook
run: |
cd ansible
ansible-playbook playbooks/deploy.yml \
--vault-password-file /tmp/vault_pass

- name: Verify Deployment
run: |
sleep 10
curl -f http://${{ secrets.VM_HOST }}:5000/health || exit 1

- name: Cleanup secrets
if: always()
run: |
rm -f /tmp/vault_pass ~/.ssh/id_rsa
109 changes: 109 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
name: Python CI/CD

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"
workflow_dispatch:

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

permissions:
contents: read

env:
IMAGE_NAME: devops-info-service
PYTHON_VERSION: "3.11"

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
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 app_python/requirements.txt -r app_python/requirements-dev.txt

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

- name: Run tests
env:
PYTHONPATH: app_python
run: |
pytest app_python/tests \
--cov=app_python \
--cov-report=term-missing \
--cov-report=xml \
--cov-fail-under=70

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
files: ./coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
verbose: true

- name: Snyk scan
if: ${{ env.SNYK_TOKEN != '' }}
uses: snyk/actions/python@v1
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --severity-threshold=high --file=app_python/requirements.txt

docker:
needs: test
runs-on: ubuntu-latest
timeout-minutes: 20
if: >
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/lab03'))
steps:
- uses: actions/checkout@v4

- name: Set version (CalVer)
run: echo "VERSION=$(date -u +'%Y.%m.%d')" >> $GITHUB_ENV

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

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

- name: Build and push image
uses: docker/build-push-action@v6
with:
context: app_python
file: app_python/Dockerfile
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}:latest
50 changes: 50 additions & 0 deletions .github/workflows/terraform-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Terraform CI

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

jobs:
validate:
name: Validate Terraform
runs-on: ubuntu-latest

defaults:
run:
working-directory: terraform/

steps:
- name: Checkout code
uses: actions/checkout@v4

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

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

- 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: Init TFLint
run: tflint --init

- name: Run TFLint
run: tflint --format compact
29 changes: 28 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
test
test
/.venv
.coverage*
coverage.xml

# Terraform
*.tfstate
*.tfstate.*
.terraform/
.terraform.lock.hcl
terraform.tfvars
*.tfvars
crash.log
*.pem
*.key
*.box
*.ova

# Pulumi
.pulumi/
__pycache__/
venv/
.pulumi-cache/

# Ansible
*.retry
.vault_pass
ansible/inventory/*.pyc
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python-envs.defaultEnvManager": "ms-python.python:system",
"python-envs.pythonProjects": []
}
12 changes: 12 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[defaults]
inventory = inventory/hosts.ini
roles_path = roles
host_key_checking = False
remote_user = vagrant
retry_files_enabled = False
vault_password_file = .vault_pass

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