Github Actions #10 Composite Custom actions
GitHub Actions’ın temel eylemleri bazen yeterli olmayabilir ve bu durumda özelleştirilmiş bir eyleme ihtiyaç duyabilirsiniz.
Custom composite actions, birden fazla adımdan oluşan daha karmaşık eylemlerdir ve özel işlemler veya senaryolar için daha fazla esneklik sağlar. Bu, birden fazla adımdan oluşan daha karmaşık eylemleri, örneğin veri tabanı yedekleme ve geri yükleme veya farklı ortamlar arasında uygulama dağıtımı gibi işlemleri kolaylaştırır.
Bunun yanı sıra, custom composite actions kullanarak, bir adımı birden çok yerde tekrarlamak yerine, aynı adımı birden çok iş akışında yeniden kullanabilirsiniz. Bu, tekrarlanan kodu azaltabilir ve GitHub Actions iş akışlarının daha kolay yönetilmesini sağlayabilir.
Sonuç olarak, custom composite actions kullanarak, iş akışlarınızı daha esnek, ölçeklenebilir ve yeniden kullanılabilir hale getirebilirsiniz.
name: Deployment
on:
push:
branches:
- master
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Get code
uses: actions/checkout@v3
- name: Load & Cache dependencies ( Local Custom Actions )
id: cache-deps
uses: ./.github/actions/cached-deps
with:
caching: 'false'
- name: Output information
run: echo "cache used? ${{ steps.cache-deps.outputs.used-cache }}"
- name: Lint code
run: npm run lint
test:
runs-on: ubuntu-latest
steps:
- name: Get code
uses: actions/checkout@v3
- name: Load & Cache dependencies ( Local Custom Actions )
id: cache-deps-test
uses: ./.github/actions/cached-deps
- name: Output information
run: echo "cache used? ${{ steps.cache-deps-test.outputs.used-cache2 }}"
- 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: Load & Cache dependencies ( Local Custom Actions )
uses: ./.github/actions/cached-deps
- name: 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 code
uses: actions/checkout@v3
- name: Get build artifacts
uses: actions/download-artifact@v3
with:
name: dist-files
path: ./dist
- name: Output contents
run: ls
- name: Deploy site
run: echo "Deploying..."
Yukarıda actions da bulunan, Load & Cache dependencies isimli adım, ./.github/actions/cached-deps dizini altındaki custom actions’ı çalıştırır. ve actions tanım dosyamıza her seferinde tekrar tekrar bu tanımları yapmak zorunda kalmayız.
- name: Load & Cache dependencies ( Local Custom Actions )
id: cache-deps
uses: ./.github/actions/cached-deps
Custom actions dosyamızın içeriği aşağıdaki gibidir;
name: Get & Cache Dependencies
description: 'Get the dependencies (via npm) and cache them.'
inputs:
caching:
description: Whether to cache dependencies or not.
required: false
default: 'true'
outputs:
used-cache:
description: 'Whether the cache was used'
value: ${{ steps.install.outputs.cache }}
used-cache2:
description: 'Whether the cache was used'
value: ${{ steps.out.outputs.cache }}
runs:
using: composite
steps:
- name: Cache dependencies
if: inputs.caching == 'true'
id: cache
uses: actions/cache@v3
with:
path: node_modules
key: deps-node-modules-${{ hashFiles('**/package-lock.json') }}
- name: Set output
id: out
if: inputs.caching == 'true'
run: echo "cache='${{ inputs.caching }}'" >> $GITHUB_OUTPUT
shell: bash
- name: Install dependencies
id: install
if: steps.cache.outputs.cache-hit != 'true' || inputs.caching != 'true'
run: |
npm ci
echo "cache='${{ inputs.caching }}'" >> $GITHUB_OUTPUT
shell: bash
Yukarıdaki bulunan tüm işlemleri tek bir workflow tanımı içerisine yazmak yerine, bu tanımları workflow dosyasında “git ve x pathi altındaki yaml dosyasından oku” diyebiliyoruz.
Bu örnekte özelleştirilmiş action , proje dosyalarının ana dizininde bir .github/actions/cached-deps
klasöründe bulunur. Bu custom action u kullanmasını istediğimiz tüm joblara bu dosyayı aşağıdaki şekilde bildirebiliriz;
- name: Load & Cache dependencies ( Local Custom Actions )
id: cache-deps
uses: ./.github/actions/cached-deps
Dilersek, custom actions tarafında kullanılması için bir değer gönderebiliriz;
- name: Load & Cache dependencies ( Local Custom Actions )
id: cache-deps
uses: ./.github/actions/cached-deps
with:
caching: 'false'
Misal, yukarıdaki örnekte ilgili job’un cache işlemlerinden muaf olması için caching parametresine “false” değerini gönderdik.


Özetle, Composite actions, birden fazla adımdan oluşan daha karmaşık actionslardır ve özel işlemler veya senaryolar için daha fazla esneklik sağlar. İşte, composite actions kullanmanın bazı avantajları:
- Daha az tekrar eden kod: Composite actions, birçok iş akışında aynı adımı tekrar etmenin gerektirdiği kod tekrarını azaltır. Bu, daha az yazılım geliştirme zamanı gerektirir ve iş akışlarını daha kolay yönetilebilir hale getirir.
- Daha esnek ve özelleştirilebilir: Composite actions, farklı senaryolar ve gereksinimler için özelleştirilebilir ve esnek bir yapıya sahiptir. Birleştirilmiş actionslar, birden fazla adımı tek bir actions olarak birleştirebilir ve böylece daha karmaşık iş akışlarına uygun hale getirebilir.
- Daha kolay bakım ve yönetim: Composite actions, kod tekrarını azalttığından ve birden çok iş akışında yeniden kullanıldığından, daha kolay bakım ve yönetim sağlar. Bir actions da yapılan değişiklikler, tüm iş akışlarında kullanılan ilgili eylemlerde etkili olacaktır.
- Paylaşım için uygun: Composite actions, herkesin kullanabileceği ve paylaşabileceği yapıda olabilir. Bu, topluluk desteğini artırır ve kodun tekrar kullanılabilirliğini ve kalitesini artırır.
Sonuç olarak, composite actions kullanarak, iş akışlarınızı daha esnek, ölçeklenebilir ve yeniden kullanılabilir hale getirebilirsiniz.