Skip to content
CodeSook
CodeSook

16. Workflow Condiational


GitHub Actions Workflow Conditional

Conditional คือการกำหนดเงื่อนไขให้ job หรือ step ทำงานเมื่อเงื่อนไขเป็นจริงเท่านั้น ใช้คีย์เวิร์ด if:

ตัวอย่างการใช้งานกับ React CSR

ถ้าเราอยากรู้ว่า branch ที่กำลังทำงานใช่ main หรือเปล่าก็จะเช็คแบบนี้
github.ref == 'refs/heads/main'

ถ้าอยากรู้ว่าใช่ develop หรือเปล่าก็จะเช็คแบบนี้ github.ref == 'refs/heads/develop'

name: Deploy React App
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Install dependencies
run: bun install
- name: Run tests
run: bun test
- name: Build for production
# ทำงานเมื่อ push ไป main branch เท่านั้น
if: github.ref == 'refs/heads/main'
run: bun run build
- name: Build for staging
# ทำงานเมื่อ push ไป develop branch เท่านั้น
if: github.ref == 'refs/heads/develop'
run: bun run build:staging
deploy:
needs: build
runs-on: ubuntu-latest
# Job นี้ทำงานเมื่อไม่ใช่ Pull Request
if: github.event_name != 'pull_request'
steps:
- name: Deploy to production
if: github.ref == 'refs/heads/main'
run: echo "Deploying to production..."
- name: Deploy to staging
if: github.ref == 'refs/heads/develop'
run: echo "Deploying to staging..."

ลองทำดูบ้าง

โปรเจคเดิมของเราเลย

แก้ deploy workflow นะ
เราจะให้ deploy job ทำงาน ถ้า branch เป็น main เท่านั้น

workflows/deploy.yaml
deploy.yaml
name: Deploy Project
5 collapsed lines
on:
push:
workflow_dispatch:
pull_request:
jobs:
build:
51 collapsed lines
runs-on: ubuntu-latest
# เพิ่ม outputs
outputs:
build-time: ${{ steps.build-info.outputs.timestamp }}
build-hash: ${{ steps.build-info.outputs.hash }}
build-status: ${{ steps.build-info.outputs.status }}
package-version: ${{ steps.version.outputs.version }}
steps:
- name: Get Code
uses: actions/checkout@v4
- name: Get Package Version
id: version
run: |
VERSION=$(cat package.json | jq -r '.version')
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Install Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.2.18
- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
~/.bun/install/cache
key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }}
- name: Install Dependencies
run: bun i
- name: Build
run: bun run build
- name: Get Build Info
id: build-info
run: |
echo "timestamp=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
echo "hash=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "status=success" >> $GITHUB_OUTPUT
- name: upload dist folder
uses: actions/upload-artifact@v4
with:
name: dist
path: |
dist
public
deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
19 collapsed lines
- name: Show Build Info
run: |
echo "🚀 Deploying version: ${{ needs.build.outputs.package-version }}"
echo "📦 Build hash: ${{ needs.build.outputs.build-hash }}"
echo "⏰ Built at: ${{ needs.build.outputs.build-time }}"
echo "✅ Build status: ${{ needs.build.outputs.build-status }}"
- name: download dist folder
uses: actions/download-artifact@v4
with:
name: dist
- name: List all files
run: ls -R
- name: Deploy
run: |
echo "Deploying version ${{ needs.build.outputs.package-version }}..."
echo "Commit: ${{ needs.build.outputs.build-hash }}"

แล้วก็ push code แต่รอบนี้เราจะ push ไปที่ branch develop แทนนะ

Terminal window
git add .
git commit -m "deploy on push to main only"
git switch -c develop
git push -u origin develop

แล้วตามไปดูที่เว็ป

Operators

มีให้ใช้เหมือนๆกับภาษาทั่วๆไปแหละ