Skip to content

Instantly share code, notes, and snippets.

@amasucci
Last active March 6, 2023 21:29
Show Gist options
  • Save amasucci/2c04463547b16cf70b4d4790bc44f2b5 to your computer and use it in GitHub Desktop.
Save amasucci/2c04463547b16cf70b4d4790bc44f2b5 to your computer and use it in GitHub Desktop.
GitHub Workflow reuse https://youtu.be/bCqPXUcBfJQ

The full video is here https://youtu.be/bCqPXUcBfJQ

application one/.github/workflows/build.yaml

name: Application One
run-name: SDLC for Application One 🚀
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Triggered by ${{ github.actor }} with ${{ github.event_name }} event, status is ${{ job.status }}."
  sast:
    needs: build
    uses: outofdevops/shared-workflows/.github/workflows/sast.yaml@main
    with:
      run-name: "Static Application Security Testing for Application-One" 
  provision-infra:
    runs-on: ubuntu-latest
    steps:
      - run: echo "⚙️ Provisioning testing infrastructure for Application One."
  dockerise:
    needs: build
    uses: outofdevops/application-one/.github/workflows/dockerise.yaml@main
    with:
      image: "application-one"
  end-2-end:
    runs-on: ubuntu-latest
    needs:
      - dockerise
      - provision-infra
    steps:
      - run: echo "⚙️ Running E2E tests"
  promote:
    needs: 
      - end-2-end
      - sast
    runs-on: ubuntu-latest
    steps:
      - run: |
          curl -L \
            -X POST \
            -H "Authorization: Bearer ${{ github.token }}"\
            -H "Accept: application/vnd.github+json" \
            https://api.github.com/repos/${{ github.repository }}/actions/workflows/promote.yaml/dispatches \
            -d '{"ref":"main","inputs":{"path":"/service-one","image-name":"service-one"}}'
  destroy-infra:
    needs: end-2-end
    runs-on: ubuntu-latest
    steps:
      - run: echo "⚙️ Destroying testing infrastructure for Application One."
  trigger-dependencies:
    needs: promote
    runs-on: ubuntu-latest
    steps:
      - run: |
          curl -L \
            -X POST \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.PAT }}"\
            https://api.github.com/repos/outofdevops/application-two/actions/workflows/build.yaml/dispatches \
            -d '{"ref":"main","inputs":{"image-name":"service-one"}}'

application one/.github/workflows/dockerise.yaml

name: Dockerisation Workflow

on:
  workflow_call:
    inputs:
      image:
        required: true
        type: string

jobs:
  docker-build:
    runs-on: ubuntu-latest
    steps:
    - run: echo "Creating Image for ${{ inputs.image }}"
  docker-tag:
    runs-on: ubuntu-latest
    steps:
    - run: echo "Tagging Image ${{ inputs.image }}"

application one/.github/workflows/promote.yaml

on:
  workflow_dispatch:
    inputs:
      path:
        required: true
        type: string
      image-name:
        required: true
        type: string
name: Promote Service One
run-name: ${{ github.actor }} is promoting ${{ inputs.image-name }}
jobs:
  promoting-service-one-image:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Promoting ${{ inputs.path }} - ${{ inputs.image-name }}"

application two/.github/workflows/build.yaml

on:
  workflow_dispatch:
    inputs:
      image-name:
        required: true
        type: string
name: Build Application Two
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo "${{ inputs.image-name }}"
  deploy:
    needs: build
    uses: outofdevops/shared-workflows/.github/workflows/deploy.yaml@main
    with:
      deployment-name: "service-two"
      version: "${{ github.sha }}"
      target: "eu-west-1"

shared workflows/.github/workflows/sast.yaml

on:
  workflow_call:
    inputs:
      run-name:
        required: false
        description: 'The name shown when executed'
        default: 'Running SAST 🚀'
        type: string
name: SAST
run-name: ${{ inputs.run-name }}
jobs:
  sast:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Scanning..."

shared workflows/.github/workflows/deploy.yaml

on:
  workflow_call:
    inputs:
      deployment-name:
        required: true
        description: 'The name of the deployment'
        type: string
      version:
        required: true
        description: 'The version to be deployed'
        type: string
      target:
        required: true
        description: 'The target environment'
        type: string
name: Deploy
run-name: "Deploying ${{ inputs.deployment-name }}:${{ inputs.version }} in ${{ inputs.target }}"
jobs:
  sast:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying ${{ inputs.deployment-name }}:${{ inputs.version }} in ${{ inputs.target }}..."
      - run: echo "Deployed!!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment