Skip to content

Commit 10b2e33

Browse files
committed
.
1 parent eaea425 commit 10b2e33

File tree

15 files changed

+311
-62
lines changed

15 files changed

+311
-62
lines changed

.github/workflows/build-java.yml

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ jobs:
1919
uses: actions/setup-java@v1
2020
with:
2121
java-version: 11
22-
# - name: Cache SonarCloud packages
23-
# uses: actions/cache@v1
24-
# with:
25-
# path: ~/.sonar/cache
26-
# key: ${{ runner.os }}-sonar
27-
# restore-keys: ${{ runner.os }}-sonar
2822
- run: ls -alh
2923
- name: Cache Gradle packages
3024
uses: actions/cache@v1
@@ -41,20 +35,20 @@ jobs:
4135
run: ./gradlew build --info
4236
working-directory: "java"
4337

44-
no-cache:
45-
runs-on: ubuntu-latest
46-
steps:
47-
- uses: actions/checkout@v3
48-
with:
49-
path: java
50-
fetch-depth: 0
51-
- name: Set up JDK 11
52-
uses: actions/setup-java@v1
53-
with:
54-
java-version: 11
55-
- name: Build and analyze
56-
env:
57-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
58-
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
59-
run: ./gradlew build --info
60-
working-directory: "java"
38+
# no-cache:
39+
# runs-on: ubuntu-latest
40+
# steps:
41+
# - uses: actions/checkout@v3
42+
# with:
43+
# path: java
44+
# fetch-depth: 0
45+
# - name: Set up JDK 11
46+
# uses: actions/setup-java@v1
47+
# with:
48+
# java-version: 11
49+
# - name: Build and analyze
50+
# env:
51+
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52+
# # SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
53+
# run: ./gradlew build --info
54+
# working-directory: "java"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Build & Sonarqube
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- develop
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
gradle-sonarqube-cache:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up JDK 11
16+
uses: actions/setup-java@v1
17+
with:
18+
java-version: 11
19+
- name: Cache Gradle packages
20+
uses: actions/cache@v3
21+
with:
22+
path: |
23+
~/.gradle/caches
24+
~/.gradle/wrapper
25+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }}
26+
restore-keys: ${{ runner.os }}-gradle
27+
- name: Cache SonarCloud packages
28+
uses: actions/cache@v3
29+
with:
30+
path: ~/.sonar/cache
31+
key: ${{ runner.os }}-sonar
32+
restore-keys: ${{ runner.os }}-sonar
33+
- name: Build and analyze
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
37+
run: ./gradlew build sonarqube --info
38+
39+
gradle-sonarqube-no-cache:
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v3
43+
- name: Set up JDK 11
44+
uses: actions/setup-java@v1
45+
with:
46+
java-version: 11
47+
- name: Build and analyze
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
51+
run: ./gradlew build sonarqube --info
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Build
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- develop
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
gradle-cache:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up JDK 11
16+
uses: actions/setup-java@v1
17+
with:
18+
java-version: 11
19+
- name: Cache Gradle packages
20+
uses: actions/cache@v3
21+
with:
22+
path: |
23+
~/.gradle/caches
24+
~/.gradle/wrapper
25+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }}
26+
restore-keys: ${{ runner.os }}-gradle
27+
- name: Build and analyze
28+
run: ./gradlew build --info
29+
30+
gradle-no-cache:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v3
34+
- name: Set up JDK 11
35+
uses: actions/setup-java@v1
36+
with:
37+
java-version: 11
38+
- name: Build and analyze
39+
run: ./gradlew build --info

java/README.md

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,84 @@
1-
# SpringBoot sample docker image
1+
# Optimize Gradle build speed using cache plugin
22

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)
3+
## GitHub Workflow
44

5-
@RequestMapping(value="/", method=RequestMethod.GET)
6-
@RequestMapping(value="/ping", method=RequestMethod.GET)
7-
8-
## AWS
5+
[.github/workflows/build-java.yml](.github/workflows/build-java.yml):
96

107
```bash
11-
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
12-
REGION=$(aws configure get default.region)
8+
name: Build
9+
on:
10+
push:
11+
branches:
12+
- master
13+
- develop
14+
pull_request:
15+
types: [opened, synchronize, reopened]
1316

14-
echo "ACCOUNT_ID: $ACCOUNT_ID"
15-
echo "REGION: $REGION"
16-
sleep 1
17+
jobs:
18+
gradle-cache:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v3
22+
- name: Set up JDK 11
23+
uses: actions/setup-java@v1
24+
with:
25+
java-version: 11
26+
- name: Cache Gradle packages
27+
uses: actions/cache@v3
28+
with:
29+
path: |
30+
~/.gradle/caches
31+
~/.gradle/wrapper
32+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }}
33+
restore-keys: ${{ runner.os }}-gradle
34+
- name: Build and analyze
35+
run: ./gradlew build --info
36+
37+
gradle-no-cache:
38+
runs-on: ubuntu-latest
39+
steps:
40+
- uses: actions/checkout@v3
41+
- name: Set up JDK 11
42+
uses: actions/setup-java@v1
43+
with:
44+
java-version: 11
45+
- name: Build and analyze
46+
run: ./gradlew build --info
47+
```
1748

