Skip to content

Commit 152fb88

Browse files
committed
Make Rust test workflow reusable
1 parent 0e70568 commit 152fb88

File tree

4 files changed

+306
-114
lines changed

4 files changed

+306
-114
lines changed

.github/workflows/reusable-ci.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Reusable Complete CI Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target-branch:
7+
description: 'Branch to checkout and test (defaults to the calling branch)'
8+
required: false
9+
type: string
10+
default: ''
11+
enable-status-reporting:
12+
description: 'Whether to enable status reporting'
13+
required: false
14+
type: boolean
15+
default: true
16+
status-context:
17+
description: 'Context for status checks'
18+
required: false
19+
type: string
20+
default: 'master/unit'
21+
target-repo:
22+
description: 'Repository to post status to'
23+
required: false
24+
type: string
25+
default: 'datadog-api-spec'
26+
rust-versions:
27+
description: 'JSON array of Rust versions to test against'
28+
required: false
29+
type: string
30+
default: '["stable"]'
31+
platforms:
32+
description: 'JSON array of platforms to run tests on'
33+
required: false
34+
type: string
35+
default: '["ubuntu-latest"]'
36+
test-script:
37+
description: 'Test script to execute'
38+
required: false
39+
type: string
40+
default: './run-tests.sh'
41+
examples-command:
42+
description: 'Examples command to execute'
43+
required: false
44+
type: string
45+
default: 'cargo check --examples'
46+
secrets:
47+
PIPELINE_GITHUB_APP_ID:
48+
required: false
49+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
50+
required: false
51+
# Integration test secrets
52+
DD_API_KEY:
53+
required: false
54+
DD_CLIENT_API_KEY:
55+
required: false
56+
DD_CLIENT_APP_KEY:
57+
required: false
58+
59+
jobs:
60+
test:
61+
uses: ./.github/workflows/reusable-rust-test.yml
62+
with:
63+
target-branch: ${{ inputs.target-branch }}
64+
rust-versions: ${{ inputs.rust-versions }}
65+
platforms: ${{ inputs.platforms }}
66+
test-script: ${{ inputs.test-script }}
67+
examples-command: ${{ inputs.examples-command }}
68+
enable-status-reporting: ${{ inputs.enable-status-reporting }}
69+
status-context: ${{ inputs.status-context }}
70+
secrets:
71+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
72+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
73+
74+
integration:
75+
uses: ./.github/workflows/reusable-integration-test.yml
76+
with:
77+
target-branch: ${{ inputs.target-branch }}
78+
enable-status-reporting: ${{ inputs.enable-status-reporting }}
79+
status-context: 'integration'
80+
target-repo: ${{ inputs.target-repo }}
81+
secrets:
82+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
83+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
84+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
85+
DD_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
86+
DD_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}

.github/workflows/test_integration.yml renamed to .github/workflows/reusable-integration-test.yml

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Run Integration Tests
1+
name: Reusable Integration Test Workflow
22

33
permissions:
44
contents: read
@@ -16,6 +16,39 @@ on:
1616
- unlabeled
1717
schedule:
1818
- cron: "0 5 * * *"
19+
workflow_call:
20+
inputs:
21+
target-branch:
22+
description: 'Branch to checkout and test (defaults to the calling branch)'
23+
required: false
24+
type: string
25+
default: ''
26+
enable-status-reporting:
27+
description: 'Whether to post status checks to datadog-api-spec repo'
28+
required: false
29+
type: boolean
30+
default: false
31+
status-context:
32+
description: 'Context for status checks'
33+
required: false
34+
type: string
35+
default: 'integration'
36+
target-repo:
37+
description: 'Repository to post status to'
38+
required: false
39+
type: string
40+
default: 'datadog-api-spec'
41+
secrets:
42+
PIPELINE_GITHUB_APP_ID:
43+
required: false
44+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
45+
required: false
46+
DD_API_KEY:
47+
required: true
48+
DD_CLIENT_API_KEY:
49+
required: true
50+
DD_CLIENT_APP_KEY:
51+
required: true
1952

