This project was forked from: arjungautam1/fullstack-backend and will be used for demonstration of DevOps CI/CD automation
-
Provides consistent development environment across users using Visual Studio Code Dev Containers. See configuration
-
Uses git pre-commit hooks to prevent Workflow failures caused by trivial errors like linter errors. This pre-commit hook is shared across users through Visual Studio Code Dev Containers using postCreateCommand.sh which sets hooks path to
.githooks
-
Uses Docker Compose to enable local deployment of the
application
(demoapp-backend) including alldependencies
(mysql). See compose.yaml -
Provides pull request checklist. See sample pull request
-
Supports multi-platform container image builds using Docker buildx bake. See docker-bake.hcl
-
Uses Container Structure Tests for running metadata, command and file existence tests to ensure consistency of image builds. See container-structure-test.yaml
-
Uses GitHub Actions workflows for automating builds, scans, tests, publishing of GitHub Packages, automatic pull request labeling, and release drafting (and versioning)
The following workflows are included in this project:
- Builds application container image and pushes it to Docker Hub
- Tests application container image with Container Structure Tests
- Scans application container image with Aqua Security Trivy
Codecov is the all-in-one code coverage reporting solution for any test suite — giving developers actionable insights to deploy reliable code with confidence. Trusted by over 29,000 organizations.
CodeQL is the analysis engine used by developers to automate security checks, and by security researchers to perform variant analysis.
SonarCloud is a cloud-based code analysis service designed to detect coding issues.
Automatically label new pull requests based on the paths of files being changed.
Drafts your next release notes as pull requests are merged into main
.
Release drafter recommends release version based on release-drafter.yml
See sample releases.
To create a release:
1. Create pre-release from draft. A corresponding tag is created by this step e.g. `v1.0.0`. Workflow is triggered when a tag starting with `v` is created.
2. Workflow builds and publishes release image to GitHub packages
3. Set pre-release as latest version
- MySQL database instance
- Install Podman Desktop
- Install Podman CLI
- Install Podman Compose
- Update Visual Studio Code User Settings
# settings.json
{
"dev.containers.dockerComposePath": "podman-compose", # Add this
"dev.containers.dockerPath": "podman" # Add this
}
This project uses Visual Studio Code Dev Containers which provides consistent Development environment across user(s) or team(s).
Visual Studio Code Dev Containers extension looks up devcontainer.json file which defines the Container environment specification.
To ensure successful build of this project, project.properties['java.version']
value from pom.xml must match with features['ghcr.io/devcontainers/features/java:1'].version
from devcontainer.json
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<properties>
<java.version>17</java.version>
</properties>
</project>
# .devcontainer/devcontainer.json
"features": {
# https://github.com/devcontainers/features/tree/main/src/java
"ghcr.io/devcontainers/features/java:1": {
"version": "17" # Version must match with pom.xml
}
}
# Build and run tests
mvn clean install
# Build and skip tests
mvn clean install -Dmaven.test.skip=true
# Notes:
# - Visual Studio Dev Container creates and runs `mysql` service. To add other dependency services, update `.devcontainer/compose.yaml`
# - Maven was configured to create 2 image tags: [1] based on project.version [2] `latest` tag
Multi-stage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain.
docker build -f Containerfile.multistage -t demoapp-backend:latest .
java -jar target/demoapp-backend-1.0.0-SNAPSHOT.jar
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.
By default, Compose looks up configuration from compose.yaml.
To ensure successful run of the Application, spring.datasource.url
from application.properties must match with compose.yaml
# src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://mysql:3306/demoapp
# Database credentials must not be hardcoded and should be provided using Environment variables
# Externalized Spring Configuration: https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
spring.datasource.username= # Environment variable: SPRING_DATASOURCE_USERNAME
spring.datasource.password= # Environment variable: SPRING_DATASOURCE_PASSWORD
# compose.yaml
services:
mysql: # Service name is also used as hostname when connecting from other containers
environment:
MYSQL_ROOT_PASSWORD: local
MYSQL_DATABASE: demoapp
MYSQL_USER: user
MYSQL_PASSWORD: password
healthcheck:
test: mysql --host=localhost --user=root --password=$$MYSQL_ROOT_PASSWORD demoapp # Login to mysql demoapp db. `$$` tells docker compose not to parse `MYSQL_ROOT_PASSWORD` environment variable
demoapp-backend:
depends_on:
mysql:
condition: service_healthy # Ensure `mysql` health before starting `demoapp-backend` container
environment:
# Externalized Spring Configuration: https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
SPRING_DATASOURCE_USERNAME: root # Overrides application.properties `spring.datasource.username`
SPRING_DATASOURCE_PASSWORD: local # Overrides application.properties `spring.datasource.password`
healthcheck:
test: curl --fail http://localhost:8080/actuator/health # Check Spring Boot Actuator. Requires `org.springframework.boot:spring-boot-starter-actuator` dependency in pom.xml
ports:
- "8080:8080" # Forwards container port 8080 to host port 8080. URL: http://localhost:8080/. Actuator URL: http://localhost:8080/actuator/health
# Note: by default, compose.yaml was configured to use an existing application image. Run build before docker compose or update compose.yaml and enable `build` field
docker compose --project-directory deploy/docker-compose up
docker compose --project-directory deploy/docker-compose up --build # rebuild application image, only applicable if `build` field is enabled
docker compose --project-directory deploy/docker-compose down --volumes # remove containers, networks and volumes created by docker compose
Container Structure Tests provide a powerful framework to validate the structure of a container image. These tests can be used to check the output of commands in an image, as well as verify metadata and contents of the filesystem
Run below command to run test for demoapp-backend
container-structure-test test --image demoapp-backend:latest --config container-structure-test.yaml