Skip to content

Commit 30b04f0

Browse files
committed
Make Ruby test workflow reusable
1 parent 132d7fd commit 30b04f0

File tree

2 files changed

+183
-115
lines changed

2 files changed

+183
-115
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
name: Reusable Ruby Testing Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
ruby-versions:
7+
description: 'JSON array of Ruby versions to test against'
8+
required: false
9+
type: string
10+
default: '["2.7", "3.2", "jruby-9.4.12.0"]'
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-script:
22+
description: 'Examples script to execute'
23+
required: false
24+
type: string
25+
default: './check-examples.sh'
26+
cache-version:
27+
description: 'Cache version for gem dependencies'
28+
required: false
29+
type: string
30+
default: ''
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+
DD_API_KEY:
47+
required: false
48+
49+
jobs:
50+
pre-commit:
51+
runs-on: ubuntu-latest
52+
if: >
53+
(github.event.pull_request.draft == false &&
54+
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
55+
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
56+
github.event_name == 'schedule'
57+
steps:
58+
- name: Get GitHub App token
59+
id: get_token
60+
uses: actions/create-github-app-token@v1
61+
with:
62+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
63+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
64+
- uses: actions/checkout@v3
65+
with:
66+
fetch-depth: 0
67+
ref: ${{ github.event.pull_request.head.sha }}
68+
token: ${{ steps.get_token.outputs.token }}
69+
- uses: actions/setup-python@v4
70+
with:
71+
python-version: '3.11'
72+
- name: Install pre-commit
73+
run: python -m pip install pre-commit
74+
- name: set PY
75+
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
76+
- uses: actions/cache@v3
77+
with:
78+
path: ~/.cache/pre-commit
79+
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
80+
- id: pre_commit
81+
name: Run pre-commit
82+
if: github.event.action != 'closed' && github.event.pull_request.merged != true
83+
run: |
84+
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
85+
env:
86+
FROM_REF: ${{ github.event.pull_request.base.sha }}
87+
TO_REF: ${{ github.event.pull_request.head.sha }}
88+
- name: Commit changes
89+
if: ${{ failure() }}
90+
run: |-
91+
git add -A
92+
git config user.name "${GIT_AUTHOR_NAME}"
93+
git config user.email "${GIT_AUTHOR_EMAIL}"
94+
git commit -m "pre-commit fixes"
95+
git push origin "HEAD:${HEAD_REF}"
96+
exit 1
97+
env:
98+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
99+
GIT_AUTHOR_EMAIL: "packages@datadoghq.com"
100+
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
101+
- id: pre_commit_schedule
102+
name: Run pre-commit in schedule
103+
if: github.event_name == 'schedule'
104+
run: |
105+
pre-commit run --all-files --show-diff-on-failure --color=always
106+
107+
test:
108+
strategy:
109+
matrix:
110+
ruby-version: ${{ fromJSON(inputs.ruby-versions) }}
111+
platform: ${{ fromJSON(inputs.platforms) }}
112+
runs-on: ${{ matrix.platform }}
113+
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'
114+
env:
115+
BUNDLE_WITHOUT: docs
116+
DD_PROFILING_NO_EXTENSION: true
117+
DD_CIVISIBILITY_AGENTLESS_ENABLED: true
118+
DD_ENV: prod
119+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
120+
steps:
121+
- uses: actions/checkout@v3
122+
- name: Set up Ruby ${{ matrix.ruby-version }}
123+
uses: ruby/setup-ruby@v1
124+
with:
125+
ruby-version: ${{ matrix.ruby-version }}
126+
bundler-cache: true
127+
cache-version: ${{ inputs.cache-version }}
128+
- name: Test
129+
run: ${{ inputs.test-script }}
130+
shell: bash
131+
132+
examples:
133+
runs-on: ubuntu-latest
134+
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'
135+
env:
136+
DD_PROFILING_NO_EXTENSION: true
137+
steps:
138+
- uses: actions/checkout@v3
139+
- name: Set up Ruby
140+
uses: ruby/setup-ruby@v1
141+
with:
142+
ruby-version: "2.7"
143+
bundler-cache: true
144+
cache-version: ${{ inputs.cache-version }}
145+
- name: Check examples
146+
run: ${{ inputs.examples-script }}
147+
shell: bash
148+
149+
report:
150+
runs-on: ubuntu-latest
151+
if: always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && inputs.enable-status-reporting
152+
needs:
153+
- test
154+
- examples
155+
steps:
156+
- name: Get GitHub App token
157+
if: github.event_name == 'pull_request'
158+
id: get_token
159+
uses: actions/create-github-app-token@v1
160+
with:
161+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
162+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
163+
repositories: datadog-api-spec
164+
- name: Post status check
165+
uses: DataDog/github-actions/post-status-check@v2
166+
with:
167+
github-token: ${{ steps.get_token.outputs.token }}
168+
repo: datadog-api-spec
169+
status: ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }}
170+
context: ${{ inputs.status-context }}

