Skip to content

Commit d402cdc

Browse files
author
Jens van der Wateren
committed
feat: Base application for devops introduction
0 parents  commit d402cdc

17 files changed

Lines changed: 544 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: CI Pipeline
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Set up JDK 17
17+
uses: actions/setup-java@v4
18+
with:
19+
java-version: '17'
20+
distribution: 'temurin'
21+
22+
- name: Cache Maven dependencies
23+
uses: actions/cache@v3
24+
with:
25+
path: ~/.m2
26+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
27+
restore-keys: ${{ runner.os }}-m2
28+
29+
- name: Run tests
30+
run: ./mvnw clean test
31+
32+
- name: Run linting (Checkstyle)
33+
run: ./mvnw checkstyle:check
34+
35+
- name: Run static analysis (SpotBugs)
36+
run: ./mvnw spotbugs:check
37+
38+
- name: Build application
39+
run: ./mvnw clean package -DskipTests
40+
41+
docker:
42+
runs-on: ubuntu-latest
43+
needs: test
44+
45+
steps:
46+
- uses: actions/checkout@v4
47+
48+
- name: Build Docker image
49+
run: docker build -t devops-training:latest .
50+
51+
- name: Test Docker image
52+
run: |
53+
docker run -d -p 8080:8080 --name test-app devops-training:latest
54+
sleep 30
55+
curl -f http://localhost:8080/api/health || exit 1
56+
docker stop test-app
57+
docker rm test-app

.idea/github-actions.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip
18+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar

Dockerfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Use Maven image with OpenJDK 17 as base image
2+
FROM maven:latest AS build
3+
4+
# Set working directory
5+
WORKDIR /workspace/app
6+
7+
# Copy pom.xml and checkstyle config
8+
COPY pom.xml .
9+
COPY checkstyle.xml .
10+
RUN mvn dependency:go-offline -B
11+
12+
# Copy source code
13+
COPY src src
14+
15+
# Build application
16+
RUN mvn package -DskipTests
17+
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
18+
19+
# Production stage
20+
FROM openjdk:17-jdk-slim
21+
22+
# Create non-root user for security
23+
RUN groupadd -r spring && useradd -r -g spring spring
24+
25+
# Set working directory
26+
WORKDIR /app
27+
28+
# Copy built application from build stage
29+
ARG DEPENDENCY=/workspace/app/target/dependency
30+
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
31+
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
32+
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
33+
34+
# Change ownership to spring user
35+
RUN chown -R spring:spring /app
36+
USER spring
37+
38+
# Expose port
39+
EXPOSE 8080
40+
41+
# Run application
42+
ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.devopstraining.DevopsTrainingApplication"]

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# DevOps Training Project
2+
3+
A Spring Boot application designed for DevOps training with linting, testing, and CI/CD pipeline examples.
4+
5+
## Project Structure
6+
7+
- **Spring Boot 3.2.0** with Java 17
8+
- **REST API** with simple endpoints
9+
- **Unit Tests** using Spring Boot Test
10+
- **Linting** with Checkstyle and SpotBugs
11+
- **Docker** containerization
12+
- **GitHub Actions** CI pipeline
13+
14+
## Quick Start
15+
16+
### Local Development
17+
```bash
18+
./mvnw spring-boot:run
19+
```
20+
21+
### Docker
22+
```bash
23+
# Build and run with Docker Compose
24+
docker-compose up --build
25+
26+
# Or build and run manually
27+
docker build -t devops-training .
28+
docker run -p 8080:8080 devops-training
29+
```
30+
31+
### Testing
32+
```bash
33+
./mvnw test
34+
```
35+
36+
### Linting
37+
```bash
38+
./mvnw checkstyle:check
39+
./mvnw spotbugs:check
40+
```
41+
42+
## API Endpoints
43+
44+
- `GET /api/hello` - Returns a hello message
45+
- `GET /api/hello/{name}` - Returns a personalized hello message
46+
- `GET /api/health` - Health check endpoint
47+
48+
## Training Goals
49+
50+
This project serves as a starting point for DevOps training. Students should:
51+
52+
1. **Current State**: The project builds, tests pass, and basic CI runs
53+
2. **Add More Pipeline Steps**: Extend the GitHub Actions workflow with:
54+
- Security scanning
55+
- Code coverage reporting
56+
- Docker image scanning
57+
- Deployment stages
58+
- Integration testing
59+
- Performance testing
60+
61+
## GitHub Actions Workflow
62+
63+
The current `.github/workflows/ci.yml` includes:
64+
- Running tests
65+
- Code linting (Checkstyle & SpotBugs)
66+
- Building the application
67+
- Docker image build and basic testing
68+
69+
### Training Exercises
70+
71+
Students can extend the pipeline by adding:
72+
- Code coverage with JaCoCo
73+
- OWASP dependency check
74+
- SonarQube analysis
75+
- Multi-environment deployments
76+
- Slack/email notifications
77+
- Automated releases
78+
79+
## Known Issues
80+
81+
The current code has intentional Checkstyle violations to demonstrate linting in action. This is perfect for training scenarios where students learn to:
82+
1. Identify code quality issues
83+
2. Fix linting violations
84+
3. Understand the importance of code standards

