Compare commits

..

20 commits

Author SHA1 Message Date
lamya1baidouri
f64d5614d7 fix Dockerfile 2025-02-03 15:45:43 +01:00
lamya1baidouri
df676424e6 fix syntax 2025-02-03 15:25:19 +01:00
lamya1baidouri
fff6bbf82f fix syntax 2025-02-03 15:16:23 +01:00
lamya1baidouri
823f41c08d fix syntax 2025-02-03 15:09:34 +01:00
lamya1baidouri
2e17504403 fix syntax 2025-02-03 15:02:48 +01:00
lamya1baidouri
e4ba09c31c fix syntax 2025-02-03 14:57:08 +01:00
lamya1baidouri
ea7ba4762d fix syntax 2025-02-03 14:51:59 +01:00
lamya1baidouri
368ba55305 fix syntax 2025-02-03 14:38:10 +01:00
lamya1baidouri
6e70bce0a7 fix syntax 2025-02-03 14:23:03 +01:00
lamya1baidouri
6740a4eb67 fix syntax 2025-02-03 14:04:06 +01:00
lamya1baidouri
76d87edf2c fix syntax 2025-02-03 14:02:04 +01:00
lamya1baidouri
21bda69797 fix power api install 2025-02-03 13:51:13 +01:00
lamya1baidouri
8ea0831d9e fix pipeline.yml syntax 4 2025-02-03 13:42:24 +01:00
lamya1baidouri
1affb0686e fix pipeline.yml syntax 3 2025-02-03 13:38:59 +01:00
lamya1baidouri
5cbaec1bc9 fix pipeline.yml syntax 3 2025-02-03 13:36:09 +01:00
lamya1baidouri
4cc2a9557f fix pipeline.yml syntax 2025-02-03 11:37:41 +01:00
lamya1baidouri
d0279e2b2a fix pipeline.yml syntax 2025-02-03 11:36:17 +01:00
lamya1baidouri
f75b07374b fix pipeline.yml syntax 2025-02-03 11:33:43 +01:00
lamya1baidouri
2ff0bfecc9 fix pipeline.yml syntax 2025-02-03 11:31:59 +01:00
lamya1baidouri
6233b03066 run basic pipeline.yml 2025-02-03 11:29:44 +01:00
2 changed files with 220 additions and 78 deletions

View file

