Skip to content

Commit 815cc38

Browse files
committed
Deploy from pipeline
1 parent 1384133 commit 815cc38

File tree

3 files changed

+170
-17
lines changed

3 files changed

+170
-17
lines changed

.github/workflows/pipeline.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Test and Deploy
2+
on: push
3+
jobs:
4+
test:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v4
8+
- uses: astral-sh/setup-uv@v5
9+
- name: install dependencies
10+
run: |
11+
uv sync
12+
- name: run tests
13+
env:
14+
OPEN_AI_KEY: ${{ secrets.OPEN_AI_KEY }}
15+
RUN_SLOW_TESTS: "true"
16+
run: |
17+
uv run -m unittest
18+
build:
19+
runs-on: ubuntu-latest
20+
needs: [ test ]
21+
permissions:
22+
contents: "read"
23+
id-token: "write"
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: google-github-actions/auth@v2
27+
with:
28+
workload_identity_provider: "${{ vars.GCP_WORKLOAD_IDENTITY_POOL_ID }}"
29+
service_account: "${{ vars.GCP_SERVICE_ACCOUNT }}"
30+
- uses: google-github-actions/setup-gcloud@v2
31+
- uses: astral-sh/setup-uv@v5
32+
- name: build
33+
run: |
34+
uv pip compile pyproject.toml -o requirements.txt
35+
gcloud builds submit --tag gcr.io/${{ vars.GCP_PROJECT_ID }}/repository-discovery/app:${{ github.sha }}
36+
deploy:
37+
runs-on: ubuntu-latest
38+
needs: [ build ]
39+
permissions:
40+
contents: "read"
41+
id-token: "write"
42+
steps:
43+
- uses: actions/checkout@v4
44+
- uses: google-github-actions/auth@v2
45+
with:
46+
workload_identity_provider: "${{ vars.GCP_WORKLOAD_IDENTITY_POOL_ID }}"
47+
service_account: "${{ vars.GCP_SERVICE_ACCOUNT }}"
48+
- uses: google-github-actions/setup-gcloud@v2
49+
- name: Deploy
50+
uses: google-github-actions/deploy-cloudrun@v2
51+
with:
52+
service: repository-discovery-app
53+
image: gcr.io/${{ vars.GCP_PROJECT_ID }}/repository-discovery/app:${{ github.sha }}
54+
region: us-central1
55+
flags: --allow-unauthenticated --platform managed
56+
secrets: |
57+
GITHUB_TOKEN=GITHUB_TOKEN:latest
58+
OPEN_AI_KEY=OPEN_AI_KEY:latest

.github/workflows/test.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

DEPLOYMENT.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Google Cloud deployment
2+
3+
1. Set the GCP project ID as an environment variable.
4+
5+
```shell
6+
export PROJECT_ID={google project id}
7+
export PROJECT_NUMBER={google project number}
8+
export OPEN_AI_KEY={api key}
9+
export GITHUB_TOKEN={github token}
10+
```
11+
12+
1. Create a service account for the pipeline.
13+
14+
```shell
15+
gcloud auth login
16+
gcloud config set project ${PROJECT_ID}
17+
gcloud auth application-default login
18+
gcloud services enable \
19+
iamcredentials.googleapis.com \
20+
run.googleapis.com \
21+
cloudbuild.googleapis.com \
22+
artifactregistry.googleapis.com \
23+
cloudresourcemanager.googleapis.com \
24+
compute.googleapis.com \
25+
secretmanager.googleapis.com \
26+
--project "${PROJECT_ID}"
27+
gcloud iam service-accounts create github-service-account --project "${PROJECT_ID}"
28+
```
29+
30+
1. Create a workload identity pool.
31+
32+
```shell
33+
gcloud iam workload-identity-pools create github-pool \
34+
--project="${PROJECT_ID}" \
35+
--location="global" \
36+
--display-name=github-pool
37+
gcloud iam workload-identity-pools describe github-pool \
38+
--project="${PROJECT_ID}" \
39+
--location="global" \
40+
--format="value(name)"
41+
```
42+
43+
1. Set the workload identity pool ID from the output of the last command.
44+
45+
```shell
46+
export WORKLOAD_IDENTITY_POOL_ID={previous command output}
47+
```
48+
49+
1. Create a workload identity pool provider.
50+
51+
```shell
52+
gcloud iam workload-identity-pools providers create-oidc github-provider \
53+
--project="${PROJECT_ID}" \
54+
--location="global" \
55+
--workload-identity-pool=github-pool \
56+
--display-name=github-provider \
57+
--attribute-mapping="google.subject=assertion.sub,attribute.actor=assertion.actor,attribute.repository=assertion.repository" \
58+
--attribute-condition="assertion.repository_owner=='initialcapacity' && assertion.ref=='refs/heads/main'" \
59+
--issuer-uri="https://token.actions.githubusercontent.com"
60+
gcloud iam service-accounts add-iam-policy-binding "github-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
61+
--project="${PROJECT_ID}" \
62+
--role="roles/iam.workloadIdentityUser" \
63+
--member="principalSet://iam.googleapis.com/${WORKLOAD_IDENTITY_POOL_ID}/attribute.repository/initialcapacity/ai-weekend-agents"
64+
gcloud iam workload-identity-pools providers describe github-provider \
65+
--project="${PROJECT_ID}" \
66+
--location="global" \
67+
--workload-identity-pool=github-pool \
68+
--format="value(name)"
69+
```
70+
71+
1. Give api permissions to the service account.
72+
73+
```shell
74+
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:github-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
75+
--role="roles/artifactregistry.admin"
76+
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:github-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
77+
--role="roles/run.admin"
78+
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:github-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
79+
--role="roles/viewer"
80+
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:github-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
81+
--role="roles/iam.serviceAccountUser"
82+
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:github-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
83+
--role="roles/cloudbuild.builds.viewer"
84+
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:github-service-account@${PROJECT_ID}.iam.gserviceaccount.com" \
85+
--role="roles/cloudbuild.builds.builder"
86+
gcloud projects get-iam-policy $PROJECT_ID --flatten="bindings[].members" \
87+
--format='table(bindings.role)' \
88+
--filter="bindings.members:github-service-account@${PROJECT_ID}.iam.gserviceaccount.com"
89+
```
90+
91+
1. Create secrets.
92+
```shell
93+
echo -n "$OPEN_AI_KEY" | gcloud secrets create OPEN_AI_KEY --data-file=-
94+
echo -n "$GITHUB_TOKEN" | gcloud secrets create GITHUB_TOKEN --data-file=-
95+
```
96+
97+
1. Allow the default service account to access secrets.
98+
```shell
99+
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
100+
--member="serviceAccount:${PROJECT_NUMBER}[email protected]" \
101+
--role='roles/secretmanager.secretAccessor'
102+
```
103+
104+
## Variables
105+
106+
Repository variables for pipeline
107+
108+
```shell
109+
GCP_PROJECT_ID=${PROJECT_ID}
110+
GCP_WORKLOAD_IDENTITY_POOL_ID=${WORKLOAD_IDENTITY_POOL_ID}/providers/github-provider
111+
GCP_SERVICE_ACCOUNT=github-service-account@${PROJECT_ID}.iam.gserviceaccount.com
112+
```

0 commit comments

Comments
 (0)