GitHub/Workflow: Difference between revisions

From Omnia
Jump to navigation Jump to search
 
Line 148: Line 148:
       run: echo "MATRIX={\"include\":[{\"runson\":[\"CI\",\"MORE\"],\"task\":\"joe\"},{\"runson\":[\"CI\",\"LESS\"],\"task\":\"bob\"}]}" >> $GITHUB_OUTPUT
       run: echo "MATRIX={\"include\":[{\"runson\":[\"CI\",\"MORE\"],\"task\":\"joe\"},{\"runson\":[\"CI\",\"LESS\"],\"task\":\"bob\"}]}" >> $GITHUB_OUTPUT
   jober:
   jober:
     needs: job1
     needs: generate
     runs-on: ${{ matrix.runson }}
     runs-on: ${{ matrix.runson }}
     strategy:
     strategy:

Revision as of 00:10, 24 December 2023

Manual Run

manual.yaml:

name: Manual Run
run-name: Test
on:
  workflow_dispatch:
    inputs:
      LABEL:
        required: true
        type: string
        description: label

jobs:
  run:
    runs-on: ${{ inputs.LABEL }}
    steps:
      - run: hostname

Basic Workflow

name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v3
      - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
      - run: echo "🍏 This job's status is ${{ job.status }}."

ref: https://docs.github.com/en/actions/quickstart

Using Job Output

jobs:
  job1:
    runs-on: ubuntu-latest
    # Map a step output to a job output
    outputs:
      output1: ${{ steps.step1.outputs.test }}
      output2: ${{ steps.step2.outputs.test }}
    steps:
      - id: step1
        run: echo "test=hello" >> "$GITHUB_OUTPUT"
      - id: step2
        run: echo "test=world" >> "$GITHUB_OUTPUT"
  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
      - env:
          OUTPUT1: ${{needs.job1.outputs.output1}}
          OUTPUT2: ${{needs.job1.outputs.output2}}
        run: echo "$OUTPUT1 $OUTPUT2"

ref: Defining outputs for jobs - GitHub Enterprise Cloud Docs - https://docs.github.com/en/enterprise-cloud@latest/actions/using-jobs/defining-outputs-for-jobs

Dynamic Runs-on

Dynamic from Input:

name: Dynamic Labels
run-name: Dynamic Labels
on:
  workflow_dispatch:
    inputs:
      LABELS:
        required: false
        type: string

jobs:
  run:
    # runs-on: ${{ fromJSON( '["CI"]' ) }}  # This works
    # runs-on: ${{ fromJSON( '["CI", "MORE"]' ) }}  # This works
    runs-on: ${{ fromJSON( inputs.LABELS ) }}  # This works if formatted like: ["CI", "MORE"]
    # runs-on: ${{ inputs.LABELS }}  # this works for single label
    # runs-on: [ CI, MORE ]  # this works
    steps:
      - run: hostname

Dynamic from script:

name: Dynamic Script
run-name: Test
on:
  workflow_dispatch:
    inputs:
      Go:
        required: false
        type: string

jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      RUNS: ${{ steps.set-RUNS.outputs.RUNS }}
    steps:
    - id: set-RUNS
      run: echo "RUNS=[\"CI\",\"MORE\"]" >> $GITHUB_OUTPUT
  job2:
    needs: job1
    runs-on: ${{fromJSON(needs.job1.outputs.RUNS)}}
    steps:
    - run: hostname


ref:

Dynamic Jobs

name: Dynamic Jobs
run-name: Test
on:
  workflow_dispatch:
    inputs:
      Go:
        required: false
        type: string

jobs:
  generate:
    runs-on: ubuntu-latest
    outputs:
      MATRIX: ${{ steps.set-MATRIX.outputs.MATRIX }}
    steps:
    - id: set-MATRIX
      run: echo "MATRIX={\"include\":[{\"runson\":[\"CI\",\"MORE\"],\"task\":\"joe\"},{\"runson\":[\"CI\",\"LESS\"],\"task\":\"bob\"}]}" >> $GITHUB_OUTPUT
  jober:
    needs: generate
    runs-on: ${{ matrix.runson }}
    strategy:
      matrix: ${{fromJson(needs.generate.outputs.matrix)}}
    steps:
    - run: hostname
    - run: echo ${{ matrix.runson }}
    - run: echo ${{ matrix.task }}

keywords