Skip to content

Commit e9001f9

Browse files
committed
Make Ruby test workflow reusable
1 parent 237cbc1 commit e9001f9

File tree

6 files changed

+353
-105
lines changed

6 files changed

+353
-105
lines changed

.github/workflows/reusable-ci.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
cache-version:
12+
description: 'Cache version for gem dependencies'
13+
required: false
14+
type: string
15+
default: ''
16+
17+
secrets:
18+
PIPELINE_GITHUB_APP_ID:
19+
required: false
20+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
21+
required: false
22+
# Integration test secrets
23+
DD_API_KEY:
24+
required: false
25+
DD_CLIENT_API_KEY:
26+
required: false
27+
DD_CLIENT_APP_KEY:
28+
required: false
29+
SLEEP_AFTER_REQUEST:
30+
required: false
31+
32+
jobs:
33+
pre-commit:
34+
uses: ./.github/workflows/reusable-pre-commit.yml
35+
with:
36+
target-branch: ${{ inputs.target-branch }}
37+
enable-commit-changes: false # Don't auto-commit in external CI
38+
secrets:
39+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
40+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
41+
42+
test:
43+
uses: ./.github/workflows/reusable-ruby-test.yml
44+
with:
45+
target-branch: ${{ inputs.target-branch }}
46+
cache-version: ${{ inputs.cache-version }}
47+
secrets:
48+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
49+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
50+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
51+
52+
examples:
53+
uses: ./.github/workflows/reusable-examples.yml
54+
with:
55+
target-branch: ${{ inputs.target-branch }}
56+
cache-version: ${{ inputs.cache-version }}
57+
58+
integration:
59+
uses: ./.github/workflows/reusable-integration-test.yml
60+
with:
61+
target-branch: ${{ inputs.target-branch }}
62+
cache-version: ${{ inputs.cache-version }}
63+
secrets:
64+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
65+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
66+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
67+
DD_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
68+
DD_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
69+
SLEEP_AFTER_REQUEST: ${{ secrets.SLEEP_AFTER_REQUEST }}
70+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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-script:
12+
description: 'Examples script to execute'
13+
required: false
14+
type: string
15+
default: './check-examples.sh'
16+
cache-version:
17+
description: 'Cache version for gem dependencies'
18+
required: false
19+
type: string
20+
default: ''
21+
22+
jobs:
23+
examples:
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+
env:
31+
DD_PROFILING_NO_EXTENSION: true
32+
steps:
33+
- uses: actions/checkout@v3
34+
with:
35+
repository: DataDog/datadog-api-client-ruby
36+
ref: ${{ inputs.target-branch || github.ref }}
37+
- name: Set up Ruby
38+
uses: ruby/setup-ruby@v1
39+
with:
40+
ruby-version: "2.7"
41+
bundler-cache: true
42+
cache-version: ${{ inputs.cache-version }}
43+
- name: Check examples
44+
run: ${{ inputs.examples-script }}
45+
shell: bash

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

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,68 @@
1-
name: Run Integration Tests
1+
name: Reusable Integration Test Workflow
22

33
permissions:
44
contents: read
55

66
on:
77
pull_request:
8+
branches:
9+
- master
810
types:
911
- opened
1012
- reopened
1113
- ready_for_review
1214
- synchronize
1315
- labeled
1416
- unlabeled
15-
branches:
16-
- master
1717
schedule:
1818
- cron: "0 2 * * *"
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+
cache-version:
42+
description: 'Cache version for gem dependencies'
43+
required: false
44+
type: string
45+
default: ''
46+
secrets:
47+
PIPELINE_GITHUB_APP_ID:
48+
required: false
49+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
50+
required: false
51+
DD_API_KEY:
52+
required: true
53+
DD_CLIENT_API_KEY:
54+
required: true
55+
DD_CLIENT_APP_KEY:
56+
required: true
57+
SLEEP_AFTER_REQUEST:
58+
required: false
1959

2060
concurrency:
2161
group: integration-${{ github.head_ref }}
2262
cancel-in-progress: true
2363

