Skip to content

Commit

Permalink
👷 Add auto-release to DockerHub
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColman committed Jan 2, 2024
1 parent 55f9c19 commit 12a5de0
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 27 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/release.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env kotlin

@file:DependsOn("io.github.typesafegithub:github-workflows-kt:1.8.0")

import io.github.typesafegithub.workflows.actions.actions.CheckoutV4
import io.github.typesafegithub.workflows.actions.actions.SetupJavaV4
import io.github.typesafegithub.workflows.actions.azure.DockerLoginV1
import io.github.typesafegithub.workflows.actions.docker.BuildPushActionV5
import io.github.typesafegithub.workflows.actions.gradle.GradleBuildActionV2
import io.github.typesafegithub.workflows.domain.RunnerType.UbuntuLatest
import io.github.typesafegithub.workflows.domain.triggers.Release
import io.github.typesafegithub.workflows.dsl.expressions.Contexts
import io.github.typesafegithub.workflows.dsl.expressions.expr
import io.github.typesafegithub.workflows.dsl.workflow
import io.github.typesafegithub.workflows.yaml.writeToFile

val version = expr { Contexts.github.ref }
val DOCKER_HUB_USERNAME by Contexts.secrets
val DOCKER_HUB_TOKEN by Contexts.secrets
val dockerNamespace = "jopiterapp/jopiter-backend"

workflow(
name = "Release",
on = listOf(Release()),
sourceFile = __FILE__.toPath()
) {
job(id = "Release", runsOn = UbuntuLatest) {
uses(
name = "Setup JDK",
action = SetupJavaV4(javaVersion = "17", distribution = SetupJavaV4.Distribution.Adopt)
)
uses(name = "Checkout", action = CheckoutV4())
uses(name = "Create ShadowJar", action = GradleBuildActionV2(arguments = ":bootJar"))
uses(
name = "Login to DockerHub",
action = DockerLoginV1(username = expr { DOCKER_HUB_USERNAME }, password = expr { DOCKER_HUB_TOKEN })
)
uses(
name = "Build and Push",
action = BuildPushActionV5(
context = ".",
push = true,
tags = listOf("$dockerNamespace/latest", "$dockerNamespace/$version")
)
)
}
}.writeToFile()
79 changes: 52 additions & 27 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@
name: Release
on:
push:
branches:
- master
paths-ignore:
- '**.md'
# This file was generated using Kotlin DSL (.github/workflows/release.main.kts).
# If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file.
# Generated with https://github.com/typesafegithub/github-workflows-kt

name: 'Release'
on:
release: {}
jobs:
release:
env:
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
runs-on: ubuntu-latest
check_yaml_consistency:
name: 'Check YAML consistency'
runs-on: 'ubuntu-latest'
steps:
- id: 'step-0'
name: 'Check out'
uses: 'actions/checkout@v4'
- id: 'step-1'
name: 'Execute script'
run: 'rm ''.github/workflows/release.yaml'' && ''.github/workflows/release.main.kts'''
- id: 'step-2'
name: 'Consistency check'
run: 'git diff --exit-code ''.github/workflows/release.yaml'''
Release:
runs-on: 'ubuntu-latest'
needs:
- 'check_yaml_consistency'
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Create ShadowJar
run: ./gradlew :bootJar
- name: Deploy to Beanstalk
uses: einaregilsson/beanstalk-deploy@v16
with:
application_name: Jopiter Backend
environment_name: Jopiterbackend-env
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
region: sa-east-1
version_label: ${{ github.run_number }}
deployment_package: build/libs/jopiter-backend.jar
- id: 'step-0'
name: 'Setup JDK'
uses: 'actions/setup-java@v4'
with:
java-version: '17'
distribution: 'adopt'
- id: 'step-1'
name: 'Checkout'
uses: 'actions/checkout@v4'
- id: 'step-2'
name: 'Create ShadowJar'
uses: 'gradle/gradle-build-action@v2'
with:
arguments: ':bootJar'
- id: 'step-3'
name: 'Login to DockerHub'
uses: 'Azure/docker-login@v1'
with:
username: '${{ secrets.DOCKER_HUB_USERNAME }}'
password: '${{ secrets.DOCKER_HUB_TOKEN }}'
- id: 'step-4'
name: 'Build and Push'
uses: 'docker/build-push-action@v5'
with:
context: '.'
push: 'true'
tags: |-
jopiterapp/jopiter-backend/latest
jopiterapp/jopiter-backend/${{ github.ref }}
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM eclipse-temurin:11
ADD build/libs/jopiter-uber.jar jopiter-uber.jar
EXPOSE 5000
CMD java -jar jopiter-uber.jar
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED
import org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.tasks.bundling.BootJar

version = "2.3.0"
group = "app.jopiter"
Expand Down Expand Up @@ -109,6 +110,10 @@ allprojects {
}
}

tasks.named<BootJar>("bootJar") {
archiveFileName.set("jopiter-uber.jar")
}

dependencies {
implementation(project(":privacy"))
implementation(project(":restaurants"))
Expand Down

0 comments on commit 12a5de0

Please sign in to comment.