Skip to content

Commit 300a0e9

Browse files
authored
Dockerfile build with Github Action
.
1 parent 66f962a commit 300a0e9

19 files changed

+754
-14
lines changed

.github/workflows/build.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- develop
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
jobs:
10+
build:
11+
name: Build
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
with:
16+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
17+
- name: Set up JDK 11
18+
uses: actions/setup-java@v1
19+
with:
20+
java-version: 11
21+
- name: Cache SonarCloud packages
22+
uses: actions/cache@v1
23+
with:
24+
path: ~/.sonar/cache
25+
key: ${{ runner.os }}-sonar
26+
restore-keys: ${{ runner.os }}-sonar
27+
- name: Cache Gradle packages
28+
uses: actions/cache@v1
29+
with:
30+
path: ~/.gradle/caches
31+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
32+
restore-keys: ${{ runner.os }}-gradle
33+
- name: Build and analyze
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
36+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
37+
run: ./gradlew build sonarqube --info

.github/workflows/docker-image.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Docker Image CI
2+
3+
on:
4+
push:
5+
branches: [ "master", "develop" ]
6+
pull_request:
7+
branches: [ "master", "develop" ]
8+
9+
jobs:
10+
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Build the Docker image
16+
run: docker build . --file Dockerfile --tag java-gradle:$(date +%s)

.gitignore

Lines changed: 142 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
# Compiled class file
2-
*.class
31

4-
# Log file
5-
*.log
6-
7-
# BlueJ files
8-
*.ctxt
92

10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
3+
.gradle
4+
.vscode
5+
**/logs
126

13-
# Package Files #
7+
*.log
148
*.jar
159
*.war
1610
*.nar
@@ -19,5 +13,141 @@
1913
*.tar.gz
2014
*.rar
2115

22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
16+
# internal
17+
*internal*
18+
19+
# MAC
20+
**/.DS_Store
21+
22+
#-------------------
23+
24+
# Byte-compiled / optimized / DLL files
25+
__pycache__/
26+
*.py[cod]
27+
*$py.class
28+
29+
# C extensions
30+
*.so
31+
32+
# Distribution / packaging
33+
.Python
34+
build/
35+
develop-eggs/
36+
dist/
37+
downloads/
38+
eggs/
39+
.eggs/
40+
# CDK
41+
#lib/
42+
lib64/
43+
parts/
44+
sdist/
45+
var/
46+
wheels/
47+
pip-wheel-metadata/
48+
share/python-wheels/
49+
*.egg-info/
50+
.installed.cfg
51+
*.egg
52+
MANIFEST
53+
54+
# PyInstaller
55+
# Usually these files are written by a python script from a template
56+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
57+
*.manifest
58+
*.spec
59+
60+
# Installer logs
61+
pip-log.txt
62+
pip-delete-this-directory.txt
63+
64+
# Unit test / coverage reports
65+
htmlcov/
66+
.tox/
67+
.nox/
68+
.coverage
69+
.coverage.*
70+
.cache
71+
nosetests.xml
72+
coverage.xml
73+
*.cover
74+
*.py,cover
75+
.hypothesis/
76+
.pytest_cache/
77+
78+
# Translations
79+
*.mo
80+
*.pot
81+
82+
# Django stuff:
83+
*.log
84+
local_settings.py
85+
db.sqlite3
86+
db.sqlite3-journal
87+
88+
# Flask stuff:
89+
instance/
90+
.webassets-cache
91+
92+
# Scrapy stuff:
93+
.scrapy
94+
95+
# Sphinx documentation
96+
docs/_build/
97+
98+
# PyBuilder
99+
target/
100+
101+
# Jupyter Notebook
102+
.ipynb_checkpoints
103+
104+
# IPython
105+
profile_default/
106+
ipython_config.py
107+
108+
# pyenv
109+
.python-version
110+
111+
# pipenv
112+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
113+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
114+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
115+
# install all needed dependencies.
116+
#Pipfile.lock
117+
118+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
119+
__pypackages__/
120+
121+
# Celery stuff
122+
celerybeat-schedule
123+
celerybeat.pid
124+
125+
# SageMath parsed files
126+
*.sage.py
127+
128+
# Environments
129+
.env
130+
.venv
131+
env/
132+
venv/
133+
ENV/
134+
env.bak/
135+
venv.bak/
136+
137+
# Spyder project settings
138+
.spyderproject
139+
.spyproject
140+
141+
# Rope project settings
142+
.ropeproject
143+
144+
# mkdocs documentation
145+
/site
146+
147+
# mypy
148+
.mypy_cache/
149+
.dmypy.json
150+
dmypy.json
151+
152+
# Pyre type checker
153+
.pyre/

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM openjdk:8-jdk-alpine
2+
3+
RUN mkdir -p /opt/build
4+
ADD ./ /opt/build
5+
WORKDIR /opt/build
6+
7+
RUN ./gradlew build --no-daemon \
8+
&& cp ./build/libs/app.jar app.jar
9+
10+
VOLUME /tmp
11+
EXPOSE 8080
12+
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","./app.jar"]

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,44 @@
1-
# java-gradle
2-
Java sample project with Gradle, SpringBoot and Docker
1+
# SpringBoot sample docker image
2+
3+
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=DevSecOpsSamples_java-gradle&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_java-gradle) [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=DevSecOpsSamples_java-gradle&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_java-gradle)
4+
5+
@RequestMapping(value="/", method=RequestMethod.GET)
6+
@RequestMapping(value="/ping", method=RequestMethod.GET)
7+
8+
## AWS
9+
10+
```bash
11+
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
12+
REGION=$(aws configure get default.region)
13+
14+
echo "ACCOUNT_ID: $ACCOUNT_ID"
15+
echo "REGION: $REGION"
16+
sleep 1
17+
18+
docker build -t java-gradle . --platform linux/amd64
19+
20+
aws ecr create-repository --repository-name java-gradle --image-scanning-configuration scanOnPush=true --region $REGION
21+
22+
docker tag java-gradle:latest ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/java-gradle:latest
23+
24+
aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
25+
26+
docker push ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/java-gradle:latest
27+
```
28+
29+
## GCP
30+
31+
```bash
32+
COMPUTE_ZONE="us-central1"
33+
PROJECT_ID="sample-project" # replace with your project
34+
```
35+
36+
```bash
37+
echo "PROJECT_ID: ${PROJECT_ID}"
38+
39+
docker build -t java-gradle . --platform linux/amd64
40+
docker tag java-gradle:latest gcr.io/${PROJECT_ID}/java-gradle:latest
41+
42+
gcloud auth configure-docker
43+
docker push gcr.io/${PROJECT_ID}/java-gradle:latest
44+
```

