Skip to content

Commit

Permalink
app2 with jib (#5)
Browse files Browse the repository at this point in the history
* app2 with jib

* enable buildx-docker-cli-oci

* enable buildx-docker-oci

* fix context

* fix file: app2/Dockerfile.builder

* fix comment

* try diff builder path

* enable buildx-docker
  • Loading branch information
bbednarek authored Nov 14, 2023
1 parent 0840262 commit a98dac3
Show file tree
Hide file tree
Showing 22 changed files with 634 additions and 146 deletions.
4 changes: 0 additions & 4 deletions .dockerignore

This file was deleted.

48 changes: 48 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build app

env:
ORG_GRADLE_PROJECT_githubUser: ${{ github.actor }}
ORG_GRADLE_PROJECT_githubPackagesReadToken: ${{ secrets.GH_PACKAGES_READ_TOKEN }}

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 60
env:
DOCKER_IMAGE: bartbednarek/multiple-docker-build-app
concurrency:
group: build-${{ github.ref }}
cancel-in-progress: true
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Checkout Repository
uses: actions/checkout@v4

- uses: actions/setup-java@v3
with:
distribution: 'adopt'
java-version: '17'
cache: 'gradle'

- name: Build Project
run: ./gradlew clean build --no-daemon

- name: Set Docker Image
run: echo "TF_VAR_image=${{ env.DOCKER_IMAGE }}:$GITHUB_RUN_NUMBER" >> $GITHUB_ENV

- name: Docker Build & Push
if: github.ref == 'refs/heads/master'
run: |
./gradlew :app:jib --no-daemon --image=$TF_VAR_image
18 changes: 4 additions & 14 deletions .github/workflows/buildx-docker-cli-oci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,21 @@ jobs:
platforms: ${{ env.PLATFORMS }}

- name: Docker local builder image
working-directory: app2
run: >
docker buildx build
--build-arg githubUser=${{ github.actor }}
--build-arg githubPackagesReadToken=${{ secrets.GH_PACKAGES_READ_TOKEN }}
--platform=${{ env.PLATFORMS }}
--output type=oci,dest=builder-image,tar=false
.
-f Dockerfile.builder
# run is cli version of
# uses: docker/build-push-action@v5
# with:
# file: Dockerfile.builder
# build-args: |
# githubUser=${{ github.actor }}
# githubPackagesReadToken=${{ secrets.GH_PACKAGES_READ_TOKEN }}
# platforms: ${{ env.PLATFORMS }}
# push: false
# outputs: |
# type=oci,dest=builder-image,tar=false
- name: Docker Build & Push
uses: docker/build-push-action@v5
with:
with:
context: app2
build-contexts: |
builder=oci-layout://./builder-image
builder=oci-layout://./app2/builder-image
platforms: ${{ env.PLATFORMS }}
push: ${{ github.ref == 'refs/heads/main' }}
tags: |
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/buildx-docker-oci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ jobs:
- name: Docker local builder image
uses: docker/build-push-action@v5
with:
file: Dockerfile.builder
context: app2
file: app2/Dockerfile.builder
build-args: |
githubUser=${{ github.actor }}
githubPackagesReadToken=${{ secrets.GH_PACKAGES_READ_TOKEN }}
platforms: ${{ env.PLATFORMS }}
push: false
outputs: |
type=oci,dest=builder-image,tar=false
- name: Docker Build & Push
uses: docker/build-push-action@v5
with:
with:
context: app2
build-contexts: |
builder=oci-layout://./builder-image
platforms: ${{ env.PLATFORMS }}
Expand Down
76 changes: 0 additions & 76 deletions .github/workflows/buildx-docker-old-actions-versions-oci.yaml

This file was deleted.

16 changes: 8 additions & 8 deletions .github/workflows/buildx-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ env:
PLATFORMS: ${{ vars.PLATFORMS }}

on:
# Disabled, because it times out after 60 min anyway...
# push:
# branches: [ main ]
# pull_request:
# branches: [ main ]
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
inputs:
platforms:
Expand Down Expand Up @@ -56,17 +55,18 @@ jobs:
- name: Docker local builder image
uses: docker/build-push-action@v5
with:
file: Dockerfile.builder
context: app2
file: app2/Dockerfile.builder
build-args: |
githubUser=${{ github.actor }}
githubPackagesReadToken=${{ secrets.GH_PACKAGES_READ_TOKEN }}
platforms: ${{ env.PLATFORMS }}
push: false
outputs: builder-image

- name: Docker Build & Push
uses: docker/build-push-action@v5
with:
with:
context: app2
build-contexts: |
builder=builder-image
platforms: ${{ env.PLATFORMS }}
Expand Down
35 changes: 24 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,47 @@ IMAGE_NAME=multiple-docker-build
PLATFORMS=`([ "$(uname -m)" = "x86_64" ] && echo "linux/amd64") || echo "linux/arm64"`
GH_USER=`git config user.name`
GH_PACKAGES_READ_TOKEN=`./gradlew printGithubPackagesReadToken -q`
DOCKER_USERNAME=`./gradlew printDockerUsername -q`
DOCKER_TOKEN=`./gradlew printDockerToken -q`
VERSION=

all: clean test

clean:
./gradlew clean
cd app2; ./gradlew clean
docker image rm -f ${IMAGE_NAME}
rm -rf builder-image

build:
build-app:
./gradlew build --no-daemon
cd app2; ./gradlew build --no-daemon

build-docker-jib:
if [ -z "$GH_PACKAGES_READ_TOKEN" ]; then echo "ERROR: GH_PACKAGES_READ_TOKEN env variable is not defined. Please check project README."; exit 1; fi
./gradlew build
if [ -z "$DOCKER_USERNAME" ]; then echo "ERROR: DOCKER_USERNAME env variable is not defined. Please check project README."; exit 1; fi
if [ -z "$DOCKER_TOKEN" ]; then echo "ERROR: DOCKER_TOKEN env variable is not defined. Please check project README."; exit 1; fi
./gradlew :app:jib --no-daemon -PgithubUser=${GH_USER} -PgithubPackagesReadToken=${GH_PACKAGES_READ_TOKEN} -Djib.to.auth.username=${DOCKER_USERNAME} -Djib.to.auth.password=${DOCKER_TOKEN}

buildx-docker:
docker buildx build --build-arg githubUser=${GH_USER} --build-arg githubPackagesReadToken=${GH_PACKAGES_READ_TOKEN} --platform=${PLATFORMS} --output builder-image . -f Dockerfile.builder
docker buildx build --load --platform=${PLATFORMS} --build-context builder=builder-image . -t ${IMAGE_NAME}
if [ -z "$GH_PACKAGES_READ_TOKEN" ]; then echo "ERROR: GH_PACKAGES_READ_TOKEN env variable is not defined. Please check project README."; exit 1; fi
cd app2; docker buildx build --build-arg githubUser=${GH_USER} --build-arg githubPackagesReadToken=${GH_PACKAGES_READ_TOKEN} --platform=${PLATFORMS} --output builder-image . -f Dockerfile.builder
cd app2; docker buildx build --load --platform=${PLATFORMS} --build-context builder=builder-image . -t ${IMAGE_NAME}

buildx-docker-oci:
docker buildx build --build-arg githubUser=${GH_USER} --build-arg githubPackagesReadToken=${GH_PACKAGES_READ_TOKEN} --platform=${PLATFORMS} --output type=oci,dest=builder-image,tar=false . -f Dockerfile.builder
docker buildx build --load --platform=${PLATFORMS} --build-context builder=oci-layout://./builder-image . -t ${IMAGE_NAME}
if [ -z "$GH_PACKAGES_READ_TOKEN" ]; then echo "ERROR: GH_PACKAGES_READ_TOKEN env variable is not defined. Please check project README."; exit 1; fi
cd app2; docker buildx build --build-arg githubUser=${GH_USER} --build-arg githubPackagesReadToken=${GH_PACKAGES_READ_TOKEN} --platform=${PLATFORMS} --output type=oci,dest=builder-image,tar=false . -f Dockerfile.builder
cd app2; docker buildx build --load --platform=${PLATFORMS} --build-context builder=oci-layout://./builder-image . -t ${IMAGE_NAME}

build-docker:
docker build --build-arg githubUser=${GH_USER} --build-arg githubPackagesReadToken=${GH_PACKAGES_READ_TOKEN} --output builder-image . -f Dockerfile.builder
docker build --load --build-context builder=builder-image . -t ${IMAGE_NAME}
if [ -z "$GH_PACKAGES_READ_TOKEN" ]; then echo "ERROR: GH_PACKAGES_READ_TOKEN env variable is not defined. Please check project README."; exit 1; fi
cd app2; docker build --build-arg githubUser=${GH_USER} --build-arg githubPackagesReadToken=${GH_PACKAGES_READ_TOKEN} --output builder-image . -f Dockerfile.builder
cd app2; docker build --load --build-context builder=builder-image . -t ${IMAGE_NAME}

test: buildx-docker
./gradlew clean test
test:
./gradlew test
cd app2; ./gradlew test

release: build
release:
if [ -z "$VERSION" ]; then echo "ERROR: Usage is: make VERSION=<version> release"; exit 1; fi
./gradlew -Pversion=$VERSION clean build publish
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ Example of building Dockerfile using multiple files

## local setup
In order to run the pipeline locally you need to define following properties in `~/.gradle/gradle.properties`.
Tokens can be generated [here](https://github.com/settings/tokens).
GitHub's tokens can be generated [here](https://github.com/settings/tokens).
Docker's tokens can be generated [here](https://hub.docker.com/settings/security).
```
githubUser=<github_username>
githubPackagesReadToken=<token with: (repo, write:packages) scope>
githubToken=<token with: (repo, write:packages) scope>
githubPackagesReadToken=<token with: (repo, read:packages) scope>
dockerUsername=<docker_username>>
dockerToken=<docker token with: (Read, Write, Delete) scope>
```
36 changes: 35 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ plugins {
id("org.jlleitschuh.gradle.ktlint") version "11.3.1"
id("maven-publish")
id("java-library")
id("com.google.cloud.tools.jib") version "3.3.1"

kotlin("jvm") version "1.7.0"
kotlin("kapt") version "1.7.0"
}

group = "com.bbednarek"
group = "com.bbednarek.app"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

Expand Down Expand Up @@ -62,12 +63,45 @@ tasks.withType<Test> {
}
}

jib {
from {
image = "eclipse-temurin:17-jre-jammy"
platforms {
platform {
architecture = "amd64"
os = "linux"
}
}
}

to {
image = "bartbednarek/multiple-docker-build-app"
// tags = setOf("latest")
}

container {
entrypoint = listOf("INHERIT")
}
}

tasks.register("printGithubPackagesReadToken") {
doFirst {
println(project.property("githubPackagesReadToken"))
}
}

tasks.register("printDockerUsername") {
doFirst {
println(project.property("dockerUsername"))
}
}

tasks.register("printDockerToken") {
doFirst {
println(project.property("dockerToken"))
}
}

val sourcesJar by tasks.registering(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/kotlin/com/github/bbednarek/App.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package com.github.bbednarek

class App {
Expand Down
4 changes: 4 additions & 0 deletions app2/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dockerignore
builder-image
app2/Dockerfile
app2/Dockerfile.builder
4 changes: 2 additions & 2 deletions Dockerfile → app2/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM gradle:8.3.0-jdk17-jammy

COPY --from=builder /home/gradle/.gradle/caches /home/gradle/.gradle/caches
COPY --from=builder /home/gradle/app /home/gradle/app
COPY --from=builder /home/gradle/multiple-docker-build /home/gradle/multiple-docker-build

WORKDIR /home/gradle/app
WORKDIR /home/gradle/multiple-docker-build

ENTRYPOINT ["gradle", "cleanTest", "test", "--no-daemon"]
4 changes: 2 additions & 2 deletions Dockerfile.builder → app2/Dockerfile.builder
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ FROM gradle:8.3.0-jdk17-jammy
ARG githubUser
ARG githubPackagesReadToken

WORKDIR app
WORKDIR multiple-docker-build

COPY . .
COPY .. .

RUN gradle -PgithubUser=$githubUser -PgithubPackagesReadToken=$githubPackagesReadToken clean build --no-daemon
Loading

0 comments on commit a98dac3

Please sign in to comment.