.github/workflows/test.yml

Lines changed: 13 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -19,124 +19,22 @@ concurrency:
1919
cancel-in-progress: true
2020

2121
jobs:
22-
pre-commit:
23-
runs-on: ubuntu-latest
22+
ruby-test:
23+
uses: ./.github/workflows/reusable-ruby-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-
id: get_token
32-
uses: actions/create-github-app-token@v1
33-
with:
34-
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
35-
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
36-
- uses: actions/checkout@v3
37-
with:
38-
fetch-depth: 0
39-
ref: ${{ github.event.pull_request.head.sha }}
40-
token: ${{ steps.get_token.outputs.token }}
41-
- uses: actions/setup-python@v4
42-
with:
43-
python-version: '3.11'
44-
- name: Install pre-commit
45-
run: python -m pip install pre-commit
46-
- name: set PY
47-
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
48-
- uses: actions/cache@v3
49-
with:
50-
path: ~/.cache/pre-commit
51-
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
52-
- id: pre_commit
53-
name: Run pre-commit
54-
if: github.event.action != 'closed' && github.event.pull_request.merged != true
55-
run: |
56-
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
57-
env:
58-
FROM_REF: ${{ github.event.pull_request.base.sha }}
59-
TO_REF: ${{ github.event.pull_request.head.sha }}
60-
- name: Commit changes
61-
if: ${{ failure() }}
62-
run: |-
63-
git add -A
64-
git config user.name "${GIT_AUTHOR_NAME}"
65-
git config user.email "${GIT_AUTHOR_EMAIL}"
66-
git commit -m "pre-commit fixes"
67-
git push origin "HEAD:${HEAD_REF}"
68-
exit 1
69-
env:
70-
HEAD_REF: ${{ github.event.pull_request.head.ref }}
71-
- id: pre_commit_schedule
72-
name: Run pre-commit in schedule
73-
if: github.event_name == 'schedule'
74-
run: |
75-
pre-commit run --all-files --show-diff-on-failure --color=always
76-
77-
test:
78-
strategy:
79-
matrix:
80-
ruby-version: ["2.7", "3.2", "jruby-9.4.12.0"]
81-
platform: [ubuntu-latest]
82-
runs-on: ${{ matrix.platform }}
83-
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'
84-
env:
85-
BUNDLE_WITHOUT: docs
86-
DD_PROFILING_NO_EXTENSION: true
87-
DD_CIVISIBILITY_AGENTLESS_ENABLED: true
88-
DD_ENV: prod
29+
with:
30+
ruby-versions: '["2.7", "3.2", "jruby-9.4.12.0"]'
31+
platforms: '["ubuntu-latest"]'
32+
test-script: './run-tests.sh'
33+
examples-script: './check-examples.sh'
34+
cache-version: ${{ vars.CACHE_VERSION }}
35+
enable-status-reporting: ${{ contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') }}
36+
status-context: 'master/unit'
37+
secrets:
38+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
39+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
8940
DD_API_KEY: ${{ secrets.DD_API_KEY }}
90-
steps:
91-
- uses: actions/checkout@v3
92-
- name: Set up Ruby ${{ matrix.ruby-version }}
93-
uses: ruby/setup-ruby@v1
94-
with:
95-
ruby-version: ${{ matrix.ruby-version }}
96-
bundler-cache: true
97-
# modify repository variable when there are problems with installing gems
98-
cache-version: ${{ vars.CACHE_VERSION }}
99-
- name: Test
100-
run: ./run-tests.sh
101-
shell: bash
102-
103-
examples:
104-
runs-on: ubuntu-latest
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-
env:
107-
DD_PROFILING_NO_EXTENSION: true
108-
steps:
109-
- uses: actions/checkout@v3
110-
- name: Set up Ruby
111-
uses: ruby/setup-ruby@v1
112-
with:
113-
ruby-version: "2.7"
114-
bundler-cache: true
115-
# modify repository variable when there are problems with installing gems
116-
cache-version: ${{ vars.CACHE_VERSION }}
117-
- name: Check examples
118-
run: ./check-examples.sh
119-
shell: bash
120-
121-
report:
122-
runs-on: ubuntu-latest
123-
if: always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
124-
needs:
125-
- test
126-
- examples
127-
steps:
128-
- name: Get GitHub App token
129-
if: github.event_name == 'pull_request'
130-
id: get_token
131-
uses: actions/create-github-app-token@v1
132-
with:
133-
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
134-
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
135-
repositories: datadog-api-spec
136-
- name: Post status 3heck
137-
uses: DataDog/github-actions/post-status-check@v2
138-
with:
139-
github-token: ${{ steps.get_token.outputs.token }}
140-
repo: datadog-api-spec
141-
status: ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }}
142-
context: master/unit

0 commit comments

Comments
 (0)