Kondisional pada GitHub Workflow

Kondisional-kondisional yang ada di Github Workflow. Kondisional ini berguna untuk memicu job step dengan kasus-kasus tertentu. source Misalnya:

Menjalankan command apabila repository di push dengan commit yang memiliki substring tertentu (match substring from github commit messages)

jobs:
  build:
    name: Nama Workflow
    runs-on: ubuntu-latest
    steps:
      - run: echo "git commit contains hello" # run this command if commit contains hello
        if: contains(github.event.head_commit.message, 'hello')
      - run: echo "git commit any"

selain contains untuk mencari sebuah substring pada string. Adapun fungsi’’ lain seperti:

  • startsWith untuk memeriksa apakah string memiliki awalan tertentu (penggunaannya sama seperti contoh kode diatas)
  • endsWith untuk memeriksa apakah string memiliki akhiran tertentu (penggunaannya sama seperti contoh kode diatas)

Melanjutkan steps meskipun command gagal (continue on error)

jobs:
  build:
    name: Nama Workflow
    runs-on: ubuntu-latest
    steps:
      - run: this_command_is_not_found xxxxx # ini akan membuat workflow berhenti
        continue-on-error: true # namun dengan ini tidak akan membuat workflow berhenti
        id: custom-id # membuat id khusus (opsional)
      - run: echo "git commit any"

Mengitung jumlah file yang termodifikasi

Menghitung berapa jumlah file yang termodifikasi (uncommited files) pada github. Lalu bagaimana cara mengintegrasikannya kedalam github CI? berikut ulasannya:

perintah dasar menggunakan git diff sebagai berikut:

git diff --cached --numstat | wc -l

Github CI - Mengitung jumlah file yang termodifikasi langsung di dalam run

Contoh konfigurasi github CI langsung di dalam run

jobs:
  build:
    name: Mengitung jumlah file yang termodifikasi ke dalam output steps
    runs-on: ubuntu-latest
    steps:
      - shell: bash
        run: |
          cached=$(git diff --cached --numstat | wc -l)
          echo "jumlah file yang termodifikasi adalah ${cached}"

Github CI - Mengitung jumlah file yang termodifikasi ke dalam output steps

Contoh konfigurasi github CI ke dalam output steps

jobs:
  build:
    name: Mengitung jumlah file yang termodifikasi ke dalam output steps
    runs-on: ubuntu-latest
    steps:
      - shell: bash
        run: |
          echo "cached=$(git diff --cached --numstat | wc -l)" >> $GITHUB_OUTPUT
        name: what changes
        id: changes
      - shell: bash
        run: |
          echo "jumlah file yang termodifikasi adalah ${{ steps.changes.outputs.cached }}"

Menghitung jumlah commits yang belum dipush

Untuk mengihitung jumlah commit yang belum dipush, dapat menggunakan perintah dasar git diff juga. Contohnya sebagai berikut:

git diff origin/master..HEAD --numstat | wc -l

Untuk penggunaan dalam Github Actions (CI) contohnya sebagai berikut:

