Skip to content
Open

Lab09 #3280

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
83 changes: 83 additions & 0 deletions .github/workflows/ansible-deploy-bonus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
name: Ansible Deploy Bonus (Go App)

on:
push:
branches: [main, master, lab06]
paths:
- 'ansible/vars/app_bonus.yml'
- 'ansible/playbooks/deploy_bonus.yml'
- 'ansible/playbooks/deploy_all.yml'
- 'ansible/roles/web_app/**'
- '.github/workflows/ansible-deploy-bonus.yml'
pull_request:
branches: [main, master, lab06]
paths:
- 'ansible/vars/app_bonus.yml'
- 'ansible/playbooks/deploy_bonus.yml'
- 'ansible/playbooks/deploy_all.yml'
- 'ansible/roles/web_app/**'

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

deploy:
name: Deploy Bonus (Go) 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 and collections
run: |
pip install ansible
cd ansible
ansible-galaxy install -r requirements.yml

- 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 Bonus Application
run: |
cd ansible
echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > /tmp/vault_pass
ansible-playbook playbooks/deploy_bonus.yml \
-i inventory/hosts.ini \
--vault-password-file /tmp/vault_pass
rm /tmp/vault_pass

- name: Verify Bonus App Deployment
run: |
sleep 10
curl -f http://${{ secrets.VM_HOST }}:8001 || exit 1
curl -f http://${{ secrets.VM_HOST }}:8001/health || exit 1
88 changes: 88 additions & 0 deletions .github/workflows/ansible-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
name: Ansible Deployment

on:
push:
branches: [main, master, lab06]
paths:
- 'ansible/vars/app_python.yml'
- 'ansible/playbooks/deploy.yml'
- 'ansible/playbooks/deploy_python.yml'
- 'ansible/playbooks/deploy_all.yml'
- 'ansible/roles/web_app/**'
- 'ansible/roles/common/**'
- 'ansible/roles/docker/**'
- 'ansible/group_vars/**'
- 'ansible/inventory/**'
- 'ansible/playbooks/provision.yml'
- '!ansible/docs/**'
- '.github/workflows/ansible-deploy.yml'
pull_request:
branches: [main, master, lab06]
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
run: |
cd ansible
ansible-lint playbooks/*.yml

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 and collections
run: |
pip install ansible
cd ansible
ansible-galaxy install -r requirements.yml

- 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 Python Application
run: |
cd ansible
echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > /tmp/vault_pass
ansible-playbook playbooks/deploy_python.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
134 changes: 134 additions & 0 deletions .github/workflows/go-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
name: Go CI/CD Pipeline

# Cancel in-progress runs when a new run is triggered
concurrency:
group: go-ci-${{ github.ref }}
cancel-in-progress: true

# Path-based triggers: only run when app_go files change
on:
push:
branches:
- main
- master
- lab03
paths:
- 'app_go/**'
- '.github/workflows/go-ci.yml'
pull_request:
branches:
- main
- master
paths:
- 'app_go/**'
- '.github/workflows/go-ci.yml'

env:
GO_VERSION: '1.21'
DOCKER_IMAGE: mirana18/devops-info-service-go

jobs:
test:
name: Code Quality & Testing
runs-on: ubuntu-latest

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

- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: app_go/go.mod
cache: true

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: latest
working-directory: app_go
args: --timeout=5m

- name: Run tests
working-directory: ./app_go
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...

- name: Generate coverage report
working-directory: ./app_go
run: |
go tool cover -func=coverage.out

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

- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: go-coverage-report
path: go_python/coverage.xml
retention-days: 7

docker:
name: Build & Push Docker Image
runs-on: ubuntu-latest
needs: test

if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || 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_PASSWORD }}

- name: Generate version tags (CalVer)
id: meta
run: |
VERSION=$(date +%Y.%m)
BUILD_NUMBER=${{ github.run_number }}
FULL_VERSION="${VERSION}.${BUILD_NUMBER}"
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7)
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "full_version=${FULL_VERSION}" >> $GITHUB_OUTPUT
echo "short_sha=${SHORT_SHA}" >> $GITHUB_OUTPUT

- name: Extract Docker metadata
id: docker_meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_IMAGE }}
tags: |
type=raw,value=${{ steps.meta.outputs.full_version }}
type=raw,value=${{ steps.meta.outputs.version }}
type=raw,value=latest
type=raw,value=sha-${{ steps.meta.outputs.short_sha }}
labels: |
org.opencontainers.image.title=DevOps Info Service (Go)
org.opencontainers.image.description=Go-based system information service
org.opencontainers.image.version=${{ steps.meta.outputs.full_version }}
org.opencontainers.image.revision=${{ github.sha }}

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: ./app_go
file: ./app_go/Dockerfile
push: true
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.DOCKER_IMAGE }}:latest
cache-to: type=inline
Loading