Skip to content

Commit 20e8ac7

Browse files
committed
Make Rust test workflow reusable
1 parent 0e70568 commit 20e8ac7

File tree

2 files changed

+163
-102
lines changed

2 files changed

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

.github/workflows/test.yml

Lines changed: 12 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -19,110 +19,20 @@ concurrency:
1919
cancel-in-progress: true
2020

2121
jobs:
22-
pre-commit:
23-
runs-on: ubuntu-latest
22+
rust-test:
23+
uses: ./.github/workflows/reusable-rust-test.yml
2424
if: >
2525
(github.event.pull_request.draft == false &&
2626
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
2727
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
2828
github.event_name == 'schedule'
29-
steps:
30-
- name: Get GitHub App token
31-
if: github.event_name == 'pull_request'
32-
id: get_token
33-
uses: actions/create-github-app-token@v1
34-
with:
35-
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
36-
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
37-
repositories: datadog-api-spec
38-
- uses: actions/checkout@v3
39-
with:
40-
fetch-depth: 0
41-
ref: ${{ github.event.pull_request.head.sha }}
42-
token: ${{ steps.get_token.outputs.token }}
43-
- name: Install pre-commit
44-
run: python -m pip install pre-commit
45-
- name: set PY
46-
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
47-
- uses: actions/cache@v3
48-
with:
49-
path: ~/.cache/pre-commit
50-
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
51-
- id: pre_commit
52-
name: Run pre-commit
53-
if: github.event.action != 'closed' && github.event.pull_request.merged != true
54-
run: |
55-
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
56-
env:
57-
FROM_REF: ${{ github.event.pull_request.base.sha }}
58-
TO_REF: ${{ github.event.pull_request.head.sha }}
59-
- name: Commit changes
60-
if: ${{ failure() }}
61-
run: |-
62-
git add -A
63-
git config user.name "${GIT_AUTHOR_NAME}"
64-
git config user.email "${GIT_AUTHOR_EMAIL}"
65-
git commit -m "pre-commit fixes"
66-
git push origin "HEAD:${HEAD_REF}"
67-
exit 1
68-
env:
69-
HEAD_REF: ${{ github.event.pull_request.head.ref }}
70-
- id: pre_commit_schedule
71-
name: Run pre-commit in schedule
72-
if: github.event_name == 'schedule'
73-
run: |
74-
pre-commit run --all-files --show-diff-on-failure --color=always
75-
76-
test:
77-
strategy:
78-
matrix:
79-
rust-version: ["stable"]
80-
platform: [ubuntu-latest]
81-
runs-on: ${{ matrix.platform }}
82-
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'
83-
steps:
84-
- name: Checkout code
85-
uses: actions/checkout@v3
86-
- name: Install Rust
87-
uses: dtolnay/rust-toolchain@v1
88-
with:
89-
toolchain: ${{ matrix.rust-version }}
90-
- uses: Swatinem/rust-cache@v2
91-
- name: Test
92-
run: ./run-tests.sh
93-
94-
examples:
95-
runs-on: ubuntu-latest
96-
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'
97-
steps:
98-
- uses: actions/checkout@v3
99-
- name: Install Rust
100-
uses: dtolnay/rust-toolchain@master
101-
with:
102-
toolchain: stable
103-
- name: Check examples
104-
run: cargo check --examples
105-
shell: bash
106-
107-
report:
108-
runs-on: ubuntu-latest
109-
if: always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
110-
needs:
111-
- test
112-
- examples
113-
steps:
114-
- name: Get GitHub App token
115-
if: github.event_name == 'pull_request'
116-
id: get_token
117-
uses: actions/create-github-app-token@v1
118-
with:
119-
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
120-
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
121-
repositories: datadog-api-spec
122-
- name: Post status check
123-
uses: DataDog/github-actions/post-status-check@v2
124-
with:
125-
github-token: ${{ steps.get_token.outputs.token }}
126-
repo: datadog-api-spec
127-
status: ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }}
128-
context: master/unit
29+
with:
30+
rust-versions: '["stable"]'
31+
platforms: '["ubuntu-latest"]'
32+
test-script: './run-tests.sh'
33+
examples-command: 'cargo check --examples'
34+
enable-status-reporting: ${{ contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') }}
35+
status-context: 'master/unit'
36+
secrets:
37+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
38+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}

0 commit comments

Comments
 (0)