Skip to content

Commit 219117e

Browse files
authored
Convert Rust testing workflow to reusable workflow (#899)
1 parent 3945473 commit 219117e

File tree

6 files changed

+337
-87
lines changed

6 files changed

+337
-87
lines changed

.github/workflows/reusable-ci.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
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+
secrets:
32+
PIPELINE_GITHUB_APP_ID:
33+
required: false
34+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
35+
required: false
36+
# Integration test secrets
37+
DD_API_KEY:
38+
required: false
39+
DD_CLIENT_API_KEY:
40+
required: false
41+
DD_CLIENT_APP_KEY:
42+
required: false
43+
44+
jobs:
45+
pre-commit:
46+
uses: ./.github/workflows/reusable-pre-commit.yml
47+
with:
48+
target-branch: ${{ inputs.target-branch }}
49+
enable-commit-changes: false # Don't auto-commit in external CI
50+
secrets:
51+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
52+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
53+
54+
test:
55+
uses: ./.github/workflows/reusable-rust-test.yml
56+
with:
57+
target-branch: ${{ inputs.target-branch }}
58+
rust-versions: ${{ inputs.rust-versions }}
59+
platforms: ${{ inputs.platforms }}
60+
test-script: ${{ inputs.test-script }}
61+
secrets:
62+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
63+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
64+
65+
examples:
66+
uses: ./.github/workflows/reusable-examples.yml
67+
with:
68+
target-branch: ${{ inputs.target-branch }}
69+
examples-command: ${{ inputs.examples-command }}
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+
secrets:
79+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
80+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
81+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
82+
DD_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
83+
DD_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
84+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
repository: DataDog/datadog-api-client-rust
35+
ref: ${{ inputs.target-branch || github.ref }}
36+
- name: Install Rust
37+
uses: dtolnay/rust-toolchain@master
38+
with:
39+
toolchain: ${{ inputs.rust-version }}
40+
- uses: Swatinem/rust-cache@v2
41+
- name: Check examples
42+
run: ${{ inputs.examples-command }}
43+
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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
repository: DataDog/datadog-api-client-rust
43+
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
44+
token: ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }}
45+
- uses: actions/checkout@v3
46+
if: github.event.pull_request.head.repo.full_name != github.repository
47+
with:
48+
repository: DataDog/datadog-api-client-rust
49+
ref: ${{ inputs.target-branch || github.ref }}
50+
- name: Install pre-commit
51+
run: python -m pip install pre-commit
52+
- name: set PY
53+
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
54+
- uses: actions/cache@v3
55+
with:
56+
path: ~/.cache/pre-commit
57+
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
58+
- id: pre_commit
59+
name: Run pre-commit
60+
if: github.event.action != 'closed' && github.event.pull_request.merged != true
61+
run: |
62+
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
63+
env:
64+
FROM_REF: ${{ github.event.pull_request.base.sha }}
65+
TO_REF: ${{ github.event.pull_request.head.sha }}
66+
- name: Commit changes
67+
if: failure() && inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository
68+
run: |-
69+
git add -A
70+
git config user.name "${GIT_AUTHOR_NAME}"
71+
git config user.email "${GIT_AUTHOR_EMAIL}"
72+
git commit -m "pre-commit fixes"
73+
git push origin "HEAD:${HEAD_REF}"
74+
exit 1
75+
env:
76+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
77+
GIT_AUTHOR_EMAIL: "packages@datadoghq.com"
78+
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
79+
- id: pre_commit_schedule
80+
name: Run pre-commit in schedule
81+
if: github.event_name == 'schedule'
82+
run: |
83+
pre-commit run --all-files --show-diff-on-failure --color=always
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
secrets:
27+
PIPELINE_GITHUB_APP_ID:
28+
required: false
29+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
30+
required: false
31+
32+
jobs:
33+
test:
34+
strategy:
35+
matrix:
36+
rust-version: ${{ fromJSON(inputs.rust-versions) }}
37+
platform: ${{ fromJSON(inputs.platforms) }}
38+
runs-on: ${{ matrix.platform }}
39+
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'
40+
steps:
41+
- name: Checkout code
42+
uses: actions/checkout@v3
43+
with:
44+
repository: DataDog/datadog-api-client-rust
45+
ref: ${{ inputs.target-branch || github.ref }}
46+
- name: Install Rust
47+
uses: dtolnay/rust-toolchain@v1
48+
with:
49+
toolchain: ${{ matrix.rust-version }}
50+
- uses: Swatinem/rust-cache@v2
51+
- name: Test
52+
run: ${{ inputs.test-script }}

0 commit comments

Comments
 (0)