|
| 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