Skip to content
Merged
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
116 changes: 116 additions & 0 deletions .github/workflows/go-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Go CI

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

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

jobs:
changes:
name: Detect Changes
runs-on: ubuntu-latest
outputs:
app: ${{ steps.filter.outputs.app }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
app:
- 'app_go/**'
- '.github/workflows/go-ci.yml'

test:
name: Lint and Test
runs-on: ubuntu-latest
needs: changes
if: needs.changes.outputs.app == 'true'
defaults:
run:
working-directory: app_go

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

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

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v4
with:
working-directory: app_go
version: latest

- name: Run tests with coverage
run: |
go test -v -coverprofile=coverage.out -covermode=atomic ./...
go tool cover -func=coverage.out

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
files: app_go/coverage.out
flags: go
token: ${{ secrets.CODECOV_TOKEN }}
if: always()

build:
name: Build and Push Docker
runs-on: ubuntu-latest
needs: test
if: github.event_name == 'push' && github.ref == 'refs/heads/master'

steps:
- name: Checkout repository
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: Generate CalVer version
id: version
run: |
echo "VERSION=$(date +%Y.%m.%d)" >> $GITHUB_OUTPUT
echo "SHA_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_IMAGE }}
tags: |
type=raw,value=${{ steps.version.outputs.VERSION }}
type=raw,value=latest
type=sha,prefix=,format=short

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: app_go
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
147 changes: 147 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
name: Python CI

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

env:
DOCKER_IMAGE: mashfeii/devops-info-service
PYTHON_VERSION: '3.13'

jobs:
changes:
name: Detect Changes
runs-on: ubuntu-latest
outputs:
app: ${{ steps.filter.outputs.app }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
app:
- 'app_python/**'
- '.github/workflows/python-ci.yml'

test:
name: Lint and Test
runs-on: ubuntu-latest
needs: changes
if: needs.changes.outputs.app == 'true'
defaults:
run:
working-directory: app_python

steps:
- name: Checkout repository
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 requirements.txt
pip install -r requirements-dev.txt

- name: Lint with ruff
run: ruff check . --output-format=github

- name: Run tests with coverage
run: pytest --cov=. --cov-report=xml --cov-report=term --cov-fail-under=70

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
files: app_python/coverage.xml
flags: python
token: ${{ secrets.CODECOV_TOKEN }}
if: always()

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

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

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}

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

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

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

steps:
- name: Checkout repository
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: Generate CalVer version
id: version
run: |
echo "VERSION=$(date +%Y.%m.%d)" >> $GITHUB_OUTPUT
echo "SHA_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_IMAGE }}
tags: |
type=raw,value=${{ steps.version.outputs.VERSION }}
type=raw,value=latest
type=sha,prefix=,format=short

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: app_python
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
4 changes: 4 additions & 0 deletions app_go/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ devops-info-service
.idea/

.DS_Store

# Test coverage
coverage.out
coverage.html
26 changes: 26 additions & 0 deletions app_go/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
![Go CI](https://github.com/mashfeii/DevOps-Core-Course/actions/workflows/go-ci.yml/badge.svg)
![Coverage](https://codecov.io/gh/mashfeii/DevOps-Core-Course/branch/master/graph/badge.svg?flag=go)

# devops info service (go)

a go web service that provides detailed information about itself and its runtime environment
Expand Down Expand Up @@ -99,6 +102,29 @@ returns service health status for monitoring

## testing

### unit tests

```bash
# run tests
go test -v

# run tests with coverage
go test -coverprofile=coverage.out
go tool cover -func=coverage.out

# generate html coverage report
go tool cover -html=coverage.out -o coverage.html
```

### what's tested

- `GET /` endpoint: response structure, service info, system info, runtime info
- `GET /health` endpoint: status code, health status, uptime
- 404 handler: error response format
- helper functions: uptime calculation

### manual testing

```bash
# test main endpoint
curl http://localhost:8080/
Expand Down
Loading
Loading