Skip to content

feat: add semantic versioning bot #90

feat: add semantic versioning bot

feat: add semantic versioning bot #90

Workflow file for this run

name: ci-cd
on:
push:
branches: [ dev, master ]
pull_request:
branches: [ dev, master ]
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
DOCKER_REGISTRY: docker.io
DOCKER_REPOSITORY: marcofontana17/cce_prototipo
jobs:
build-and-test:
name: build and test
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
strategy:
matrix:
os:
- ubuntu-22.04
- windows-2022
- macos-14
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.24'
check-latest: true
- name: Cache Go modules
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Test all modules on ${{ matrix.os }}
# add microservices here
run: |
# Define modules
modules=("api_gateway:./api_gateway" "service:./service" "utils:./utils")
# Initialize variables for summary
total_coverage=0
module_count=0
failed_modules=()
echo "=== Testing all modules on ${{ matrix.os }} ==="
echo ""
# Function to compare floating point numbers
compare_coverage() {
local coverage=$1
local threshold=$2
# Convert to integer by multiplying by 100 to avoid floating point issues
coverage_int=$(echo "$coverage * 100" | awk '{printf "%.0f", $1}')
threshold_int=$(echo "$threshold * 100" | awk '{printf "%.0f", $1}')
[ "$coverage_int" -lt "$threshold_int" ]
}
# Function to add floating point numbers
add_coverage() {
local total=$1
local add=$2
echo "$total + $add" | awk '{printf "%.2f", $1}'
}
# Function to calculate average
calc_average() {
local total=$1
local count=$2
echo "$total / $count" | awk '{printf "%.2f", $1}'
}
# Loop through each module
for module_info in "${modules[@]}"; do
module_name="${module_info%%:*}"
module_path="${module_info##*:}"
echo "========================================"
echo "Testing module: $module_name"
echo "Path: $module_path"
echo "========================================"
cd "$module_path"
# Download and verify dependencies
echo "Downloading dependencies..."
if ! go mod download; then
echo "Failed to download dependencies for $module_name"
failed_modules+=("$module_name:deps")
cd - > /dev/null
continue
fi
echo "Verifying dependencies..."
if ! go mod verify; then
echo "Failed to verify dependencies for $module_name"
failed_modules+=("$module_name:verify")
cd - > /dev/null
continue
fi
# Check if module has test files
test_files=$(find . -name "*_test.go" -type f | wc -l)
if [ "$test_files" -eq 0 ]; then
echo "No test files found for $module_name, skipping tests"
echo "Module $module_name completed (no tests to run)"
echo ""
cd - > /dev/null
continue
fi
# Run basic tests
echo "Running tests..."
if ! go test -v ./...; then
echo "Tests failed for $module_name"
failed_modules+=("$module_name:tests")
cd - > /dev/null
continue
fi
# Run tests with race detector
echo "Running tests with race detector..."
if ! go test -race -short ./...; then
echo "Race detector tests failed for $module_name"
failed_modules+=("$module_name:race")
cd - > /dev/null
continue
fi
# Run tests with coverage
echo "Running tests with coverage..."
if go test -coverprofile=coverage.out ./...; then
if [ -f coverage.out ]; then
echo "Coverage report for $module_name:"
go tool cover -func=coverage.out
# Extract coverage percentage
coverage=$(go tool cover -func=coverage.out | grep total | awk '{print substr($3, 1, length($3)-1)}')
if [ -n "$coverage" ] && [ "$coverage" != "0.0" ]; then
echo "Total coverage for $module_name: ${coverage}%"
# Add to total for average calculation
total_coverage=$(add_coverage "$total_coverage" "$coverage")
module_count=$((module_count + 1))
else
echo "No coverage data available for $module_name (likely no testable code)"
fi
else
echo "No coverage file generated for $module_name"
fi
else
echo "Coverage tests failed for $module_name"
failed_modules+=("$module_name:coverage-tests")
fi
echo "Completed testing $module_name"
echo ""
cd - > /dev/null
done
# Summary
echo "========================================"
echo "TEST SUMMARY for ${{ matrix.os }}"
echo "========================================"
if [ ${#failed_modules[@]} -eq 0 ]; then
echo "All modules passed all tests!"
else
echo "Some modules had failures:"
for failure in "${failed_modules[@]}"; do
echo " - $failure"
done
fi
if [ $module_count -gt 0 ]; then
average_coverage=$(calc_average "$total_coverage" "$module_count")
echo "Average coverage across all modules: ${average_coverage}%"
else
echo "No modules with testable code found"
fi
echo "========================================"
# Exit with error if any module failed
if [ ${#failed_modules[@]} -gt 0 ]; then
echo "Build failed due to module failures"
exit 1
fi
echo "All tests passed successfully!"
push-docker-images:
runs-on: ubuntu-24.04
name: Build and Push Docker Images
needs: build-and-test
strategy:
matrix:
# add microservices here
service:
- name: "api_gateway"
dockerfile: "./api_gateway/Dockerfile"
suffix: "api_gateway"
- name: "service"
dockerfile: "./service/Dockerfile"
suffix: "service"
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Extract metadata for ${{ matrix.service.name }}
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_REPOSITORY }}
tags: |
type=raw,value=${{ matrix.service.suffix }}-latest,enable=${{ github.ref == 'refs/heads/master' }}
type=raw,value=${{ matrix.service.suffix }}-dev-latest,enable=${{ github.ref == 'refs/heads/dev' }}
type=sha,prefix=${{ github.ref_name }}-,suffix=-${{ matrix.service.suffix }}
- name: Build and push ${{ matrix.service.name }}
uses: docker/build-push-action@v6
with:
context: .
file: ${{ matrix.service.dockerfile }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max