Skip to content

If‑and‑Needs Example #9

If‑and‑Needs Example

If‑and‑Needs Example #9

Workflow file for this run

name: If‑and‑Needs Example
# Trigger on both pushes and PRs
on:
workflow_dispatch:
env:
ACTIONS_RUNNER_DEBUG: true
ACTIONS_STEP_DEBUG: true
jobs:
build:
# ─────────── Server‑side job IF ───────────
# Only queue 'build' when on 'main' branch
if: github.ref == 'refs/heads/master'
runs-on: self-hosted
steps:
- name: sleep
run: sleep 60
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '14'
# ───────── Runner‑side step IF ─────────
# Only run tests when the actor is 'octocat'
- id: run-tests
name: Run tests
run: npm test
if: github.actor == 'octocat'
# Always upload logs, even if tests fail or are skipped
deploy:
# ─────────── Job dependency (needs) ───────────
needs: build
# Server‑side IF: only deploy when 'build' succeeded
if: needs.build.result == 'success'
runs-on: self-hosted
steps:
- name: Checkout code
uses: actions/checkout@v4
# Runner‑side step IF: only on push events (not PRs)
- name: Deploy to Production
run: echo "Deploying to production environment..."
if: github.event_name == 'push'
test:
runs-on: self-hosted
outputs:
raw-json: ${{ steps.set-json.outputs.json_data }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set JSON with embedded command
id: set-json
run: |
# Use single quotes around the whole string so inner quotes and colons are safe
echo 'json_data={"cmd": "echo \"hello\""}' >> $GITHUB_OUTPUT
- name: Inspect raw output
run: |
echo "Raw json_data output:"
echo "${{ steps.set-json.outputs.json_data }}"
- name: toJSON of entire outputs
run: |
echo "toJSON output: ${{ toJSON(steps.set-json.outputs) }}"
- name: fromJSON parse the cmd field
run: |
# fromJSON returns an object, but does NOT execute the cmd value
PARSED_CMD=${{ fromJSON(steps.set-json.outputs.json_data).cmd }}
echo "Parsed cmd field: $PARSED_CMD"
- name: Verify no command execution
run: |
echo "If 'hello' appears below, command was executed (it should NOT):"
echo "Result:"
# Note: we are NOT running $PARSED_CMD here—just echoing it
echo "${{ fromJSON(steps.set-json.outputs.json_data).cmd }}"