Sunday, November 30, 2025

What are the top level Github actions yaml file constructs ?

Yes, there are several other top-level elements you can use in GitHub Actions workflow files. Here are all the available top-level elements:


## Complete List of Top-Level Elements:


### 1. **`name`** (you have this)

```yaml

name: Workflow Name

```


### 2. **`on`** (you have this)

```yaml

on: [push, pull_request]

```


### 3. **`jobs`** (you have this)

```yaml

jobs:

  my-job:

    runs-on: ubuntu-latest

```


### 4. **`run-name`** (optional)

```yaml

run-name: Deploy to ${{ inputs.deploy_target }} by @${{ github.actor }}

```


### 5. **`env`** - Global environment variables

```yaml

env:

  NODE_ENV: production

  DATABASE_URL: ${{ secrets.DATABASE_URL }}

```


### 6. **`defaults`** - Default settings for all jobs

```yaml

defaults:

  run:

    shell: bash

    working-directory: scripts

```


### 7. **`concurrency`** - Control concurrent workflow runs

```yaml

concurrency:

  group: production-${{ github.ref }}

  cancel-in-progress: true

```


### 8. **`permissions`** - Fine-grained permissions

```yaml

permissions:

  actions: read

  checks: write

  contents: read

  deployments: write

```


### 9. **`on.schedule`** - For scheduled workflows (part of `on`)

```yaml

on:

  schedule:

    - cron: '0 2 * * *'  # Daily at 2 AM

```


### 10. **`on.workflow_dispatch`** - Manual triggers

```yaml

on:

  workflow_dispatch:

    inputs:

      environment:

        description: 'Environment to deploy'

        required: true

        default: 'staging'

```


### 11. **`on.pull_request`** - PR-specific triggers

```yaml

on:

  pull_request:

    types: [opened, synchronize, reopened]

    branches: [main]

```


## Complete Example with All Elements:


```yaml

name: Comprehensive Workflow


on:

  push:

    branches: [main]

  pull_request:

    branches: [main]

  workflow_dispatch:

    inputs:

      logLevel:

        description: 'Log level'

        required: true

        default: 'warning'


env:

  NODE_VERSION: '16'

  PYTHON_VERSION: '3.9'


defaults:

  run:

    shell: bash

    working-directory: ./src


concurrency:

  group: ${{ github.workflow }}-${{ github.ref }}

  cancel-in-progress: true


permissions:

  contents: read

  packages: write


jobs:

  test:

    runs-on: ubuntu-latest

    steps:

      - uses: actions/checkout@v3

      - name: Run tests

        run: pytest

  

  build:

    runs-on: ubuntu-latest

    needs: test

    steps:

      - uses: actions/checkout@v3

      - name: Build

        run: npm run build

```


So in summary, while your workflow uses the three essential elements (`name`, `on`, `jobs`), you have several other powerful options available depending on your needs!


No comments:

Post a Comment