build.gradle

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
plugins {
2+
id 'org.springframework.boot' version '2.2.1.RELEASE'
3+
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
4+
id 'java'
5+
id 'base'
6+
id "org.sonarqube" version "3.4.0.2513"
7+
}
8+
sourceCompatibility = '1.8'
9+
archivesBaseName = 'app'
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
implementation 'org.springframework.boot:spring-boot-starter-web'
17+
testImplementation('org.springframework.boot:spring-boot-starter-test') {
18+
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
19+
}
20+
}
21+
22+
test {
23+
useJUnitPlatform()
24+
}
25+
26+
sonarqube {
27+
properties {
28+
property "sonar.projectName", "java-gradle"
29+
property "sonar.projectKey", "DevSecOpsSamples_java-gradle"
30+
property "sonar.organization", "devsecopssamples"
31+
// property "sonar.host.url", "http://127.0.0.1:9000"
32+
property "sonar.host.url", "https://sonarcloud.io"
33+
property "sonar.sourceEncoding", "UTF-8"
34+
property "sonar.sources", "."
35+
property "sonar.java.binaries", "build"
36+
property "sonar.exclusions", "**/node_modules/**, **/cdk.out/**"
37+
property "sonar.issue.ignore.multicriteria", "e1"
38+
property "sonar.issue.ignore.multicriteria.e1.ruleKey", "typescript:S1848"
39+
property "sonar.issue.ignore.multicriteria.e1.resourceKey", "**/*.ts"
40+
property "sonar.links.ci", "https://github.com/DevSecOpsSamples/java-gradle"
41+
}
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

build/tmp/bootJar/MANIFEST.MF

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Manifest-Version: 1.0
2+
Main-Class: org.springframework.boot.loader.JarLauncher
3+
Start-Class: com.sec.sample.DemoApplication
4+
Spring-Boot-Version: 2.2.1.RELEASE
5+
Spring-Boot-Classes: BOOT-INF/classes/
6+
Spring-Boot-Lib: BOOT-INF/lib/
7+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
com/sec/sample/SampleController.java
2+
com.sec.sample.SampleController
3+
com/sec/sample/DemoApplication.java
4+
com.sec.sample.DemoApplication

buildspec.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: 0.2
2+
phases:
3+
pre_build:
4+
commands:
5+
- env
6+
- 'export TAG=${CODEBUILD_RESOLVED_SOURCE_VERSION}'
7+
- 'echo TAG: ${TAG}'
8+
finally:
9+
- ls -alh
10+
build:
11+
commands:
12+
- 'docker build -t $ECR_REPO_URI:$TAG .'
13+
- aws --version
14+
- 'aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com'
15+
- 'docker push $ECR_REPO_URI:$TAG'
16+
post_build:
17+
commands:
18+
- echo "[{'name':'fargate-restapi-container','imageUri':'$ECR_REPO_URI:$TAG'}]" > imagedefinitions.json
19+
- pwd; ls -al; cat imagedefinitions.json
20+
artifacts:
21+
files:
22+
- imagedefinitions.json

0 commit comments

Comments
 (0)