2464
jobs:
25-
test_integration:
65+
integration_tests:
2666
runs-on: ubuntu-latest
2767
if: >
2868
(github.event_name == 'pull_request' &&
@@ -48,17 +88,20 @@ jobs:
4888
with:
4989
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
5090
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
51-
repositories: datadog-api-spec
91+
repositories: ${{ inputs.target-repo || 'datadog-api-spec' }}
5292
- name: Checkout code
5393
uses: actions/checkout@v3
94+
with:
95+
repository: DataDog/datadog-api-client-ruby
96+
ref: ${{ inputs.target-branch || github.ref }}
5497
- name: Post pending status check
55-
if: github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
98+
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')
5699
uses: DataDog/github-actions/post-status-check@v2
57100
with:
58101
github-token: ${{ steps.get_token.outputs.token }}
59-
repo: datadog-api-spec
102+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
60103
status: pending
61-
context: integration
104+
context: ${{ inputs.status-context || 'integration' }}
62105
- name: Install system zstd
63106
run: |
64107
sudo apt-get -y install zstd
@@ -68,7 +111,7 @@ jobs:
68111
with:
69112
ruby-version: "3.2"
70113
bundler-cache: true
71-
cache-version: ${{ secrets.CACHE_VERSION }}
114+
cache-version: ${{ inputs.cache-version }}
72115
- name: Install deps
73116
run: bundle install
74117
- name: Run integration tests
@@ -84,20 +127,20 @@ jobs:
84127
DD_TEST_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
85128
DD_TRACE_ANALYTICS_ENABLED: "true"
86129
RECORD: "none"
87-
SLEEP_AFTER_REQUEST: "${{ vars.SLEEP_AFTER_REQUEST }}"
130+
SLEEP_AFTER_REQUEST: ${{ secrets.SLEEP_AFTER_REQUEST || vars.SLEEP_AFTER_REQUEST }}
88131
- name: Post failure status check
89-
if: failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
132+
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')
90133
uses: DataDog/github-actions/post-status-check@v2
91134
with:
92135
github-token: ${{ steps.get_token.outputs.token }}
93-
repo: datadog-api-spec
136+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
94137
status: failure
95-
context: integration
138+
context: ${{ inputs.status-context || 'integration' }}
96139
- name: Post success status check
97-
if: "!failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')"
140+
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')"
98141
uses: DataDog/github-actions/post-status-check@v2
99142
with:
100143
github-token: ${{ steps.get_token.outputs.token }}
101-
repo: datadog-api-spec
144+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
102145
status: success
103-
context: integration
146+
context: ${{ inputs.status-context || 'integration' }}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
env:
23+
GIT_AUTHOR_EMAIL: "packages@datadoghq.com"
24+
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
25+
26+
jobs:
27+
pre-commit:
28+
runs-on: ubuntu-latest
29+
if: >
30+
(github.event.pull_request.draft == false &&
31+
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
32+
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
33+
github.event_name == 'schedule'
34+
steps:
35+
- name: Get GitHub App token
36+
id: get_token
37+
if: inputs.enable-commit-changes
38+
uses: actions/create-github-app-token@v1
39+
with:
40+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
41+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
42+
- uses: actions/checkout@v3
43+
with:
44+
fetch-depth: 0
45+
repository: DataDog/datadog-api-client-ruby
46+
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
47+
token: ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }}
48+
- uses: actions/setup-python@v4
49+
with:
50+
python-version: '3.11'
51+
- name: Install pre-commit
52+
run: python -m pip install pre-commit
53+
- name: set PY
54+
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
55+
- uses: actions/cache@v3
56+
with:
57+
path: ~/.cache/pre-commit
58+
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
59+
- id: pre_commit
60+
name: Run pre-commit
61+
if: github.event.action != 'closed' && github.event.pull_request.merged != true
62+
run: |
63+
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
64+
env:
65+
FROM_REF: ${{ github.event.pull_request.base.sha }}
66+
TO_REF: ${{ github.event.pull_request.head.sha }}
67+
- name: Commit changes
68+
if: ${{ failure() && inputs.enable-commit-changes }}
69+
run: |-
70+
git add -A
71+
git config user.name "${GIT_AUTHOR_NAME}"
72+
git config user.email "${GIT_AUTHOR_EMAIL}"
73+
git commit -m "pre-commit fixes"
74+
git push origin "HEAD:${HEAD_REF}"
75+
exit 1
76+
env:
77+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
78+
- id: pre_commit_schedule
79+
name: Run pre-commit in schedule
80+
if: github.event_name == 'schedule'
81+
run: |
82+
pre-commit run --all-files --show-diff-on-failure --color=always

0 commit comments

Comments
 (0)