DEV Community

JL
JL

Posted on

Github Workflow

Events

Image description
Common filters:

on:
  push:
    paths-ignore:
      - 'docs/**'
Enter fullscreen mode Exit fullscreen mode

Ref:
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet

Alternatively, developers can also add strings in the commit comments to skip manually:
[skip ci]
https://docs.github.com/en/actions/managing-workflow-runs/skipping-workflow-runs

Steps
can be shell scripts, or actions (using "with" to configure).

Jobs: In Parallel vs Sequential
Jobs can be sequential be specifying "needs: [job1, job2]" keywords.

Expressions & Context Objects
e.g.

run: echo "${{ toJSON(github) }}"
Enter fullscreen mode Exit fullscreen mode

Example NodeJS project workflow

name: Deploy Project
on: [push, workflow_dispatch]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Get code
        uses: actions/checkout@v3
      - name: Install NodeJS
        uses: actions/setup-node@v3
        with:
          node-version: 18
      - name: Install dependencies
        run: npm ci
      - name: Run tests
        run: npm test
  deploy:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Get code
        uses: actions/checkout@v3
      - name: Install NodeJS
        uses: actions/setup-node@v3
        with:
          node-version: 18
      - name: Install dependencies
        run: npm ci
      - name: Build project
        run: npm run build
      - name: Deploy
        run: echo "Deploying ..."

Enter fullscreen mode Exit fullscreen mode

Useful actions:

Uploading Job Artifacts
https://github.com/actions/upload-artifact

Downloading Artifacts
https://github.com/actions/download-artifact


**Job Outputs**
find dist/assets/*.js -type f -execdir echo 'my-variable={}' >> $GITHUB_OUTPUT ';'
later on, this variable can be accessed via:

outputs:
    script-file: {{ steps.publis.outputs.my-variable }}
Enter fullscreen mode Exit fullscreen mode

Sharing outputs across multiple jobs
needs
https://docs.github.com/en/actions/learn-github-actions/contexts#needs-context

Caching Dependencies for speeding up jobs
https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows
e.g. use cache action in both build and test, with same hash value in the cache name (to make sure they are using the same cache in the central place):

Image description

Top comments (0)