Github Actions #7 if condition

Onur BOLATOĞLU
3 min readJan 3, 2023

Bazı durumlarda hata veren görev ve adımlar hakkında bilgi sahibi olmamız gerekebilir. Bu nedenle “if” ibaresini görevlerimizde kullanabiliriz.

  test:
runs-on: ubuntu-latest
steps:
- name: Get code
uses: actions/checkout@v3
- name: Cache dependencies
id: cache
uses: actions/cache@v3
with:
path: ~/.npm
key: deps-node-modules-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm ci
- name: Test code
id: run-tests
run: npm run test
- name: Upload test report
if: ${{ failure() && steps.run-tests.outcome == 'failure' }}
uses: actions/upload-artifact@v3
with:
name: test-report
path: test.json

Yukarıda tests adında bir job mevcut, burada “test code” adımı hata veriyor. ( Kodumuzda bilinçli bir değişiklik yaptık, bu sayede) Normal şartlarda, hata aldıktan sonra tüm adımlar durdurulur ve workflow sona ermesi gerekmektedir.

Fakat biz “ if: failure() && steps.run-tests.outcome == ‘failure’ “ koşulunu ekleyerek, sorunun nedenini test.json dosyasına yazdırılmasını sağlayıp, daha sonra bu problemin kaynağını inceleyebiliriz.

Yukarıdaki job da, “Test code” adımı hata verecek ve Upload test report adımı “Test code” adımının hata verdiği durumlarda çalışacaktır. “Upload test report” adımı adından da anlaşılacağı gibi, test.json dosyasını görüntüleyebilmemiz için, Actions içerisinde depolamamızı sağlar.

lint ve test jobları paralel çalışır.
test jobu hata verdiği için, test jobunun durumuna göre çalışacak olan “build” ve “deploy” jobları çalışmadı. Ek olarak “Test code” adımından sonra çalışmasını istediğimiz “Upload test report” adımı çalıştı.

Özetle,

Bu işlem, ilk adımda “Test code” olarak adlandırılan bir adımı çalıştırır ve daha sonra eğer “Test code” adımı hata verirse, “Upload test report” adımı çalıştırır.

“Test code” adımı, “npm run test” komutunu çalıştırır ve bu komut, package.json dosyasında tanımlanmış “test” script’ini çalıştırır. Bu script uygulamanın testlerini çalıştırır ve testlerin sonuçlarını bir json dosyası olarak raporlar.

“Upload test report” adımı ise, “test.json” dosyasını “actions/upload-artifact@v3” adlı bir GitHub Action ile yükler. Bu Action, belirtilen dosyayı GitHub üzerinde bir “artifact” olarak depolar ve daha sonra bu artifact’i indirme veya görüntüleme imkanı verir. Bu, test sonuçlarının daha sonra incelenmesine yardımcı olur.

Önemli;

Bir hatadan sonra çalışacak bir adım için ek koşullar ekleyebilirsiniz, ancak yine de, bir durum denetimi işlevi içermeyen if koşullarına otomatik olarak uygulanan başarının () varsayılan durum denetimini geçersiz kılmak için Failure()’u dahil etmeniz gerekir.

Yukarıdaki actions’un tamamı;

name: Website Deployment
on:
push:
branches:
- master
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Get code
uses: actions/checkout@v3
- name: Cache dependencies
id: cache
uses: actions/cache@v3
with:
path: ~/.npm
key: deps-node-modules-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm ci
- name: Lint code
run: npm run lint
test:
runs-on: ubuntu-latest
steps:
- name: Get code
uses: actions/checkout@v3
- name: Cache dependencies
id: cache
uses: actions/cache@v3
with:
path: ~/.npm
key: deps-node-modules-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm ci
- name: Test code
id: run-tests
run: npm run test
- name: Upload test report
if: ${{ failure() && steps.run-tests.outcome == 'failure' }}
uses: actions/upload-artifact@v3
with:
name: test-report
path: test.json
build:
needs: test
runs-on: ubuntu-latest
steps:
- name: Get code
uses: actions/checkout@v3
- name: Cache dependencies
id: cache
uses: actions/cache@v3
with:
path: ~/.npm
key: deps-node-modules-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm ci
- name: Build website
id: build-website
run: npm run build
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: dist-files
path: dist
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Get build artifacts
uses: actions/download-artifact@v3
with:
name: dist-files
- name: Output contents
run: ls
- name: Deploy
run: echo "Deploying..."

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response