Automate your workflows with Kotlin Fosdem - 2020 1 Automate your - - PowerPoint PPT Presentation

automate your workflows with kotlin
SMART_READER_LITE
LIVE PREVIEW

Automate your workflows with Kotlin Fosdem - 2020 1 Automate your - - PowerPoint PPT Presentation

Automate your workflows with Kotlin Fosdem - 2020 1 Automate your workflows with Kotlin @martinbonnin @mgauzins 2 A daily work... 1. Assign a ticket 2. Create a branch 3. Code... 4. Create a pull request But also... 5. Move


slide-1
SLIDE 1

Automate your workflows with Kotlin

Fosdem - 2020

1

slide-2
SLIDE 2

Automate your workflows with Kotlin

@martinbonnin @mgauzins

2

slide-3
SLIDE 3

A daily work...

1. Assign a ticket 2. Create a branch 3. Code... 🤔 4. Create a pull request 5. Move ticket state 6. Merge pull request 7. Move ticket state 8. Create an alpha a. Increment a version b. Tag c. Push 9. Send a message to designers/product owners 10. Integrate feedbacks 11. Back to step 1

  • Manage app translations
  • Keep the store app up to date

(images, listings, archives)

  • Manage app rollout
  • Notify about the updates
  • Publish to alternative stores

But also...

3

slide-4
SLIDE 4

Environment

4

Appcenter Slack Jira Github PlayStore CI Transifex

slide-5
SLIDE 5

Why automating ?

  • Reliability
  • Reproducibility
  • Documentation
  • Fun
  • Kotlin to the rescue

5

  • It takes times.

https://xkcd.com/1319/

slide-6
SLIDE 6

Why Kotlin?

  • The language we use every day

○ No context change (bash syndrome) ○ Every team member knows it

  • Modern
  • Great IDE
  • Great Ecosystem

6

slide-7
SLIDE 7

What did we replace ?

7

Fastlane (Ruby) Transifex cli (Ruby) after_success.sh (Bash) generate_docs.sh (Bash) Github hub (Go) build.gradle (Groovy) Ad-hoc scripts 3rd party tools Build system General purpose tools

slide-8
SLIDE 8
  • Kotlin scripts

○ Based on Kscript

  • Kotlin command line app (cli)

○ Called Kinta ○ Based on Clikt

Tools we used

8

slide-9
SLIDE 9

Kscript

9

slide-10
SLIDE 10

Scripting - motivations

10

  • For short projects/single file projects
  • No need for gradle
  • Easy to setup/use
slide-11
SLIDE 11

Scripting - simple example

// hello.kts println("Hello ${args[0]}!") // running the script $ kotlinc -script hello.kts Fosdem Hello Fosdem!

11

https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md

slide-12
SLIDE 12

Kscript - scripting improvements

  • Compiled script caching
  • Shebang and interpreter usage
  • Dependencies
  • Standalone binaries
  • IDE support
  • https://github.com/holgerbrandl/kscript

12

slide-13
SLIDE 13

Kscript - installation

curl -s "https://get.sdkman.io" | bash # install sdkman source ~/.bash_profile # add sdkman to PATH sdk install kotlin # install Kotlin sdk install kscript # install Kscript touch hello.kts kscript --idea hello.kts # start the IDE

13

slide-14
SLIDE 14

Kscript

14

// weekend.kts #!/usr/bin/env kscript @file:DependsOn("com.squareup.okhttp3:okhttp :4.3.1") import okhttp3.OkHttpClient import okhttp3.Request val weekend = Request.Builder().get().url("http://isitweek endyet.com/").build().let { OkHttpClient().newCall(it) }.execute().body!!.string().toLowerCase().co ntains("yes") if (weekend) { println("It is the weekend!") } else { println("Not yet :-|") } $ chmod +x weekend.kts $ ./weekend.kts It is the weekend!

slide-15
SLIDE 15

Kscript - IDE

15

$ kscript --idea weekend.kts

slide-16
SLIDE 16

Kscript - Debugger

16

slide-17
SLIDE 17

Kscript - Real life examples

17

  • Generating project website (mkdocs + github pages)
  • Install scripts
  • Migration from build.gradle to build.gradle.kts
  • Finding duplicates in string.xml
  • Categorizing my expenses !
  • etc...
slide-18
SLIDE 18

Scripting - limitations

18

  • JVM required
  • JVM startup time
  • Multiple files is hard to maintain
  • No Gradle => no plugins, kapt, etc...
slide-19
SLIDE 19

Kinta: a Kotlin Cli

19

slide-20
SLIDE 20

20

03 04