checkstyle.xml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
6+
<module name="Checker">
7+
<property name="charset" value="UTF-8"/>
8+
<property name="severity" value="warning"/>
9+
<property name="fileExtensions" value="java, properties, xml"/>
10+
11+
<!-- Checks for whitespace -->
12+
<module name="FileTabCharacter">
13+
<property name="eachLine" value="true"/>
14+
</module>
15+
16+
<!-- Checks for Size Violations -->
17+
<module name="LineLength">
18+
<property name="max" value="120"/>
19+
</module>
20+
21+
<module name="TreeWalker">
22+
<!-- Checks for Naming Conventions -->
23+
<module name="ConstantName"/>
24+
<module name="LocalFinalVariableName"/>
25+
<module name="LocalVariableName"/>
26+
<module name="MemberName"/>
27+
<module name="MethodName"/>
28+
<module name="PackageName"/>
29+
<module name="ParameterName"/>
30+
<module name="StaticVariableName"/>
31+
<module name="TypeName"/>
32+
33+
<!-- Checks for imports -->
34+
<module name="AvoidStarImport"/>
35+
<module name="IllegalImport"/>
36+
<module name="RedundantImport"/>
37+
<module name="UnusedImports"/>
38+
39+
<!-- Checks for Size Violations -->
40+
<module name="MethodLength"/>
41+
<module name="ParameterNumber"/>
42+
43+
<!-- Checks for whitespace -->
44+
<module name="EmptyForIteratorPad"/>
45+
<module name="GenericWhitespace"/>
46+
<module name="MethodParamPad"/>
47+
<module name="NoWhitespaceAfter"/>
48+
<module name="NoWhitespaceBefore"/>
49+
<module name="OperatorWrap"/>
50+
<module name="ParenPad"/>
51+
<module name="TypecastParenPad"/>
52+
<module name="WhitespaceAfter"/>
53+
<module name="WhitespaceAround"/>
54+
55+
<!-- Modifier Checks -->
56+
<module name="ModifierOrder"/>
57+
<module name="RedundantModifier"/>
58+
59+
<!-- Checks for blocks -->
60+
<module name="AvoidNestedBlocks"/>
61+
<module name="EmptyBlock"/>
62+
<module name="LeftCurly"/>
63+
<module name="NeedBraces"/>
64+
<module name="RightCurly"/>
65+
66+
<!-- Checks for common coding problems -->
67+
<module name="EmptyStatement"/>
68+
<module name="EqualsHashCode"/>
69+
<module name="HiddenField">
70+
<property name="ignoreSetter" value="true"/>
71+
<property name="ignoreConstructorParameter" value="true"/>
72+
</module>
73+
<module name="IllegalInstantiation"/>
74+
<module name="InnerAssignment"/>
75+
<module name="MissingSwitchDefault"/>
76+
<module name="SimplifyBooleanExpression"/>
77+
<module name="SimplifyBooleanReturn"/>
78+
79+
<!-- Checks for class design -->
80+
<module name="DesignForExtension"/>
81+
<module name="FinalClass"/>
82+
<module name="HideUtilityClassConstructor"/>
83+
<module name="InterfaceIsType"/>
84+
<module name="VisibilityModifier"/>
85+
86+
<!-- Miscellaneous other checks -->
87+
<module name="ArrayTypeStyle"/>
88+
<module name="FinalParameters"/>
89+
<module name="TodoComment"/>
90+
<module name="UpperEll"/>
91+
</module>
92+
</module>

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3.8'
2+
3+
services:
4+
app:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
ports:
9+
- "8080:8080"
10+
environment:
11+
- SPRING_PROFILES_ACTIVE=docker
12+
healthcheck:
13+
test: ["CMD", "curl", "-f", "http://localhost:8080/api/health"]
14+
interval: 30s
15+
timeout: 10s
16+
retries: 3
17+
start_period: 40s
18+
restart: unless-stopped

0 commit comments

Comments
 (0)