Skip to content

Commit 2437f68

Browse files
authored
Implement conditional workflow execution (#981)
Added a conditional job to check if the workflow should run based on changes in the 'Sources/' directory.
1 parent 99bf479 commit 2437f68

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

.github/workflows/BuildAndTest.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,30 @@ permissions:
99
contents: read
1010

1111
jobs:
12+
should-run:
13+
runs-on: ubuntu-latest
14+
outputs:
15+
should-run: ${{ steps.check.outputs.should-run }}
16+
steps:
17+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
18+
with:
19+
fetch-depth: 0
20+
- name: Check if the workflow should run
21+
id: check
22+
run: |
23+
git diff --name-only origin/${{ github.base_ref }} HEAD | grep -q "Sources/" && echo "should-run=true" || echo "should-run=false"
1224
FormattingLint:
25+
needs: should-run
26+
if: ${{ needs.should-run.outputs.should-run }}
1327
runs-on: macos-15
1428
steps:
1529
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
1630
- name: SwiftFormat
1731
run: echo swiftformat --lint `git diff --name-only HEAD^1 HEAD` --reporter github-actions-log
1832

1933
SwiftLint:
34+
needs: should-run
35+
if: ${{ needs.should-run.outputs.should-run }}
2036
runs-on: ubuntu-latest
2137
steps:
2238
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
@@ -26,6 +42,8 @@ jobs:
2642
args: --strict
2743
DIFF_BASE: ${{ github.base_ref }}
2844
macOS:
45+
needs: should-run
46+
if: ${{ needs.should-run.outputs.should-run }}
2947
runs-on: macos-15
3048
steps:
3149
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
@@ -41,6 +59,8 @@ jobs:
4159
xcrun llvm-cov export -ignore-filename-regex="pb\.swift|grpc\.swift" -format="lcov" .build/debug/opentelemetry-swiftPackageTests.xctest/Contents/MacOS/opentelemetry-swiftPackageTests -instr-profile .build/debug/codecov/default.profdata > .build/debug/codecov/coverage_report.lcov
4260
./codecov -f .build/debug/codecov/coverage_report.lcov
4361
iOS:
62+
needs: should-run
63+
if: ${{ needs.should-run.outputs.should-run }}
4464
runs-on: macos-15
4565
steps:
4666
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
@@ -54,6 +74,8 @@ jobs:
5474
- name: Test for iOS
5575
run: make test-without-building-ios
5676
tvOS:
77+
needs: should-run
78+
if: ${{ needs.should-run.outputs.should-run }}
5779
runs-on: macos-15
5880
steps:
5981
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
@@ -67,6 +89,8 @@ jobs:
6789
- name: Test for tvOS
6890
run: make test-without-building-tvos
6991
watchOS:
92+
needs: should-run
93+
if: ${{ needs.should-run.outputs.should-run }}
7094
runs-on: macos-15
7195
steps:
7296
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
@@ -80,6 +104,8 @@ jobs:
80104
- name: Test for watchOS
81105
run: make test-without-building-watchos
82106
visionOS:
107+
needs: should-run
108+
if: ${{ needs.should-run.outputs.should-run }}
83109
runs-on: macos-15
84110
steps:
85111
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
@@ -93,6 +119,8 @@ jobs:
93119
- name: Test for visionOS
94120
run: make test-without-building-visionos
95121
linux:
122+
needs: should-run
123+
if: ${{ needs.should-run.outputs.should-run }}
96124
runs-on: ubuntu-latest
97125
container: swift:6.2@sha256:0e4716bd34384d22963a63afbdbc93be3129dfd0753185aa1ded27755abdcae8
98126
steps:
@@ -101,3 +129,31 @@ jobs:
101129
run: swift build --build-tests
102130
- name: Run tests for Linux
103131
run: swift test
132+
required-status-checks:
133+
needs:
134+
- FormattingLint
135+
- SwiftLint
136+
- macOS
137+
- iOS
138+
- tvOS
139+
- watchOS
140+
- visionOS
141+
- linux
142+
runs-on: ubuntu-latest
143+
if: always()
144+
steps:
145+
- name: Check if all required jobs passed
146+
run: |
147+
if [[ ${{ needs.SwiftLint.result }} == 'failure' || \
148+
${{ needs.FormattingLint.result }} == 'failure' || \
149+
${{ needs.macOS.result }} == 'failure' || \
150+
${{ needs.iOS.result }} == 'failure' || \
151+
${{ needs.tvOS.result }} == 'failure' || \
152+
${{ needs.watchOS.result }} == 'failure' || \
153+
${{ needs.visionOS.result }} == 'failure' || \
154+
${{ needs.linux.result }} == 'failure' ]]; then
155+
echo "One or more required jobs failed. Failing the workflow."
156+
exit 1
157+
else
158+
echo "All required jobs passed/skipped."
159+
fi

0 commit comments

Comments
 (0)