Skip to content

Commit 3e3f7d3

Browse files
committed
Add workflows
1 parent c732fa3 commit 3e3f7d3

File tree

5 files changed

+346
-1
lines changed

5 files changed

+346
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Child Workflow 1 - Tests
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
inherit
7+
8+
jobs:
9+
run-tests:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version: '18'
19+
20+
- name: Install dependencies
21+
run: |
22+
echo "Installing dependencies..."
23+
sleep 2
24+
echo "Dependencies installed successfully"
25+
26+
- name: Run unit tests
27+
run: |
28+
echo "Running unit tests..."
29+
sleep 3
30+
echo "All tests passed!"
31+
32+
- name: Generate test report
33+
run: |
34+
echo "Generating test report..."
35+
mkdir -p test-results
36+
echo "Test Summary: 25 tests passed, 0 failed" > test-results/summary.txt
37+
38+
- name: Upload test results
39+
uses: actions/upload-artifact@v4
40+
with:
41+
name: test-results-child1
42+
path: test-results/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Child Workflow 2 - Build & Security
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
inherit
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version: '18'
19+
20+
- name: Build application
21+
run: |
22+
echo "Building application..."
23+
sleep 4
24+
echo "Build completed successfully"
25+
mkdir -p build
26+
echo "Built application v1.0.0" > build/version.txt
27+
28+
- name: Upload build artifacts
29+
uses: actions/upload-artifact@v4
30+
with:
31+
name: build-artifacts-child2
32+
path: build/
33+
34+
security-scan:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- name: Checkout code
38+
uses: actions/checkout@v4
39+
40+
- name: Run security scan
41+
run: |
42+
echo "Running security vulnerability scan..."
43+
sleep 3
44+
echo "Security scan completed - no vulnerabilities found"
45+
46+
- name: Generate security report
47+
run: |
48+
echo "Generating security report..."
49+
mkdir -p security-results
50+
echo "Security Status: PASS - No vulnerabilities detected" > security-results/security-report.txt
51+
52+
- name: Upload security results
53+
uses: actions/upload-artifact@v4
54+
with:
55+
name: security-results-child2
56+
path: security-results/
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: Parent Workflow - Comprehensive CI/CD
2+
3+
on:
4+
push:
5+
branches: [main, develop]
6+
7+
permissions:
8+
contents: read
9+
pages: write
10+
pull-requests: write
11+
id-token: write
12+
13+
jobs:
14+
# Run child workflows in parallel
15+
run-tests:
16+
name: "Execute Test Suite"
17+
uses: ./.github/workflows/child-workflow-1.yml
18+
secrets: inherit
19+
20+
run-build-security:
21+
name: "Execute Build & Security Checks"
22+
uses: ./.github/workflows/child-workflow-2.yml
23+
secrets: inherit
24+
25+
# Parent workflow job that runs after both children complete
26+
collect-and-deploy:
27+
name: "Collect Results & Deploy"
28+
needs: [run-tests, run-build-security]
29+
runs-on: ubuntu-latest
30+
if: always() # Run even if some child workflows fail
31+
steps:
32+
- name: Checkout code
33+
uses: actions/checkout@v4
34+
35+
- name: Check child workflow results
36+
run: |
37+
echo "=== Child Workflow Results ==="
38+
echo "Tests workflow: ${{ needs.run-tests.result }}"
39+
echo "Build & Security workflow: ${{ needs.run-build-security.result }}"
40+
echo "================================"
41+
42+
# Fail if any required child workflow failed
43+
if [[ "${{ needs.run-tests.result }}" != "success" ]] || \
44+
[[ "${{ needs.run-build-security.result }}" != "success" ]]; then
45+
echo "❌ One or more child workflows failed"
46+
exit 1
47+
fi
48+
49+
echo "✅ All child workflows completed successfully!"
50+
51+
- name: Download all artifacts from child workflows
52+
uses: actions/download-artifact@v4
53+
with:
54+
path: combined-artifacts/
55+
56+
- name: Process and combine results
57+
run: |
58+
echo "Processing combined results from child workflows..."
59+
ls -la combined-artifacts/
60+
61+
echo "=== Test Results ==="
62+
if [ -f "combined-artifacts/test-results-child1/summary.txt" ]; then
63+
cat combined-artifacts/test-results-child1/summary.txt
64+
fi
65+
66+
echo "=== Build Artifacts ==="
67+
if [ -f "combined-artifacts/build-artifacts-child2/version.txt" ]; then
68+
cat combined-artifacts/build-artifacts-child2/version.txt
69+
fi
70+
71+
echo "=== Security Results ==="
72+
if [ -f "combined-artifacts/security-results-child2/security-report.txt" ]; then
73+
cat combined-artifacts/security-results-child2/security-report.txt
74+
fi
75+
76+
- name: Create deployment package
77+
run: |
78+
echo "Creating deployment package..."
79+
mkdir -p deployment
80+
echo "Deployment ready - $(date)" > deployment/deployment-info.txt
81+
82+
# Copy relevant artifacts to deployment folder
83+
if [ -d "combined-artifacts/build-artifacts-child2" ]; then
84+
cp -r combined-artifacts/build-artifacts-child2/* deployment/
85+
fi
86+
87+
- name: Upload final deployment artifacts
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: deployment-package
91+
path: deployment/
92+
93+
- name: Deployment simulation
94+
run: |
95+
echo "🚀 Simulating deployment..."
96+
sleep 2
97+
echo "✅ Deployment completed successfully!"
98+
echo "🎉 Parent workflow execution finished!"

README.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,117 @@
1-
# workflow-order
1+
# Workflow Order Example Project
2+
3+
This project demonstrates a GitHub Actions workflow structure with two child workflows and one parent workflow, where the parent workflow executes after both child workflows complete.
4+
5+
## Project Structure
6+
7+
```
8+
.github/workflows/
9+
├── child-workflow-1.yml # Child Workflow 1 - Tests
10+
├── child-workflow-2.yml # Child Workflow 2 - Build & Security
11+
└── parent-workflow.yml # Parent Workflow - Comprehensive CI/CD
12+
```
13+
14+
## Workflow Architecture
15+
16+
### Child Workflows
17+
18+
#### 1. Child Workflow 1 - Tests (`child-workflow-1.yml`)
19+
- **Purpose**: Runs unit tests and generates test reports
20+
- **Jobs**:
21+
- `run-tests`: Sets up Node.js, installs dependencies, runs tests
22+
- **Artifacts**: Uploads test results to `test-results-child1`
23+
- **Triggers**: Can be called by other workflows or triggered on push/PR
24+
25+
#### 2. Child Workflow 2 - Build & Security (`child-workflow-2.yml`)
26+
- **Purpose**: Builds the application and performs security scans
27+
- **Jobs**:
28+
- `build`: Builds the application and creates build artifacts
29+
- `security-scan`: Runs security vulnerability scanning
30+
- **Artifacts**:
31+
- Uploads build artifacts to `build-artifacts-child2`
32+
- Uploads security results to `security-results-child2`
33+
- **Triggers**: Can be called by other workflows or triggered on push/PR
34+
35+
### Parent Workflow
36+
37+
#### Parent Workflow - Comprehensive CI/CD (`parent-workflow.yml`)
38+
- **Purpose**: Orchestrates the entire CI/CD pipeline
39+
- **Execution Order**:
40+
1. **Parallel Execution**: Runs both child workflows simultaneously
41+
- `run-tests` (calls child-workflow-1.yml)
42+
- `run-build-security` (calls child-workflow-2.yml)
43+
2. **Sequential Execution**: After both children complete
44+
- `collect-and-deploy`: Collects results and performs deployment
45+
46+
## Workflow Execution Flow
47+
48+
```mermaid
49+
graph TD
50+
A[Parent Workflow Triggered] --> B[Child Workflow 1: Tests]
51+
A --> C[Child Workflow 2: Build & Security]
52+
B --> D[Parent: Collect Results & Deploy]
53+
C --> D
54+
D --> E[Deployment Complete]
55+
```
56+
57+
## Key Features
58+
59+
### 1. **Parallel Child Execution**
60+
- Both child workflows run simultaneously for efficiency
61+
- Each child workflow is independent and can be reused
62+
63+
### 2. **Dependency Management**
64+
- Parent workflow waits for both children using `needs: [run-tests, run-build-security]`
65+
- Uses `if: always()` to run collection job even if children fail
66+
67+
### 3. **Artifact Aggregation**
68+
- Parent workflow downloads all artifacts from child workflows
69+
- Combines and processes results from multiple sources
70+
- Creates a final deployment package
71+
72+
### 4. **Error Handling**
73+
- Checks the result status of each child workflow
74+
- Fails the parent workflow if any required child fails
75+
- Provides clear status reporting
76+
77+
## Usage
78+
79+
### Triggering the Workflows
80+
81+
The parent workflow (`parent-workflow.yml`) is the main entry point and will automatically trigger both child workflows when:
82+
- Code is pushed to `main` or `develop` branches
83+
- A pull request is opened against `main` or `develop` branches
84+
85+
### Individual Child Workflow Testing
86+
87+
Each child workflow can also be triggered independently for testing:
88+
- Push changes to trigger individual workflows
89+
- Manually trigger workflows from the GitHub Actions tab
90+
91+
## Customization
92+
93+
### Adding More Child Workflows
94+
1. Create a new workflow file in `.github/workflows/`
95+
2. Add `workflow_call` trigger for reusability
96+
3. Update the parent workflow to include the new child in the `needs` array
97+
98+
### Modifying Execution Order
99+
- To run children sequentially: Add `needs` dependencies between child workflows
100+
- To add more parent jobs: Create additional jobs with appropriate `needs` dependencies
101+
102+
## Benefits of This Architecture
103+
104+
1. **Modularity**: Each workflow has a specific purpose and can be maintained independently
105+
2. **Reusability**: Child workflows can be called by multiple parent workflows
106+
3. **Parallel Execution**: Improves overall pipeline performance
107+
4. **Clear Dependencies**: Easy to understand execution order and dependencies
108+
5. **Artifact Management**: Centralized collection and processing of build artifacts
109+
6. **Error Handling**: Robust error checking and reporting
110+
111+
## Example Scenarios
112+
113+
This pattern is useful for:
114+
- **Complex CI/CD Pipelines**: Where different teams manage different parts
115+
- **Multi-Environment Deployments**: Running tests in parallel before deployment
116+
- **Compliance Requirements**: Separate security and quality checks
117+
- **Large Monorepos**: Different workflows for different components

workflow-config.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Example configuration for the workflow project
2+
3+
# Project Settings
4+
project_name: "workflow-order-example"
5+
version: "1.0.0"
6+
7+
# Workflow Configuration
8+
workflows:
9+
child_workflows:
10+
- name: "child-workflow-1"
11+
purpose: "Tests"
12+
jobs: ["run-tests"]
13+
artifacts: ["test-results-child1"]
14+
15+
- name: "child-workflow-2"
16+
purpose: "Build & Security"
17+
jobs: ["build", "security-scan"]
18+
artifacts: ["build-artifacts-child2", "security-results-child2"]
19+
20+
parent_workflow:
21+
name: "parent-workflow"
22+
purpose: "Comprehensive CI/CD"
23+
depends_on: ["child-workflow-1", "child-workflow-2"]
24+
jobs: ["collect-and-deploy"]
25+
26+
# Branch Configuration
27+
trigger_branches:
28+
- main
29+
- develop
30+
31+
# Environment Settings
32+
node_version: "18"
33+
runner: "ubuntu-latest"

0 commit comments

Comments
 (0)