git diff origin/${GITHUB_REF#refs/heads/}..HEAD --numstat | wc -l

GITHUB_REF#refs/heads/ berguna untuk mendapatkan branch yang saat ini dijalankan.

Snippet lainnya sebagai berikut

Get current SHA short hash

bash

echo $GITHUB_SHA | cut -c 1-6

Github CI Yaml output env

echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV

Github CI Yaml output steps

echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_OUTPUT

Tentang artikel ini

Artikel ini untuk mempermudah visitor untuk memahami github workflow.

Contoh FULL Github CI

https://github.com/dimaslanjaka/static-blog-generator/blob/e8ef351552d57c5e28e016e39e78fef139a8e7b2/.github/workflows/build-beta.yml

yaml
name: Build Beta on: push: branches: ['beta'] paths-ignore: - '**/dist/**' - '**/release/**' - 'packages/google-news-sitemap' - 'packages/hexo-post-parser' - 'packages/git-command-helper' - 'packages/safelinkify' - 'packages/hexo-blogger-xml' - '**/test/**' pull_request: types: - closed workflow_dispatch: # run single job concurrency: group: build cancel-in-progress: true jobs: build: strategy: fail-fast: true env: NODE_OPTIONS: '--max-old-space-size=8192' YARN_ENABLE_IMMUTABLE_INSTALLS: false ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: repository: dimaslanjaka/static-blog-generator ref: beta token: ${{ secrets.ACCESS_TOKEN }} submodules: 'recursive' #- run: sh scripts/install-submodules.sh #name: clone submodules - name: Set EOL git configs continue-on-error: true run: | git config core.eol lf git config core.autocrlf input git checkout-index --force --all - uses: actions/setup-node@v3 with: node-version: 18.x token: ${{ secrets.GITHUB_TOKEN }} architecture: x64 #cache: 'yarn' #cache-dependency-path: '**/yarn.lock' - name: Enable corepack run: | corepack enable corepack prepare yarn@3.2.0 --activate - name: Set env id: set-env shell: bash run: | echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV echo "GITHUB_BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV echo "GITHUB_COMMIT_URL=https://github.com/${{github.repository}}/commit/$(echo $GITHUB_SHA)" >> $GITHUB_ENV echo "GITHUB_RUNNER_URL=https://github.com/${{github.repository}}/commit/${{github.sha}}/checks/${{github.run_id}}" >> $GITHUB_ENV echo "CACHE_NPM=$(npm config get cache)" >> $GITHUB_ENV echo "CACHE_YARN2=$(yarn config get cacheFolder || echo \".yarn\")" >> $GITHUB_ENV echo "cache-npm=$(npm config get cache)" >> $GITHUB_OUTPUT echo "cache-yarn2=$(yarn config get cacheFolder || echo \".yarn\")" >> $GITHUB_OUTPUT #echo "cache-yarn=$(yarn cache dir || echo \".yarn\")" >> $GITHUB_OUTPUT #echo "CACHE_YARN=$(yarn cache dir || echo \".yarn\")" >> $GITHUB_ENV - name: Get env shell: bash run: | echo "branch : ${{ env.GITHUB_BRANCH }}" echo "commit msg : ${{ github.event.head_commit.message }}" echo "commit hash : ${{ env.GITHUB_SHA_SHORT }}" echo "commit url : ${{ env.GITHUB_COMMIT_URL }}" echo "runner url : ${{ env.GITHUB_RUNNER_URL }}" echo "cache npm : ${{ env.CACHE_NPM }}" echo "cache yarn2 : ${{ env.CACHE_YARN2 }}" #echo "cache yarn : ${{ env.CACHE_YARN }}" - uses: actions/cache@v3 # pat-s/always-upload-cache@v3.0.11 id: cache name: get caches with: path: | ${{ steps.set-env.outputs.cache-npm }} ${{ steps.set-env.outputs.cache-yarn2 }} #${{ steps.set-env.outputs.cache-yarn }} #./packages/sbg-server/src/public #**/tmp/** #**/node_modules/** #**/.yarn key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }} restore-keys: | ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }} ${{ runner.os }}- - name: Install Global Packages run: npm i -g typescript ts-node gulp-cli hexo-cli webpack-cli npm-run-all depcheck lerna husky - run: yarn install - run: | arr_variable=("sbg-api" "sbg-server" "sbg-utility" "sbg-cli" "static-blog-generator" "instant-indexing") for i in "${arr_variable[@]}" do yarn workspace $i build && yarn workspace $i run pack done shell: bash name: build - run: npx sbg clean all working-directory: test name: test clean - run: npx sbg post standalone working-directory: test name: test post standalone - run: npx sbg post copy working-directory: test name: test post copy - run: npx hexo generate working-directory: test name: test generate site with hexo - run: npx sbg generate feed working-directory: test name: test generate feed - run: npx sbg generate sitemap working-directory: test name: test generate sitemap - run: npx sbg deploy copy working-directory: test name: test copy generated site to deployment directory - run: | git status echo "cached=$(git diff --cached --numstat | wc -l)" >> $GITHUB_OUTPUT echo "commits=$(git diff origin/master..HEAD --numstat | wc -l)" >> $GITHUB_OUTPUT name: what changes id: changes - name: commit and push if: steps.changes.outputs.cached > 0 || steps.changes.outputs.commits > 0 shell: bash run: | git config --global user.email "dimaslanjaka@gmail.com" git config --global user.name "dimaslanjaka" arr_variable=("sbg-api" "sbg-server" "sbg-utility" "sbg-cli" "static-blog-generator" "instant-indexing") for i in "${arr_variable[@]}" do git add "packages/${i}/dist" &> /dev/null git add "packages/${i}/release" &> /dev/null git commit -m "chore: build ${i} from ${{ env.GITHUB_COMMIT_URL }} runner ${{ env.GITHUB_RUNNER_URL }}" &> /dev/null done # git submodule foreach "git add dist release" # git push --recurse-submodules=on-demand git push &> /dev/null