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

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

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
run: |
cd ansible
ansible-lint playbooks/*.yml || true

deploy:
name: Deploy Application
needs: lint
runs-on: ubuntu-latest
if: github.event_name == 'push'
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
ansible-galaxy collection install community.docker

- 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

- 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 /tmp/vault_pass

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

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'

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

env:
PYTHON_VERSION: '3.11'
DOCKER_IMAGE: netimaaaa/devops-info-service

jobs:
test:
name: Test & Lint
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ['3.11', '3.12']
fail-fast: true

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

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
cache-dependency-path: 'app_python/requirements.txt'

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

- name: Lint with pylint
run: |
cd app_python
pylint app.py --disable=C0114,C0116,R0903,W0718 || true

- name: Format check with black
run: |
cd app_python
black --check app.py tests/ || true

- name: Run tests with pytest
run: |
cd app_python
pytest tests/ -v --tb=short

- name: Run tests with coverage
run: |
cd app_python
pytest tests/ --cov=. --cov-report=xml --cov-report=term

- name: Upload coverage to Codecov
if: matrix.python-version == '3.11'
uses: codecov/codecov-action@v4
with:
file: ./app_python/coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
token: ${{ secrets.CODECOV_TOKEN }}

docker:
name: Build & Push Docker Image
runs-on: ubuntu-latest
needs: [test, security]
if: github.event_name == 'push' && github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' || github.ref == 'refs/heads/lab03'

steps:
- name: Checkout code
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.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Generate version tags
id: meta
run: |
# Calendar versioning: YYYY.MM.BUILD_NUMBER
VERSION=$(date +'%Y.%m').${{ github.run_number }}
MONTH_VERSION=$(date +'%Y.%m')

echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "month_version=${MONTH_VERSION}" >> $GITHUB_OUTPUT

# Generate tags
TAGS="${{ env.DOCKER_IMAGE }}:${VERSION}"
TAGS="${TAGS},${{ env.DOCKER_IMAGE }}:${MONTH_VERSION}"
TAGS="${TAGS},${{ env.DOCKER_IMAGE }}:latest"

echo "tags=${TAGS}" >> $GITHUB_OUTPUT

echo "Generated version: ${VERSION}"
echo "Generated tags: ${TAGS}"

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: ./app_python
file: ./app_python/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache,mode=max
labels: |
org.opencontainers.image.title=DevOps Info Service
org.opencontainers.image.description=DevOps course info service
org.opencontainers.image.version=${{ steps.meta.outputs.version }}
org.opencontainers.image.created=${{ github.event.head_commit.timestamp }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.source=${{ github.repositoryUrl }}

- name: Image digest
run: echo "Image pushed with tags ${{ steps.meta.outputs.tags }}"
28 changes: 27 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
test
# Python - app_python/
app_python/__pycache__/
app_python/*.py[cod]
app_python/venv/
app_python/env/
app_python/*.log

# Go - app_go/
app_go/devops-info-service
app_go/devops-info-service-*
app_go/*.exe

# IDE
.idea/
.vscode/

# OS
.DS_Store
Thumbs.db

# Env
.env

*.retry
.vault_pass
ansible/inventory/*.pyc
__pycache__/
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
Loading