Skip to content

Instantly share code, notes, and snippets.

@mjbear
Last active September 30, 2024 16:22
Show Gist options
  • Save mjbear/839aeb07311593c80f261e972b4d9a05 to your computer and use it in GitHub Desktop.
Save mjbear/839aeb07311593c80f261e972b4d9a05 to your computer and use it in GitHub Desktop.
GitHub Actions for CI/CD with conditional logic
name: Testing CI/CD with GitHub Actions
# Simple test leveraging `echo` to do nothing special other than show job was triggered
#
# Use case: skip 'docker' job since normal contributors cannot access repo secrets (and the job fails)
# REFERENCES:
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows
# https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions
on:
push:
branches: ["main"]
tags:
- "v*"
pull_request:
branches: ["main"]
pull_request_target:
types: ["closed"]
release:
types: ["created"]
# GOAL:
# - run test job on pushes to main branch or v* tag
# - run test job on PRs against main branch
# - run test job on Release creation
#
# - run build job on PR merge
# - run build job on Push and Release
#
# - run delivery job on the above events when the triggered actor is the repo owner
# - run delivery job on Release creation (ex: maybe a maintainer that isn't repo owner)
jobs:
test:
runs-on: ubuntu-latest
# amended the push conditions so this fires on tag push,
# but NOT the push+merge since this will fire on PR close
steps:
- run: echo "test job"
# Use with CAUTION - could divulge secrets
# ~~~ do not run on prod repo ~~~
#- name: Dump GitHub context
# env:
# GITHUB_CONTEXT: ${{ toJson(github) }}
# run: echo "$GITHUB_CONTEXT"
build:
runs-on: ubuntu-latest
needs: test
# if: github.event.pull_request.merged == true
if: >-
${{
github.event.pull_request.merged == true ||
github.event_name == 'push' ||
github.event_name == 'release'
}}
steps:
- run: echo "build job"
# technically this could be combined with build, but this is for testing purposes
delivery:
runs-on: ubuntu-latest
needs:
- test
- build
# if: ${{ github.event_name == 'release' && github.event.action == 'created' }}
if: >-
${{
github.triggering_actor == github.repository_owner ||
github.event_name == 'release' && github.event.action == 'created'
}}
steps:
- run: echo "delivery job"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment