Skip to content

Commit 47ade88

Browse files
authored
Merge pull request #3 from DevSecOpsSamples/develop
Dockerfile build with Github Action
2 parents 66238fb + 05f039c commit 47ade88

11 files changed

+431
-22
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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ RUN mkdir -p /opt/build
44
ADD ./ /opt/build
55
WORKDIR /opt/build
66

7-
RUN pwd \
8-
&& ls -alh \
9-
&& ./gradlew build --no-daemon \
10-
&& ls -alh ./build/libs/ \
11-
&& cp ./build/libs/devopssample-java-gradle-0.0.1.jar app.jar
7+
RUN ./gradlew build --no-daemon \
8+
&& cp ./build/libs/app.jar app.jar
129

1310
VOLUME /tmp
1411
EXPOSE 8080

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1-
# SpringBoot sample 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)
24

35
@RequestMapping(value="/", method=RequestMethod.GET)
46
@RequestMapping(value="/ping", method=RequestMethod.GET)
5-
@RequestMapping(value="/serviceid/monitoring/v1/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: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ plugins {
22
id 'org.springframework.boot' version '2.2.1.RELEASE'
33
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
44
id 'java'
5+
id 'base'
6+
id "org.sonarqube" version "3.4.0.2513"
57
}
6-
7-
group = 'devopssample'
8-
version = '0.0.1'
98
sourceCompatibility = '1.8'
9+
archivesBaseName = 'app'
1010

1111
repositories {
1212
mavenCentral()
@@ -19,6 +19,27 @@ dependencies {
1919
}
2020
}
2121

22+
springBoot {
23+
mainClassName = 'com.sample.SampleApplication.java'
24+
}
2225
test {
2326
useJUnitPlatform()
2427
}
28+
29+
sonarqube {
30+
properties {
31+
property "sonar.projectName", "java-gradle"
32+
property "sonar.projectKey", "DevSecOpsSamples_java-gradle"
33+
property "sonar.organization", "devsecopssamples"
34+
// property "sonar.host.url", "http://127.0.0.1:9000"
35+
property "sonar.host.url", "https://sonarcloud.io"
36+
property "sonar.sourceEncoding", "UTF-8"
37+
property "sonar.sources", "."
38+
property "sonar.java.binaries", "build"
39+
property "sonar.exclusions", "**/node_modules/**, **/cdk.out/**"
40+
property "sonar.issue.ignore.multicriteria", "e1"
41+
property "sonar.issue.ignore.multicriteria.e1.ruleKey", "typescript:S1848"
42+
property "sonar.issue.ignore.multicriteria.e1.resourceKey", "**/*.ts"
43+
property "sonar.links.ci", "https://github.com/DevSecOpsSamples/java-gradle"
44+
}
45+
}

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)