18-
docker build -t java-gradle . --platform linux/amd64
49+
[.github/workflows/build-java-sonarqube.yml](.github/workflows/build-java-sonarqube.yml):
1950

20-
aws ecr create-repository --repository-name java-gradle --image-scanning-configuration scanOnPush=true --region $REGION
51+
### Time Taken
2152

22-
docker tag java-gradle:latest ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/java-gradle:latest
53+
| Action Step | Cache Miss | Cache Hit |
54+
|------------------------|-------------|------------|
55+
| Set up JDK 11 | 6s | 6s |
56+
| Cache Gradle packages | 0s | 8s |
57+
| Grable build | 33s | 8s |
2358

24-
aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
59+
You can speed up build time 17 seconds.
2560

26-
docker push ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/java-gradle:latest
27-
```
61+
- Cache miss:
2862

29-
## GCP
63+
![cache-miss](screenshots/cache-miss.png?raw=true)
3064

31-
```bash
32-
COMPUTE_ZONE="us-central1"
33-
PROJECT_ID="sample-project" # replace with your project
34-
```
65+
Download gradle wrapper and SpringBoot dependencies.
3566

36-
```bash
37-
echo "PROJECT_ID: ${PROJECT_ID}"
67+
- Cache hit:
3868

39-
docker build -t java-gradle . --platform linux/amd64
40-
docker tag java-gradle:latest gcr.io/${PROJECT_ID}/java-gradle:latest
69+
![cache-hit](screenshots/cache-hit.png?raw=true)
4170

42-
gcloud auth configure-docker
43-
docker push gcr.io/${PROJECT_ID}/java-gradle:latest
44-
```
71+
![github-cache](screenshots/github-cache.png?raw=true)
72+
73+
Run details in usage menu:
74+
75+
![build-time-in-usage](screenshots/build-time-in-usage.png?raw=true)
76+
77+
78+
## Reference
79+
80+
- [GitHub Actions /Using workflows / Cache dependencies / Caching dependencies to speed up workflows](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#managing-caches)
81+
82+
- https://github.com/actions/cache
83+
84+
- https://github.com/actions/cache/blob/main/examples.md#java---gradle

java/build-java-sonarqube.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Build & Sonarqube
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- develop
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
gradle-sonarqube-cache:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up JDK 11
16+
uses: actions/setup-java@v1
17+
with:
18+
java-version: 11
19+
- name: Cache Gradle packages
20+
uses: actions/cache@v3
21+
with:
22+
path: |
23+
~/.gradle/caches
24+
~/.gradle/wrapper
25+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }}
26+
restore-keys: ${{ runner.os }}-gradle
27+
- name: Cache SonarCloud packages
28+
uses: actions/cache@v3
29+
with:
30+
path: ~/.sonar/cache
31+
key: ${{ runner.os }}-sonar
32+
restore-keys: ${{ runner.os }}-sonar
33+
- name: Build and analyze
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
37+
run: ./gradlew build sonarqube --info
38+
39+
gradle-sonarqube-no-cache:
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v3
43+
- name: Set up JDK 11
44+
uses: actions/setup-java@v1
45+
with:
46+
java-version: 11
47+
- name: Build and analyze
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
51+
run: ./gradlew build sonarqube --info

java/build-java.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Build
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- develop
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
gradle-cache:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up JDK 11
16+
uses: actions/setup-java@v1
17+
with:
18+
java-version: 11
19+
- name: Cache Gradle packages
20+
uses: actions/cache@v3
21+
with:
22+
path: |
23+
~/.gradle/caches
24+
~/.gradle/wrapper
25+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/gradle-wrapper.properties') }}
26+
restore-keys: ${{ runner.os }}-gradle
27+
- name: Build and analyze
28+
run: ./gradlew build --info
29+
30+
gradle-no-cache:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v3
34+
- name: Set up JDK 11
35+
uses: actions/setup-java@v1
36+
with:
37+
java-version: 11
38+
- name: Build and analyze
39+
run: ./gradlew build --info

java/build.gradle

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
44
id 'java'
55
id 'base'
6-
id "org.sonarqube" version "3.4.0.2513"
6+
id "org.sonarqube" version "3.5.0.2730"
77
}
88
sourceCompatibility = '1.8'
99
archivesBaseName = 'app'
@@ -28,18 +28,15 @@ test {
2828

2929
sonarqube {
3030
properties {
31-
property "sonar.projectName", "java-gradle"
32-
property "sonar.projectKey", "DevSecOpsSamples_java-gradle"
33-
property "sonar.organization", "devsecopssamples"
31+
property "sonar.projectName", "test-githubaction-java"
32+
// property "sonar.projectKey", "DevSecOpsSamples_test-githubaction-java"
33+
// property "sonar.organization", "devsecopssamples"
34+
property "sonar.projectKey", "engel80-todo_test-githubaction-java"
35+
property "sonar.organization", "engel80-todo"
3436
// property "sonar.host.url", "http://127.0.0.1:9000"
3537
property "sonar.host.url", "https://sonarcloud.io"
3638
property "sonar.sourceEncoding", "UTF-8"
37-
property "sonar.sources", "."
3839
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"
40+
property "sonar.links.ci", "https://github.com/DevSecOpsSamples/test-githubaction-java/actions"
4441
}
4542
}

0 commit comments

Comments
 (0)