2053
concurrency:
2154
group: integration-${{ github.head_ref }}
@@ -48,18 +81,19 @@ jobs:
4881
with:
4982
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
5083
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
51-
installation_retrieval_mode: repository
52-
installation_retrieval_payload: DataDog/datadog-api-spec
84+
repositories: ${{ inputs.target-repo || 'datadog-api-spec' }}
5385
- name: Checkout code
5486
uses: actions/checkout@v3
87+
with:
88+
ref: ${{ inputs.target-branch || github.ref }}
5589
- name: Post pending status check
56-
if: github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
90+
if: github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')
5791
uses: DataDog/github-actions/post-status-check@v2
5892
with:
5993
github-token: ${{ steps.get_token.outputs.token }}
60-
repo: datadog-api-spec
94+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
6195
status: pending
62-
context: integration
96+
context: ${{ inputs.status-context || 'integration' }}
6397
- name: Install Rust
6498
uses: dtolnay/rust-toolchain@v1
6599
with:
@@ -78,18 +112,18 @@ jobs:
78112
DD_TEST_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
79113
RECORD: "none"
80114
- name: Post failure status check
81-
if: failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
115+
if: failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')
82116
uses: DataDog/github-actions/post-status-check@v2
83117
with:
84118
github-token: ${{ steps.get_token.outputs.token }}
85-
repo: datadog-api-spec
119+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
86120
status: failure
87-
context: integration
121+
context: ${{ inputs.status-context || 'integration' }}
88122
- name: Post success status check
89-
if: "!failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')"
123+
if: "!failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')"
90124
uses: DataDog/github-actions/post-status-check@v2
91125
with:
92126
github-token: ${{ steps.get_token.outputs.token }}
93-
repo: datadog-api-spec
127+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
94128
status: success
95-
context: integration
129+
context: ${{ inputs.status-context || 'integration' }}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
name: Reusable Rust Testing Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target-branch:
7+
description: 'Branch to checkout and test (defaults to the calling branch)'
8+
required: false
9+
type: string
10+
default: ''
11+
rust-versions:
12+
description: 'JSON array of Rust versions to test against'
13+
required: false
14+
type: string
15+
default: '["stable"]'
16+
platforms:
17+
description: 'JSON array of platforms to run tests on'
18+
required: false
19+
type: string
20+
default: '["ubuntu-latest"]'
21+
test-script:
22+
description: 'Test script to execute'
23+
required: false
24+
type: string
25+
default: './run-tests.sh'
26+
examples-command:
27+
description: 'Examples command to execute'
28+
required: false
29+
type: string
30+
default: 'cargo check --examples'
31+
enable-status-reporting:
32+
description: 'Whether to post status checks to datadog-api-spec repo'
33+
required: false
34+
type: boolean
35+
default: false
36+
status-context:
37+
description: 'Context for status checks'
38+
required: false
39+
type: string
40+
default: 'master/unit'
41+
secrets:
42+
PIPELINE_GITHUB_APP_ID:
43+
required: false
44+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
45+
required: false
46+
47+
jobs:
48+
pre-commit:
49+
runs-on: ubuntu-latest
50+
if: >
51+
(github.event.pull_request.draft == false &&
52+
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
53+
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
54+
github.event_name == 'schedule'
55+
steps:
56+
- name: Get GitHub App token
57+
if: github.event_name == 'pull_request'
58+
id: get_token
59+
uses: actions/create-github-app-token@v1
60+
with:
61+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
62+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
63+
repositories: datadog-api-spec
64+
- uses: actions/checkout@v3
65+
with:
66+
ref: ${{ inputs.target-branch || github.ref }}
67+
with:
68+
fetch-depth: 0
69+
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
70+
token: ${{ steps.get_token.outputs.token }}
71+
- name: Install pre-commit
72+
run: python -m pip install pre-commit
73+
- name: set PY
74+
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
75+
- uses: actions/cache@v3
76+
with:
77+
path: ~/.cache/pre-commit
78+
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
79+
- id: pre_commit
80+
name: Run pre-commit
81+
if: github.event.action != 'closed' && github.event.pull_request.merged != true
82+
run: |
83+
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
84+
env:
85+
FROM_REF: ${{ github.event.pull_request.base.sha }}
86+
TO_REF: ${{ github.event.pull_request.head.sha }}
87+
- name: Commit changes
88+
if: ${{ failure() }}
89+
run: |-
90+
git add -A
91+
git config user.name "${GIT_AUTHOR_NAME}"
92+
git config user.email "${GIT_AUTHOR_EMAIL}"
93+
git commit -m "pre-commit fixes"
94+
git push origin "HEAD:${HEAD_REF}"
95+
exit 1
96+
env:
97+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
98+
GIT_AUTHOR_EMAIL: "[email protected]"
99+
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
100+
- id: pre_commit_schedule
101+
name: Run pre-commit in schedule
102+
if: github.event_name == 'schedule'
103+
run: |
104+
pre-commit run --all-files --show-diff-on-failure --color=always
105+
106+
test:
107+
strategy:
108+
matrix:
109+
rust-version: ${{ fromJSON(inputs.rust-versions) }}
110+
platform: ${{ fromJSON(inputs.platforms) }}
111+
runs-on: ${{ matrix.platform }}
112+
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
113+
steps:
114+
- name: Checkout code
115+
uses: actions/checkout@v3
116+
with:
117+
ref: ${{ inputs.target-branch || github.ref }}
118+
- name: Install Rust
119+
uses: dtolnay/rust-toolchain@v1
120+
with:
121+
toolchain: ${{ matrix.rust-version }}
122+
- uses: Swatinem/rust-cache@v2
123+
- name: Test
124+
run: ${{ inputs.test-script }}
125+
126+
examples:
127+
runs-on: ubuntu-latest
128+
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
129+
steps:
130+
- uses: actions/checkout@v3
131+
with:
132+
ref: ${{ inputs.target-branch || github.ref }}
133+
- name: Install Rust
134+
uses: dtolnay/rust-toolchain@master
135+
with:
136+
toolchain: stable
137+
- name: Check examples
138+
run: ${{ inputs.examples-command }}
139+
shell: bash
140+
141+
report:
142+
runs-on: ubuntu-latest
143+
if: always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && inputs.enable-status-reporting
144+
needs:
145+
- test
146+
- examples
147+
steps:
148+
- name: Get GitHub App token
149+
if: github.event_name == 'pull_request'
150+
id: get_token
151+
uses: actions/create-github-app-token@v1
152+
with:
153+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
154+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
155+
repositories: datadog-api-spec
156+
- name: Post status check
157+
uses: DataDog/github-actions/post-status-check@v2
158+
with:
159+
github-token: ${{ steps.get_token.outputs.token }}
160+
repo: datadog-api-spec
161+
status: ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }}
162+
context: ${{ inputs.status-context }}

0 commit comments

Comments
 (0)