CI/CD for Kotlin and CloudFunctions
Tags: actionshackathon21
Published: 2021-11-29 08:30:34

### My Workflow

This is a workflow that covers **Kotlin Building**, **Test Coverage Badge Generation** and **Cloud Functions Deployment**.

At the begging of the year I wanted to create a project to try out serverless computation and cloud-native, using Kotlin with the Cloud Functions form GCP seems like a good start point to me, so I created a small crawler to crawl some apartment buying registration information and send out an email notification. (In my city the building companies publish buying registration information once they decide to start selling, and you need to register first if you want to buy one).

So my next problem was to create a CI/CD pipeline, which I decided to try out Github Actions, and I was happy to found out there are already some great actions that could help me to generate a fancy test coverage badge and deploy my code to Cloud Functions easily. By leveraging `cicirello/jacoco-badge-generator@v2` and `google-github-actions/deploy-cloud-functions@v0.1.2`, my pipeline was built quickly.

And the final result looks like this:
![](https://github.com/hyrepo/apartment-registration-alert/raw/master/doc/architecture.png)

### Submission Category: 

Wacky Wildcards


### Yaml File or Link to Code

```yaml
name: CI/CD

on:
  push:
    branches: [ master ]
    paths:
      - src/**

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up JDK 8
        uses: actions/setup-java@v2
        with:
          java-version: '8'
          distribution: 'adopt'

      - name: Build with Gradle
        run: ./gradlew build

      - name: Generate JaCoCo Badge
        uses: cicirello/jacoco-badge-generator@v2
        with:
          jacoco-csv-file: build/reports/jacoco/test/jacocoTestReport.csv
          generate-branches-badge: true

      - name: Log coverage percentage
        run: |
          echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
          echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

      - name: Commit and push the badge (if it changed)
        uses: EndBug/add-and-commit@v7
        with:
          default_author: github_actions
          message: 'doc: update badge'
          add: '*.svg'

      - name: Upload JaCoCo coverage report
        uses: actions/upload-artifact@v2
        with:
          name: jacoco-report
          path: build/reports/jacoco/test/html

  deploy:
    needs: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Cloud Functions Deploy
        uses: google-github-actions/deploy-cloud-functions@v0.1.2
        with:
          credentials: ${{ secrets.GCP_SA_KEY }}
          name: apartment-registration-alert
          region: asia-northeast1
          env_vars: AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }},AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}
          entry_point: Application
          runtime: java11
          service_account_email: ${{ secrets.SERVICE_ACCOUNT_EMAIL }}
          max_instances: 1
          event_trigger_type: google.pubsub.topic.publish
          event_trigger_resource: projects/apartment-registration-alert/topics/schedule-trigger

```


{% github https://github.com/hyrepo/apartment-registration-notification %}