Help Commands customisable, nested commands Argument/Option composable, type safe, prompt, default

➔ Open Source library ➔ Command Line Interface for Kotlin

CLIKT : Presentation

https://github.com/ajalt/clikt

slide-21
SLIDE 21

CLIKT to Kinta

  • We need entry points for workflows then

commands

  • Provide a simple way to launch theses

commands by anyone. (Command line interface)

  • Reach even more platforms

21

slide-22
SLIDE 22

Kinta CLI integration

  • apply plugin: 'application'
  • Create a jar
  • Specify the ‘Main’ class
  • Generate starting scripts

plugins { application } tasks.withType<Jar> { archiveFileName.set("kinta-cli.jar") manifest { attributes("Main-Class" to com.dailymotion.kinta.MainKt") } from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) }) } application { mainClassName = "com.dailymotion.kinta.MainKt" }

22

build.gradle.kts

slide-23
SLIDE 23

PublishPlayStore workflow

  • What is a workflow?
  • Workflow detail

○ Upload archive ○ Create a release on a specific track ○ Find a local changelog for the version ○ Upload the changelog

23

kinta publish beta --archiveFile=app-release.aab

slide-24
SLIDE 24

PublishPlayStore workflow

  • bject PublishPlayStore : CliktCommand(

name = "publish", help = "Publish a version on the given track") { private val track by argument("--track", help = "The Play Store track") private val archiveFile by argument("--archiveFile") private val percent by option("--percent", help = "The user fraction receiving the update").double()

  • verride fun run() {

// Beautiful code is coming... } }

  • verride fun run() {

PlayStoreIntegration.uploadDraft( archiveFile = File(archiveFile), track = track ) PlayStoreIntegration.createRelease( track = track, listVersionCodes = listOf(versionCode), percent = percentToApply ) val changeLogs = LocalMetadataHelper.getChangelog(versionCode) PlayStoreIntegration.uploadWhatsNew( versionCode = versionCode, whatsNewProvider = changeLogs ) }

24

slide-25
SLIDE 25

A Swiss knife

Git - tickets

  • startWork
  • PR

Translations

  • txPull
  • txPush
  • txPR

Common tools

  • trigger
  • branch
  • hotfix
  • cleanLocal
  • cleanRemote

Play Store metadatas

  • uploadWhatsNew
  • uploadListing
  • uploadScreenshots
  • generateScreenshots

Play Store releases

  • beta
  • release

Builds

  • nightly
  • buildPR
  • buildTag

25

slide-26
SLIDE 26

The daily work becomes simpler!

  • 4. Create a pull request
  • 5. Move ticket state

kinta startWork {TICKET_ID}

  • 1. Assign a ticket
  • 2. Create a branch
  • 7. Move ticket state
  • 8. Create an alpha

Increment version Tag Push

  • 9. Send a message
  • 3. Code
  • 6. Merge pull request

(Sorry you definitely have to write code) kinta pr Keep the validation on GitHub interface kinta nightly

26

slide-27
SLIDE 27

What’s next

27

slide-28
SLIDE 28

Kinta - customization

28

  • Make the kinta tool usable outside Dailymotion
  • 3rd party services have a well defined API…
  • … but every organization has their own processes and workflows.
  • There’s a fine line between customization and reuse
slide-29
SLIDE 29

Kinta - Integration and Workflows

29

An Integration is:

  • A Kotlin class linked to a specific

domain: ○ Github ○ Transifex ○ etc...

  • Highly reusable
  • Redistributed
  • Static

○ It doesn’t change often

  • Composed of atomic methods
  • Documented using Kdoc
  • Inside the redistributed

kinta-integrations artifact

A Workflow is:

  • A Clikt command for a specific

complex task: ○ Publish a release ○ Create a Translation PR ○ etc...

  • Inside the host project
  • Most of the times specific to the host

project

  • Uses integrations to accomplish

complex tasks

  • Documented using clikt
slide-30
SLIDE 30

Kinta - Custom workflows

30

  • Custom workflows are built using gradle and loaded at runtime using

a ServiceLocator

  • Kinta also comes with default built in workflows:

○ Publishing to the play store ○ Opening a pull request ○ etc... Kinta cli kinta-integrations.jar custom-workflows.jar builtin-workflows.jar

slide-31
SLIDE 31

31

  • Figuring out a way to distribute the kinta binary
  • Also distribute the backend/webapp that hosts artifacts
  • https://github.com/dailymotion/kinta
  • Feedbacks welcome
  • Disclaimer: it’s still very early stage and things may break

What’s next

slide-32
SLIDE 32

Thanks.

32