Skip to content

Commit a49948c

Browse files
committed
Make Rust test workflow reusable
1 parent dffc362 commit a49948c

File tree

7 files changed

+425
-87
lines changed

7 files changed

+425
-87
lines changed

.github/workflows/reusable-ci.yml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
pre-commit:
61+
uses: ./.github/workflows/reusable-pre-commit.yml
62+
with:
63+
target-branch: ${{ inputs.target-branch }}
64+
enable-commit-changes: false # Don't auto-commit in external CI
65+
secrets:
66+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
67+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
68+
69+
test:
70+
uses: ./.github/workflows/reusable-rust-test.yml
71+
with:
72+
target-branch: ${{ inputs.target-branch }}
73+
rust-versions: ${{ inputs.rust-versions }}
74+
platforms: ${{ inputs.platforms }}
75+
test-script: ${{ inputs.test-script }}
76+
secrets:
77+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
78+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
79+
80+
examples:
81+
uses: ./.github/workflows/reusable-examples.yml
82+
with:
83+
target-branch: ${{ inputs.target-branch }}
84+
examples-command: ${{ inputs.examples-command }}
85+
secrets:
86+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
87+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
88+
89+
integration:
90+
uses: ./.github/workflows/reusable-integration-test.yml
91+
with:
92+
target-branch: ${{ inputs.target-branch }}
93+
enable-status-reporting: false # We handle reporting in the main report job
94+
status-context: 'integration'
95+
target-repo: ${{ inputs.target-repo }}
96+
secrets:
97+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
98+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
99+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
100+
DD_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
101+
DD_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
102+
103+
report:
104+
uses: ./.github/workflows/reusable-report.yml
105+
needs:
106+
- test
107+
- examples
108+
- integration
109+
if: always()
110+
with:
111+
test-result: ${{ needs.test.result }}
112+
examples-result: ${{ needs.examples.result }}
113+
context: ${{ inputs.status-context }}
114+
target-repo: ${{ inputs.target-repo }}
115+
enable-status-reporting: ${{ inputs.enable-status-reporting }}
116+
secrets:
117+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
118+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Reusable Examples 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+
examples-command:
12+
description: 'Examples command to execute'
13+
required: false
14+
type: string
15+
default: 'cargo check --examples'
16+
rust-version:
17+
description: 'Rust version to use for examples'
18+
required: false
19+
type: string
20+
default: 'stable'
21+
secrets:
22+
PIPELINE_GITHUB_APP_ID:
23+
required: false
24+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
25+
required: false
26+
27+
jobs:
28+
examples:
29+
runs-on: ubuntu-latest
30+
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'
31+
steps:
32+
- uses: actions/checkout@v3
33+
with:
34+
ref: ${{ inputs.target-branch || github.ref }}
35+
- name: Install Rust
36+
uses: dtolnay/rust-toolchain@master
37+
with:
38+
toolchain: ${{ inputs.rust-version }}
39+
- uses: Swatinem/rust-cache@v2
40+
- name: Check examples
41+
run: ${{ inputs.examples-command }}
42+
shell: bash

.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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Reusable Pre-commit 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-commit-changes:
12+
description: 'Whether to commit and push pre-commit fixes'
13+
required: false
14+
type: boolean
15+
default: true
16+
secrets:
17+
PIPELINE_GITHUB_APP_ID:
18+
required: false
19+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
20+
required: false
21+
22+
jobs:
23+
pre-commit:
24+
runs-on: ubuntu-latest
25+
if: >
26+
(github.event.pull_request.draft == false &&
27+
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
28+
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
29+
github.event_name == 'schedule'
30+
steps:
31+
- name: Get GitHub App token
32+
if: inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository
33+
id: get_token
34+
uses: actions/create-github-app-token@v1
35+
with:
36+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
37+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
38+
- uses: actions/checkout@v3
39+
if: github.event.pull_request.head.repo.full_name == github.repository
40+
with:
41+
fetch-depth: 0
42+
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
43+
token: ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }}
44+
- uses: actions/checkout@v3
45+
if: github.event.pull_request.head.repo.full_name != github.repository
46+
with:
47+
ref: ${{ inputs.target-branch || github.ref }}
48+
- name: Install pre-commit
49+
run: python -m pip install pre-commit
50+
- name: set PY
51+
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
52+
- uses: actions/cache@v3
53+
with:
54+
path: ~/.cache/pre-commit
55+
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
56+
- id: pre_commit
57+
name: Run pre-commit
58+
if: github.event.action != 'closed' && github.event.pull_request.merged != true
59+
run: |
60+
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
61+
env:
62+
FROM_REF: ${{ github.event.pull_request.base.sha }}
63+
TO_REF: ${{ github.event.pull_request.head.sha }}
64+
- name: Commit changes
65+
if: failure() && inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository
66+
run: |-
67+
git add -A
68+
git config user.name "${GIT_AUTHOR_NAME}"
69+
git config user.email "${GIT_AUTHOR_EMAIL}"
70+
git commit -m "pre-commit fixes"
71+
git push origin "HEAD:${HEAD_REF}"
72+
exit 1
73+
env:
74+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
75+
GIT_AUTHOR_EMAIL: "packages@datadoghq.com"
76+
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
77+
- id: pre_commit_schedule
78+
name: Run pre-commit in schedule
79+
if: github.event_name == 'schedule'
80+
run: |
81+
pre-commit run --all-files --show-diff-on-failure --color=always

0 commit comments

Comments
 (0)