18-June-25

Performance optimization in ci/cd pipeline focus on faster build & test for example using feature like caching, and pararel jobs

18-June-25
Photo by nbtrisna The intricate embrace of myth, forever etched against the quiet blue above.

Daily Quest #15: Performance Optimization

Performance optimization in ci/cd pipeline focus on faster build & test for example using feature like caching, and pararel jobs

Reference :

  • https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows
  • https://github.com/docker/build-push-action
Real world usecase : Instead to runing terraform init to re-download all provider, we can using caching in .terraform/plugins. An then init only check if update available.

Skenario : Using caching for terraform providers, node modules, docker layers and build pararel jobs in github actions for faster pipeline

  1. Create new workflow .github/workflows/performance-opt.yml
name: Performance optimization
on:
  push:
    branches:
    - main
jobs:
  infra-cache:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Cache terraform providers
      uses: actions/cache@v4
      with:
        path: .terrafrom/plugins
        key: ${{ runner.os }}-terraform-${{ hashFiles('**/*.tf') }}
        restore-keys: |
          ${{ runner.os }}-terraform-
    
    - name: Setup terraform
      uses: hashicorp/setup-terraform@v3
      with:
        terraform_version: 1.7.2

    - name: Terraform init & Apply
      run: |
        terraform init 
        terraform plan -out=tfplan.out

  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3
    
    - name: setup buildx
      uses: docker/setup-buildx-action@v3

    - name: Cache node modules
      uses: actions/cache@v4
      with:
        path: ~/.npm
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-node-
    - name: Build docker image with layer cache
      uses: docker/build-push-action@v4
      with:
        context: .
        file: Dockerfile
        push: false
        cache-from: type=gha
        cache-to: type=gha,mode=max
        tags: ci-cd-demo:latest
  1. Result running
Pasted image 20250619023125
Pasted image 20250619023802

Question :

  • Bagaimana caching folder .terraform/plugins mempercepat terraform init?
  • Jelaskan cara kerja opsi cache-from dan cache-to di docker/build-push-action.

Answer :

  1. Dalam terraform init diperlukan caching agar tidak perlu mendownload depedency kembali ketika menjalankan terraform init. Ini akan mempersingkat waktu berjalanya workflow serta menghemat bandwith unduhan
  2. cache-from merupakan spesifik tempat dimana cache sebelumnya ditaruh. Dan cache-to adalah tempat dimana export build terbaru cache layer dimana mengizinkan untuk build dan menggunakan kembali cache tersebut.