GitHub Actions Playground
The following example demonstrates the concept of "reusable workflows" in GitHub Actions.
Directory Structure
.github/actions
— composite actions.github/workflows
— GitHub Actions workflowsdeploy.yml
— parent workflowdeploy-postgres.yml
— child workflowdeploy-redis.yml
— child workflowdeploy-vpn.yml
— child workflow
.github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
postgres:
name: "PostgreSQL"
uses: ./.github/workflows/deploy-postgres.yml
redis:
name: "Redis"
uses: ./.github/workflows/deploy-redis.yml
needs: [postgres]
vpn:
name: "OpenVPN"
uses: ./.github/workflows/deploy-vpn.yml
needs: [postgres]
- Declares the list of jobs that reference child workflows
- Declares the execution order of the jobs using dependencies (
needs
) - Uses short job IDs plus descriptive names
- Fires up on push to the
main
branch
.github/workflows/deploy-redis.yml
name: Deploy Redis
on: workflow_call
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: 'echo "Step 1"'
- run: 'echo "Step 2"'
- run: 'echo "Step 3"'
- Declares a reusable workflow that can be called from other workflows
Using External Workflows
- Create
actions
repository under your GitHub account (e.g.koistya/actions
) - Create reusable workflows in the
.github/workflows
folder (e.g..github/workflows/deploy.yml
) - Create composite actions in the root folder (e.g.
create-pod/action.yml
) - Allow access to actions/workflows in the
actions
repository from external repos
Reference external workflows and composite actions as shown in this example:
name: Deploy
on:
push:
branches: [main]
jobs:
postgres:
name: "PostgreSQL"
uses: example/actions/.github/workflows/deploy-postgres.yml@main
other:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: example/actions/create-pod@main
Where example/actions
is the repository containing external workflows such as koistya/actions
and @main
is the branch name or a tag.
References