Skip to content

Commit 643f806

Browse files
committed
Make Ruby test workflow reusable
1 parent 132d7fd commit 643f806

File tree

7 files changed

+453
-105
lines changed

7 files changed

+453
-105
lines changed

.github/workflows/reusable-ci.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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-commit-changes:
12+
description: 'Whether to commit and push pre-commit fixes'
13+
required: false
14+
type: boolean
15+
default: true
16+
enable-status-reporting:
17+
description: 'Whether to enable status reporting'
18+
required: false
19+
type: boolean
20+
default: true
21+
status-context:
22+
description: 'Context for status checks'
23+
required: false
24+
type: string
25+
default: 'master/unit'
26+
target-repo:
27+
description: 'Repository to post status to'
28+
required: false
29+
type: string
30+
default: 'datadog-api-spec'
31+
cache-version:
32+
description: 'Cache version for gem dependencies'
33+
required: false
34+
type: string
35+
default: ''
36+
37+
secrets:
38+
PIPELINE_GITHUB_APP_ID:
39+
required: false
40+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
41+
required: false
42+
# Integration test secrets
43+
DD_API_KEY:
44+
required: false
45+
DD_CLIENT_API_KEY:
46+
required: false
47+
DD_CLIENT_APP_KEY:
48+
required: false
49+
SLEEP_AFTER_REQUEST:
50+
required: false
51+
52+
jobs:
53+
pre-commit:
54+
uses: ./.github/workflows/reusable-pre-commit.yml
55+
with:
56+
target-branch: ${{ inputs.target-branch }}
57+
enable-commit-changes: ${{ inputs.enable-commit-changes }}
58+
secrets:
59+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
60+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
61+
62+
test:
63+
uses: ./.github/workflows/reusable-ruby-test.yml
64+
with:
65+
target-branch: ${{ inputs.target-branch }}
66+
cache-version: ${{ inputs.cache-version }}
67+
enable-status-reporting: false # We handle reporting in the main report job
68+
status-context: ${{ inputs.status-context }}
69+
secrets:
70+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
71+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
72+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
73+
74+
examples:
75+
uses: ./.github/workflows/reusable-examples.yml
76+
with:
77+
target-branch: ${{ inputs.target-branch }}
78+
cache-version: ${{ inputs.cache-version }}
79+
80+
integration:
81+
uses: ./.github/workflows/reusable-integration-test.yml
82+
with:
83+
target-branch: ${{ inputs.target-branch }}
84+
cache-version: ${{ inputs.cache-version }}
85+
enable-status-reporting: false # We handle reporting in the main report job
86+
status-context: 'integration'
87+
target-repo: ${{ inputs.target-repo }}
88+
secrets:
89+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
90+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
91+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
92+
DD_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
93+
DD_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
94+
SLEEP_AFTER_REQUEST: ${{ secrets.SLEEP_AFTER_REQUEST }}
95+
96+
report:
97+
uses: ./.github/workflows/reusable-report.yml
98+
needs:
99+
- test
100+
- examples
101+
- integration
102+
if: always()
103+
with:
104+
test-result: ${{ needs.test.result }}
105+
examples-result: ${{ needs.examples.result }}
106+
integration-result: ${{ needs.integration.result }}
107+
context: ${{ inputs.status-context }}
108+
target-repo: ${{ inputs.target-repo }}
109+
enable-status-reporting: ${{ inputs.enable-status-reporting }}
110+
secrets:
111+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
112+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
ref: ${{ inputs.target-branch || github.ref }}
36+
- name: Set up Ruby
37+
uses: ruby/setup-ruby@v1
38+
with:
39+
ruby-version: "2.7"
40+
bundler-cache: true
41+
cache-version: ${{ inputs.cache-version }}
42+
- name: Check examples
43+
run: ${{ inputs.examples-script }}
44+
shell: bash

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

Lines changed: 58 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,19 @@ 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+
ref: ${{ inputs.target-branch || github.ref }}
5496
- name: Post pending status check
55-
if: github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
97+
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')
5698
uses: DataDog/github-actions/post-status-check@v2
5799
with:
58100
github-token: ${{ steps.get_token.outputs.token }}
59-
repo: datadog-api-spec
101+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
60102
status: pending
61-
context: integration
103+
context: ${{ inputs.status-context || 'integration' }}
62104
- name: Install system zstd
63105
run: |
64106
sudo apt-get -y install zstd
@@ -68,7 +110,7 @@ jobs:
68110
with:
69111
ruby-version: "3.2"
70112
bundler-cache: true
71-
cache-version: ${{ secrets.CACHE_VERSION }}
113+
cache-version: ${{ inputs.cache-version }}
72114
- name: Install deps
73115
run: bundle install
74116
- name: Run integration tests
@@ -84,20 +126,20 @@ jobs:
84126
DD_TEST_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
85127
DD_TRACE_ANALYTICS_ENABLED: "true"
86128
RECORD: "none"
87-
SLEEP_AFTER_REQUEST: "${{ vars.SLEEP_AFTER_REQUEST }}"
129+
SLEEP_AFTER_REQUEST: ${{ secrets.SLEEP_AFTER_REQUEST || vars.SLEEP_AFTER_REQUEST }}
88130
- name: Post failure status check
89-
if: failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
131+
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')
90132
uses: DataDog/github-actions/post-status-check@v2
91133
with:
92134
github-token: ${{ steps.get_token.outputs.token }}
93-
repo: datadog-api-spec
135+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
94136
status: failure
95-
context: integration
137+
context: ${{ inputs.status-context || 'integration' }}
96138
- name: Post success status check
97-
if: "!failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')"
139+
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')"
98140
uses: DataDog/github-actions/post-status-check@v2
99141
with:
100142
github-token: ${{ steps.get_token.outputs.token }}
101-
repo: datadog-api-spec
143+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
102144
status: success
103-
context: integration
145+
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+
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+
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
46+
token: ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }}
47+
- uses: actions/setup-python@v4
48+
with:
49+
python-version: '3.11'
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 }}
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+
- 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)