|
1 | | -# SpringBoot sample docker image |
| 1 | +# Optimize Gradle build speed using cache plugin |
2 | 2 |
|
3 | | -[](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_java-gradle) [](https://sonarcloud.io/summary/new_code?id=DevSecOpsSamples_java-gradle) |
| 3 | +## GitHub Workflow |
4 | 4 |
|
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): |
9 | 6 |
|
10 | 7 | ```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] |
13 | 16 |
|
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 | +``` |
17 | 48 |
|
18 | | -docker build -t java-gradle . --platform linux/amd64 |
| 49 | +[.github/workflows/build-java-sonarqube.yml](.github/workflows/build-java-sonarqube.yml): |
19 | 50 |
|
20 | | -aws ecr create-repository --repository-name java-gradle --image-scanning-configuration scanOnPush=true --region $REGION |
| 51 | +### Time Taken |
21 | 52 |
|
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 | |
23 | 58 |
|
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. |
25 | 60 |
|
26 | | -docker push ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/java-gradle:latest |
27 | | -``` |
| 61 | +- Cache miss: |
28 | 62 |
|
29 | | -## GCP |
| 63 | +  |
30 | 64 |
|
31 | | -```bash |
32 | | -COMPUTE_ZONE="us-central1" |
33 | | -PROJECT_ID="sample-project" # replace with your project |
34 | | -``` |
| 65 | + Download gradle wrapper and SpringBoot dependencies. |
35 | 66 |
|
36 | | -```bash |
37 | | -echo "PROJECT_ID: ${PROJECT_ID}" |
| 67 | +- Cache hit: |
38 | 68 |
|
39 | | -docker build -t java-gradle . --platform linux/amd64 |
40 | | -docker tag java-gradle:latest gcr.io/${PROJECT_ID}/java-gradle:latest |
| 69 | +  |
41 | 70 |
|
42 | | -gcloud auth configure-docker |
43 | | -docker push gcr.io/${PROJECT_ID}/java-gradle:latest |
44 | | -``` |
| 71 | +  |
| 72 | + |
| 73 | +Run details in usage menu: |
| 74 | + |
| 75 | + |
| 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 |
0 commit comments