@ -1,11 +1,46 @@
# Not actually used by the devcontainer, but it is used by gitpod
ARG VARIANT=17-bullseye
FROM mcr.microsoft.com/vscode/devcontainers/java:0-${VARIANT}
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
ARG USER=vscode
VOLUME /home/$USER/.m2
VOLUME /home/$USER/.gradle
ARG JAVA_VERSION=17.0.7-ms
RUN sudo mkdir /home/$USER/.m2 /home/$USER/.gradle && sudo chown $USER:$USER /home/$USER/.m2 /home/$USER/.gradle
RUN bash -lc '. /usr/local/sdkman/bin/sdkman-init.sh && sdk install java $JAVA_VERSION && sdk use java $JAVA_VERSION'
# Build stage
FROM registry.access.redhat.com/ubi8/openjdk-17:latest as builder
# Install required packages
USER root
RUN microdnf install -y tar gzip
# Switch back to default user
USER 1001
WORKDIR /app
# Copy Maven wrapper and POM
COPY --chown=1001:0 .mvn/ .mvn/
COPY --chown=1001:0 mvnw pom.xml ./
RUN chmod +x mvnw
# Create Checkstyle suppressions file
RUN echo '<?xml version="1.0"?>' > checkstyle-suppressions.xml && \
echo '<!DOCTYPE suppressions PUBLIC "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN" "https://checkstyle.org/dtds/suppressions_1_2.dtd">' >> checkstyle-suppressions.xml && \
echo '<suppressions>' >> checkstyle-suppressions.xml && \
echo ' <suppress files=".*\.jar" checks="NoHttp"/>' >> checkstyle-suppressions.xml && \
echo ' <suppress files=".*\.pom" checks="NoHttp"/>' >> checkstyle-suppressions.xml && \
echo '</suppressions>' >> checkstyle-suppressions.xml
# Download dependencies
RUN ./mvnw dependency:go-offline -Dcheckstyle.suppressions.location=checkstyle-suppressions.xml
# Copy source code
COPY --chown=1001:0 src ./src
# Build the application
RUN ./mvnw package -DskipTests -Dcheckstyle.suppressions.location=checkstyle-suppressions.xml
# Run stage
FROM registry.access.redhat.com/ubi8/openjdk-17-runtime:latest
WORKDIR /app
# Copy the built artifact from builder stage
COPY --from=builder /app/target/*.jar app.jar
# Container runs on port 8080
EXPOSE 8080
# Set the startup command
ENTRYPOINT ["java", "-jar", "app.jar"]

View file

@ -2,59 +2,66 @@ name: Enhanced Java Application Pipeline with Metrics
on:
push:
branches: [ main ]
branches: [ pipeline-optimization ]
pull_request:
branches: [ main ]
branches: [ pipeline-optimization ]
jobs:
build-with-metrics:
runs-on: ubuntu-latest
timeout-minutes: 60
services:
prometheus:
image: prom/prometheus:latest
ports:
- 9090:9090
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:9090/-/healthy"]
interval: 10s
timeout: 5s
retries: 3
options: >-
--health-cmd "wget -q -O- http://localhost:9090/-/healthy || exit 1"
--health-interval 10s
--health-timeout 5s
--health-retries 3
pushgateway:
image: prom/pushgateway:latest
ports:
- 9091:9091
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:9091/-/healthy"]
interval: 10s
timeout: 5s
retries: 3
options: >-
--health-cmd "wget -q -O- http://localhost:9091/-/healthy || exit 1"
--health-interval 10s
--health-timeout 5s
--health-retries 3
steps:
- uses: actions/checkout@v4
# Installation and setup of monitoring tools
- name: Setup monitoring tools
id: setup-monitoring
timeout-minutes: 5
run: |
sudo apt-get update
sudo apt-get install -y powerstat linux-tools-common linux-tools-generic
sudo snap install powerapi
curl -L https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz -o node_exporter.tar.gz
tar xvfz node_exporter.tar.gz
# Start monitoring tools with improved configuration
- name: Start monitoring
run: |
# Start PowerAPI with Prometheus output
sudo powerapi --pid $$ --frequency 1000 --output prometheus --pushgateway-url http://localhost:9091/metrics/job/powerapi &
echo "POWERAPI_PID=$!" >> $GITHUB_ENV
set -eo pipefail
echo "::group::Installing system packages"
sudo apt-get update || (echo "Failed to update package lists" && exit 1)
sudo apt-get install -y powerstat linux-tools-common linux-tools-generic || (echo "Failed to install powerstat and linux tools" && exit 1)
echo "::endgroup::"
echo "::group::Setting up node exporter"
curl -L --retry 3 https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz -o node_exporter.tar.gz || (echo "Failed to download node exporter" && exit 1)
tar xvfz node_exporter.tar.gz || (echo "Failed to extract node exporter" && exit 1)
echo "::endgroup::"
- name: Start monitoring
id: start-monitoring
timeout-minutes: 2
run: |
set -eo pipefail
# Start node exporter
./node_exporter-*/node_exporter --web.listen-address=":9100" &
echo "NODE_EXPORTER_PID=$!" >> $GITHUB_ENV
# Create start timestamp file
timeout 30s bash -c 'until curl -s http://localhost:9100/metrics > /dev/null; do sleep 1; done' || (echo "Node exporter failed to start" && exit 1)
date +%s%N > pipeline_start_time.txt
- name: Set up JDK 17
@ -65,88 +72,188 @@ jobs:
cache: maven
- name: Build with Maven
id: build
timeout-minutes: 15
env:
MAVEN_OPTS: "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn"
run: |
set -eo pipefail
echo "Creating checkstyle suppressions file..."
cat > checkstyle-suppressions.xml << 'EOF'
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<suppress files="node_exporter.*" checks="NoHttp"/>
</suppressions>
EOF
echo "Modifying checkstyle configuration..."
if [ -f "src/checkstyle/nohttp-checkstyle.xml" ]; then
sed -i '/<module name="Checker">/a \ <module name="SuppressionFilter">\n <property name="file" value="${config_loc}/checkstyle-suppressions.xml"/>\n </module>' src/checkstyle/nohttp-checkstyle.xml
fi
echo "Starting Maven build..."
start_time=$(date +%s%N)
./mvnw -B verify
./mvnw -B verify \
-Dcheckstyle.config.location=src/checkstyle/nohttp-checkstyle.xml \
-Dcheckstyle.suppressions.location=checkstyle-suppressions.xml
build_status=$?
end_time=$(date +%s%N)
echo "BUILD_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
if [ $build_status -ne 0 ]; then
echo "::error::Maven build failed with status $build_status"
exit $build_status
fi
- name: Run tests
id: test
if: success() || failure()
timeout-minutes: 20
run: |
set -eo pipefail
start_time=$(date +%s%N)
./mvnw test
test_status=$?
end_time=$(date +%s%N)
echo "TEST_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
exit $test_status
- name: Build Docker image
id: docker-build
if: success()
timeout-minutes: 10
run: |
set -eo pipefail
start_time=$(date +%s%N)
docker build -t app:latest .
docker build -t app:latest -f .devcontainer/Dockerfile . --no-cache
build_status=$?
end_time=$(date +%s%N)
echo "DOCKER_BUILD_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
exit $build_status
- name: Setup Kubernetes
id: k8s-setup
if: success()
uses: helm/kind-action@v1
with:
wait: 120s
- name: Deploy to Kubernetes
id: deploy
if: success()
timeout-minutes: 10
run: |
set -eo pipefail
start_time=$(date +%s%N)
kubectl apply -f k8s/
kubectl wait --for=condition=ready pod -l app=petclinic --timeout=180s
kubectl apply -f k8s/ || (echo "Failed to apply Kubernetes manifests" && exit 1)
if ! kubectl wait --for=condition=ready pod -l app=petclinic --timeout=180s; then
echo "::error::Deployment failed - collecting debug information"
kubectl describe pods -l app=petclinic
kubectl logs -l app=petclinic --all-containers=true
exit 1
fi
end_time=$(date +%s%N)
echo "DEPLOY_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
# Export metrics with improved labeling and job naming
- name: Export metrics to Prometheus
run: |
# Export timing metrics with descriptive labels
echo "pipeline_build_duration_ms{stage=\"build\",project=\"petclinic\"} ${{ env.BUILD_TIME }}" | curl --data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline
echo "pipeline_test_duration_ms{stage=\"test\",project=\"petclinic\"} ${{ env.TEST_TIME }}" | curl --data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline
echo "pipeline_docker_build_duration_ms{stage=\"docker-build\",project=\"petclinic\"} ${{ env.DOCKER_BUILD_TIME }}" | curl --data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline
echo "pipeline_deploy_duration_ms{stage=\"deploy\",project=\"petclinic\"} ${{ env.DEPLOY_TIME }}" | curl --data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline
# Export power consumption metrics
while IFS=, read -r timestamp watts; do
echo "power_consumption_watts{project=\"petclinic\"} $watts $timestamp" | curl --data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline
done < energy_metrics.csv
# Collect additional resource metrics
- name: Collect resource metrics
run: |
# Memory usage metric
echo "pipeline_memory_usage_bytes{project=\"petclinic\"} $(free -b | grep Mem: | awk '{print $3}')" | curl --data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline
# CPU usage metric
echo "pipeline_cpu_usage_percent{project=\"petclinic\"} $(top -bn1 | grep "Cpu(s)" | awk '{print $2}')" | curl --data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline
# Stop monitoring tools and collect metrics
- name: Collect metrics
if: always()
timeout-minutes: 5
run: |
# End timestamp
set -eo pipefail
export_metric() {
local metric_name=$1
local metric_value=$2
local stage=$3
if [ -n "$metric_value" ]; then
echo "${metric_name}{stage=\"${stage}\",project=\"petclinic\"} ${metric_value}" | \
curl --retry 3 --retry-delay 2 --max-time 10 --silent --show-error \
--data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline || \
echo "::warning::Failed to export ${metric_name} for ${stage}"
fi
}
export_metric "pipeline_build_duration_ms" "${BUILD_TIME}" "build"
export_metric "pipeline_test_duration_ms" "${TEST_TIME}" "test"
export_metric "pipeline_docker_build_duration_ms" "${DOCKER_BUILD_TIME}" "docker-build"
export_metric "pipeline_deploy_duration_ms" "${DEPLOY_TIME}" "deploy"
- name: Collect resource metrics
if: always()
timeout-minutes: 2
run: |
set -eo pipefail
export_metric() {
local metric_name=$1
local metric_value=$2
local stage=$3
if [ -n "$metric_value" ]; then
echo "${metric_name}{stage=\"${stage}\",project=\"petclinic\"} ${metric_value}" | \
curl --retry 3 --retry-delay 2 --max-time 10 --silent --show-error \
--data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline || \
echo "::warning::Failed to export ${metric_name} for ${stage}"
fi
}
mem_usage=$(free -b | grep Mem: | awk '{print $3}') || echo "::warning::Failed to collect memory usage"
if [ -n "$mem_usage" ]; then
export_metric "pipeline_memory_usage_bytes" "$mem_usage" "memory"
fi
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}') || echo "::warning::Failed to collect CPU usage"
if [ -n "$cpu_usage" ]; then
export_metric "pipeline_cpu_usage_percent" "$cpu_usage" "cpu"
fi
- name: Collect final metrics
if: always()
timeout-minutes: 5
run: |
set -eo pipefail
date +%s%N > pipeline_end_time.txt
# Stop PowerAPI
sudo kill ${{ env.POWERAPI_PID }}
if [ -n "$NODE_EXPORTER_PID" ]; then
kill $NODE_EXPORTER_PID || echo "::warning::Failed to stop node exporter"
fi
# Stop node exporter
kill ${{ env.NODE_EXPORTER_PID }}
{
echo "=== System Resources ===" > system_metrics.txt
top -b -n 1 >> system_metrics.txt
} || echo "::warning::Failed to collect top metrics"
# Collect system metrics
top -b -n 1 > system_metrics.txt
free -m > memory_metrics.txt
df -h > disk_metrics.txt
{
echo "=== Memory Usage ===" > memory_metrics.txt
free -m >> memory_metrics.txt
} || echo "::warning::Failed to collect memory metrics"
{
echo "=== Disk Usage ===" > disk_metrics.txt
df -h >> disk_metrics.txt
} || echo "::warning::Failed to collect disk metrics"
# Save metrics as artifacts
- name: Save metrics
if: always()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: pipeline-metrics
path: |
energy_metrics.csv
system_metrics.txt
memory_metrics.txt
disk_metrics.txt
pipeline_start_time.txt
pipeline_end_time.txt
retention-days: 90
if-no-files-found: warn