Skip to content

Commit b140b88

Browse files
chore(ci): add WIF for integration tests (GoogleCloudPlatform#54)
1 parent 1c2e965 commit b140b88

File tree

4 files changed

+154
-43
lines changed

4 files changed

+154
-43
lines changed

.github/labels.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@
6969
- name: 'autorelease: tagged'
7070
color: ededed
7171
description: Release please has completed a release for this.
72+
73+
- name: 'tests: run'
74+
color: 3ded97
75+
description: Label to trigger Github Action tests.

.github/trusted-contribution.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
annotations:
16+
- type: label
17+
text: "tests: run"

.github/workflows/lint.yaml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
# limitations under the License.
1414

1515
name: lint
16-
on: [pull_request]
16+
on:
17+
pull_request:
18+
pull_request_target:
19+
types: [labeled]
1720

1821
jobs:
19-
build:
20-
name: lint
22+
lint:
23+
if: "${{ github.event.action != 'labeled' || github.event.label.name == 'tests: run' }}"
24+
name: run lint
2125
runs-on: ubuntu-latest
2226
steps:
2327
- name: Setup Go
@@ -28,7 +32,26 @@ jobs:
2832
run: go install golang.org/x/tools/cmd/goimports@latest
2933
- name: Checkout code
3034
uses: actions/checkout@v3
35+
with:
36+
ref: ${{ github.event.pull_request.head.sha }}
37+
repository: ${{ github.event.pull_request.head.repo.full_name }}
3138
- run: goimports -w .
3239
- run: go mod tidy
3340
- name: Verify no changes from goimports and go mod tidy. If you're reading this and the check has failed, run `goimports -w . && go mod tidy`.
3441
run: git diff --exit-code
42+
- name: Remove PR Label
43+
if: "${{ github.event.action == 'labeled' && github.event.label.name == 'tests: run' }}"
44+
uses: actions/github-script@v6
45+
with:
46+
github-token: ${{ secrets.GITHUB_TOKEN }}
47+
script: |
48+
try {
49+
await github.rest.issues.removeLabel({
50+
name: 'tests: run',
51+
owner: context.repo.owner,
52+
repo: context.repo.repo,
53+
issue_number: context.payload.pull_request.number
54+
});
55+
} catch (e) {
56+
console.log('Failed to remove label. Another job may have already removed it!');
57+
}

.github/workflows/tests.yaml

Lines changed: 107 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,113 @@ on:
1818
push:
1919
branches:
2020
- main
21+
pull_request_target:
22+
types: [labeled]
23+
schedule:
24+
- cron: '0 2 * * *'
2125

2226
jobs:
2327
integration:
24-
runs-on: [self-hosted, linux, x64]
25-
name: "integration tests (linux)"
26-
steps:
27-
- name: Setup Go
28-
uses: actions/setup-go@v3
29-
with:
30-
go-version: "1.18"
31-
- name: Checkout code
32-
uses: actions/checkout@v3
33-
- name: Get Secrets
34-
id: 'secrets'
35-
uses: 'google-github-actions/get-secretmanager-secrets@v0'
36-
with:
37-
secrets: |-
38-
ALLOYDB_CONN_NAME:alloydb-connector-testing/ALLOYDB_CONN_NAME
39-
ALLOYDB_CLUSTER_PASS:alloydb-connector-testing/ALLOYDB_CLUSTER_PASS
40-
- name: Run tests
41-
env:
42-
ALLOYDB_DB: 'postgres'
43-
ALLOYDB_USER: 'postgres'
44-
ALLOYDB_PASS: '${{ steps.secrets.outputs.ALLOYDB_CLUSTER_PASS }}'
45-
ALLOYDB_CONNECTION_NAME: '${{ steps.secrets.outputs.ALLOYDB_CONN_NAME }}'
46-
run: |
47-
go test -v -race -cover ./tests
48-
build:
49-
name: "unit tests"
50-
runs-on: ${{ matrix.os }}
51-
strategy:
52-
matrix:
53-
os: [macos-latest, windows-latest, ubuntu-latest]
54-
steps:
55-
- name: Setup Go
56-
uses: actions/setup-go@v3
57-
with:
58-
go-version: "1.18"
59-
- name: Checkout code
60-
uses: actions/checkout@v3
61-
- name: Run tests
62-
run: |
63-
go test -v -race -cover -short ./...
28+
# run job on proper workflow event triggers (skip job for pull_request event from forks and only run pull_request_target for "tests: run" label)
29+
if: "${{ (github.event.action != 'labeled' && github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name) || github.event.label.name == 'tests: run' }}"
30+
runs-on: [self-hosted, linux, x64]
31+
name: "integration tests (linux)"
32+
permissions:
33+
contents: 'read'
34+
id-token: 'write'
35+
steps:
36+
- name: Remove PR label
37+
if: "${{ github.event.action == 'labeled' && github.event.label.name == 'tests: run' }}"
38+
uses: actions/github-script@v6
39+
with:
40+
github-token: ${{ secrets.GITHUB_TOKEN }}
41+
script: |
42+
try {
43+
await github.rest.issues.removeLabel({
44+
name: 'tests: run',
45+
owner: context.repo.owner,
46+
repo: context.repo.repo,
47+
issue_number: context.payload.pull_request.number
48+
});
49+
} catch (e) {
50+
console.log('Failed to remove label. Another job may have already removed it!');
51+
}
52+
53+
- name: Setup Go
54+
uses: actions/setup-go@v3
55+
with:
56+
go-version: "1.18"
57+
58+
- name: Checkout code
59+
uses: actions/checkout@v3
60+
with:
61+
ref: ${{ github.event.pull_request.head.sha }}
62+
repository: ${{ github.event.pull_request.head.repo.full_name }}
63+
64+
- id: 'auth'
65+
name: 'Authenticate to Google Cloud'
66+
uses: 'google-github-actions/auth@v0.8.0'
67+
with:
68+
workload_identity_provider: ${{ secrets.PROVIDER_NAME }}
69+
service_account: ${{ secrets.SERVICE_ACCOUNT }}
70+
71+
- name: 'Set up Cloud SDK'
72+
uses: 'google-github-actions/setup-gcloud@v0.6.0'
73+
74+
- name: Get Secrets
75+
id: 'secrets'
76+
uses: 'google-github-actions/get-secretmanager-secrets@v0.5.0'
77+
with:
78+
secrets: |-
79+
ALLOYDB_CONN_NAME:${{ secrets.GOOGLE_CLOUD_PROJECT }}/ALLOYDB_CONN_NAME
80+
ALLOYDB_CLUSTER_PASS:${{ secrets.GOOGLE_CLOUD_PROJECT }}/ALLOYDB_CLUSTER_PASS
81+
82+
- name: Run tests
83+
env:
84+
ALLOYDB_DB: 'postgres'
85+
ALLOYDB_USER: 'postgres'
86+
ALLOYDB_PASS: '${{ steps.secrets.outputs.ALLOYDB_CLUSTER_PASS }}'
87+
ALLOYDB_CONNECTION_NAME: '${{ steps.secrets.outputs.ALLOYDB_CONN_NAME }}'
88+
run: |
89+
go test -v -race -cover ./tests
90+
91+
unit:
92+
if: "${{ github.event.action != 'labeled' || github.event.label.name == 'tests: run' }}"
93+
name: "unit tests"
94+
runs-on: ${{ matrix.os }}
95+
strategy:
96+
matrix:
97+
os: [macos-latest, windows-latest, ubuntu-latest]
98+
fail-fast: false
99+
steps:
100+
- name: Remove PR label
101+
if: "${{ github.event.action == 'labeled' && github.event.label.name == 'tests: run' }}"
102+
uses: actions/github-script@v6
103+
with:
104+
github-token: ${{ secrets.GITHUB_TOKEN }}
105+
script: |
106+
try {
107+
await github.rest.issues.removeLabel({
108+
name: 'tests: run',
109+
owner: context.repo.owner,
110+
repo: context.repo.repo,
111+
issue_number: context.payload.pull_request.number
112+
});
113+
} catch (e) {
114+
console.log('Failed to remove label. Another job may have already removed it!');
115+
}
116+
117+
- name: Setup Go
118+
uses: actions/setup-go@v3
119+
with:
120+
go-version: "1.18"
121+
122+
- name: Checkout code
123+
uses: actions/checkout@v3
124+
with:
125+
ref: ${{ github.event.pull_request.head.sha }}
126+
repository: ${{ github.event.pull_request.head.repo.full_name }}
127+
128+
- name: Run tests
129+
run: |
130+
go test -v -race -cover -short ./...

0 commit comments

Comments
 (0)