209 lines
4.0 KiB
Markdown
209 lines
4.0 KiB
Markdown
# CI/CD Configuration
|
|
|
|
## GitHub Actions
|
|
|
|
### Basic Test Workflow
|
|
|
|
```yaml
|
|
name: Spring Boot Tests
|
|
|
|
on: [push, pull_request]
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- uses: actions/checkout@v3
|
|
|
|
- name: Set up JDK 17
|
|
uses: actions/setup-java@v3
|
|
with:
|
|
java-version: '17'
|
|
distribution: 'temurin'
|
|
|
|
- name: Cache Maven dependencies
|
|
uses: actions/cache@v3
|
|
with:
|
|
path: ~/.m2/repository
|
|
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
|
|
restore-keys: ${{ runner.os }}-maven-
|
|
|
|
- name: Run tests
|
|
run: ./mvnw test -Dspring.profiles.active=test
|
|
|
|
- name: Generate test report
|
|
uses: dorny/test-reporter@v1
|
|
if: always()
|
|
with:
|
|
name: Maven Tests
|
|
path: target/surefire-reports/*.xml
|
|
reporter: java-junit
|
|
```
|
|
|
|
### With Testcontainers
|
|
|
|
```yaml
|
|
name: Tests with Testcontainers
|
|
|
|
on: [push, pull_request]
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
|
|
services:
|
|
postgres:
|
|
image: postgres:16-alpine
|
|
env:
|
|
POSTGRES_PASSWORD: test
|
|
POSTGRES_USER: test
|
|
POSTGRES_DB: testdb
|
|
options: >-
|
|
--health-cmd pg_isready
|
|
--health-interval 10s
|
|
--health-timeout 5s
|
|
--health-retries 5
|
|
|
|
steps:
|
|
- uses: actions/checkout@v3
|
|
|
|
- name: Set up JDK 17
|
|
uses: actions/setup-java@v3
|
|
with:
|
|
java-version: '17'
|
|
distribution: 'temurin'
|
|
|
|
- name: Run tests
|
|
run: ./mvnw test
|
|
env:
|
|
SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/testdb
|
|
SPRING_DATASOURCE_USERNAME: test
|
|
SPRING_DATASOURCE_PASSWORD: test
|
|
```
|
|
|
|
## GitLab CI
|
|
|
|
```yaml
|
|
stages:
|
|
- test
|
|
|
|
test:
|
|
stage: test
|
|
image: openjdk:17-jdk-slim
|
|
|
|
services:
|
|
- name: postgres:16-alpine
|
|
alias: postgres
|
|
variables:
|
|
POSTGRES_DB: testdb
|
|
POSTGRES_USER: test
|
|
POSTGRES_PASSWORD: test
|
|
|
|
variables:
|
|
SPRING_DATASOURCE_URL: "jdbc:postgresql://postgres:5432/testdb"
|
|
SPRING_DATASOURCE_USERNAME: test
|
|
SPRING_DATASOURCE_PASSWORD: test
|
|
|
|
cache:
|
|
paths:
|
|
- .m2/repository/
|
|
|
|
script:
|
|
- ./mvnw test
|
|
|
|
artifacts:
|
|
when: always
|
|
reports:
|
|
junit: target/surefire-reports/TEST-*.xml
|
|
```
|
|
|
|
## Docker Compose for Local Testing
|
|
|
|
```yaml
|
|
version: '3.8'
|
|
|
|
services:
|
|
postgres:
|
|
image: postgres:16-alpine
|
|
environment:
|
|
POSTGRES_DB: testdb
|
|
POSTGRES_USER: test
|
|
POSTGRES_PASSWORD: test
|
|
ports:
|
|
- "5432:5432"
|
|
volumes:
|
|
- postgres_data:/var/lib/postgresql/data
|
|
|
|
mysql:
|
|
image: mysql:8.0
|
|
environment:
|
|
MYSQL_DATABASE: testdb
|
|
MYSQL_USER: test
|
|
MYSQL_PASSWORD: test
|
|
MYSQL_ROOT_PASSWORD: test
|
|
ports:
|
|
- "3306:3306"
|
|
volumes:
|
|
- mysql_data:/var/lib/mysql
|
|
|
|
redis:
|
|
image: redis:7-alpine
|
|
ports:
|
|
- "6379:6379"
|
|
|
|
volumes:
|
|
postgres_data:
|
|
mysql_data:
|
|
```
|
|
|
|
Run tests with: `docker-compose up -d && ./mvnw test`
|
|
|
|
## Maven Test Profiles
|
|
|
|
```xml
|
|
<profiles>
|
|
<profile>
|
|
<id>unit-tests</id>
|
|
<build>
|
|
<plugins>
|
|
<plugin>
|
|
<groupId>org.apache.maven.plugins</groupId>
|
|
<artifactId>maven-surefire-plugin</artifactId>
|
|
<configuration>
|
|
<includes>
|
|
<include>**/*Test.java</include>
|
|
</includes>
|
|
<excludes>
|
|
<exclude>**/*IntegrationTest.java</exclude>
|
|
</excludes>
|
|
</configuration>
|
|
</plugin>
|
|
</plugins>
|
|
</build>
|
|
</profile>
|
|
|
|
<profile>
|
|
<id>integration-tests</id>
|
|
<build>
|
|
<plugins>
|
|
<plugin>
|
|
<groupId>org.apache.maven.plugins</groupId>
|
|
<artifactId>maven-failsafe-plugin</artifactId>
|
|
<executions>
|
|
<execution>
|
|
<goals>
|
|
<goal>integration-test</goal>
|
|
<goal>verify</goal>
|
|
</goals>
|
|
</execution>
|
|
</executions>
|
|
</plugin>
|
|
</plugins>
|
|
</build>
|
|
</profile>
|
|
</profiles>
|
|
```
|
|
|
|
Run with: `./mvnw test -Punit-tests` or `./mvnw verify -Pintegration-tests`
|