This tutorial shows you how to use self-hosted GitHub runners on worker pools to execute the workflows defined in your GitHub repository, and scale your worker pool with Cloud Run External Metrics Autoscaling (CREMA).
About self-hosted GitHub runners
In a GitHub Actions workflow, runners are the machines that execute jobs. For example, a runner can clone your repository locally, install testing software, and then run commands that evaluate your code.
You can use self-hosted runners to run GitHub Actions on Cloud Run worker pool instances. This tutorial shows you how to automatically scale a pool of runners based on the number of running and unscheduled jobs.
Objectives
In this tutorial, you will:
Add self-hosted GitHub runners to support a GitHub repository, and create Secret Manager secrets to securely store tokens and secrets.
Deploy a Cloud Run worker pool to Cloud Run and use the worker pool to accept jobs from GitHub actions.
Deploy the autoscaler CREMA service to scale your worker pool.
Test your CREMA service by verifying logs.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage, use the pricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run, Secret Manager, Parameter Manager, Artifact Registry, and Cloud Build APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. - Install and initialize the gcloud CLI.
- Update components:
gcloud components update
- Set the following configuration variables for CREMA used in this tutorial:
Replace PROJECT_ID with the ID of your Google Cloud project.PROJECT_ID=PROJECT_ID CREMA_SERVICE_ACCOUNT_NAME=crema-service-account@$PROJECT_ID.iam.gserviceaccount.com CREMA_REPO_NAME=crema AR_REGION=us-central1
- You incur charges for your Cloud Run scaling service based on how often you trigger scaling. For more information, estimate costs with the pricing calculator.
Required roles
To get the permissions that you need to complete the tutorial, ask your administrator to grant you the following IAM roles on your project:
- Artifact Registry Repository Administrator (
roles/artifactregistry.repoAdmin) - Cloud Build Editor (
roles/cloudbuild.builds.editor) - Cloud Run Admin (
roles/run.admin) - Create Service Accounts (
roles/iam.serviceAccountCreator) - Secret Manager Admin (
roles/secretmanager.admin) - Service Account User (
roles/iam.serviceAccountUser) - Service Usage Consumer (
roles/serviceusage.serviceUsageConsumer) - Storage Admin (
roles/storage.admin) - Parameter Manager Admin (
roles/parametermanager.admin)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
You need permission to edit the settings on a GitHub repository to configure the self-hosted runners. The repository can be user-owned, or an organisation owned repository.
GitHub recommends using self-hosted runners with private repositories only.
Create a custom service account
This tutorial uses a custom service account with the minimum permissions required to use the provisioned resources. To set up the service account, do the following:
gcloud iam service-accounts create crema-service-account \ --display-name="CREMA Service Account" Add self-hosted GitHub runners
To add self-hosted GitHub runners, follow the instructions in adding self-hosted runners in the GitHub documentation.
Identify the GitHub repository
In this tutorial, the GITHUB_REPO variable represents the repository name. This is the part of the name that you find after the domain name for both personal user repositories and organization repositories. For example:
- If your domain URL is
https://github.com/myuser/myrepo, the GITHUB_REPO ismyuser/myrepo. - If your domain URL is
https://github.com/mycompany/ourrepo, the GITHUB_REPO ismycompany/ourrepo.
Create access token
Create a GitHub access token to dynamically add and remove runners by interacting with your selected repository. To create an access token on GitHub and save it in the Secret Manager, follow these steps:
- Ensure you are logged into your GitHub account.
- Navigate to GitHub's Settings > Developer Settings > Personal Access Tokens > Tokens (classic) page.
- Click Generate new token, and select Generate new token (classic).
- For the token scope, select the repo checkbox.
- Click Generate token.
- Copy the generated token.
For more information on access tokens see Authentication requirements in GitHub documentation.
Create a secret for your access token using Secret Manager
Take the secret token you created in the previous step, and store it in Secret Manager. To set access permissions, follow these steps:
Create the secret in Secret Manager:
echo -n "GITHUB_TOKEN" | gcloud secrets create github_runner_token --data-file=-Replace the GITHUB_TOKEN with the value you copied from GitHub.
Grant the
roles/secretmanager.secretAccessorto your custom service account to access your newly created secret:gcloud secrets add-iam-policy-binding github_runner_token \ --member "serviceAccount:$CREMA_SERVICE_ACCOUNT_NAME" \ --role "roles/secretmanager.secretAccessor"
Deploy a worker pool
Create a Cloud Run worker pool to process GitHub actions. This pool will use an image based on the GitHub-created actions/runner image. To deploy a worker pool, follow these steps:
Clone the sample repository to your local machine to retrieve the code sample for use:
git clone https://github.com/GoogleCloudPlatform/cloud-run-samplesChange to the directory that contains the Cloud Run sample code:
cd cloud-run-samples/github-runner/worker-pool-containerDeploy the worker pool:
gcloud beta run worker-pools deploy WORKER_POOL_NAME \ --region us-central1 \ --source . \ --scaling 1 \ --set-env-vars GITHUB_REPO=GITHUB_REPO \ --set-secrets GITHUB_TOKEN=github_runner_token:latest \ --service-account $CREMA_SERVICE_ACCOUNT_NAME \ --memory 2Gi \ --cpu 4Replace the following:
- WORKER_POOL_NAME: the name of the worker pool
- WORKER_POOL_LOCATION: the region of the worker pool
- GITHUB_REPO: the GitHub repo name
If this is the first time using Cloud Run source deploys in this project, Cloud Run prompts you to create a default Artifact Registry repository.
Understand the code sample
The worker pool is configured with a Dockerfile that is based on the GitHub-created actions/runner image:
This helper script runs when the container is started, registering itself to the configured repository as an ephemeral instance, using a token you create.
Use the worker pool to accept jobs from GitHub actions
Your worker pool instance is ready to accept jobs from GitHub actions.
If your repo doesn't already have any GitHub actions, follow the instructions in the quickstart for creating your first workflow.
If your repo has GitHub actions, verify that you completed the setup of your self-hosted runner by invoking a GitHub action on your repository.
If your GitHub action doesn't use self-hosted runners, change your GitHub action's job from runs-on value to self-hosted.
Once you can configured an action to use the self-hosted runners, run the action.
Confirm the action completes successfully in the GitHub interface.
Deploy the autoscaler CREMA service
You deployed one worker in your original pool, which allows processing of one action at a time. Depending on your Continuous Integration (CI) usage, you might need to scale your pool to handle an influx of work to be done.
Once you deploy the worker pool with an active GitHub runner, configure the CREMA autoscaler to provision worker instances based on the job status in the actions queue.
This implementation listens for a workflow_job event. When you create a workflow job, it scales up the worker pool, and once the job is completed, scales it down again. It won't scale the pool beyond the maximum number of instances you configure, and scales to zero when all running jobs have completed.
You can adapt CREMA based on your workloads.
Configure the autoscaler
This tutorial uses the Parameter Manager to store the YAML configuration file for CREMA.
Create a parameter in the Parameter Manager to store parameter versions for CREMA:
PARAMETER_ID=crema-config PARAMETER_REGION=global gcloud parametermanager parameters create $PARAMETER_ID --location=$PARAMETER_REGION --parameter-format=YAMLCreate a YAML file,
my-crema-config.yamlin the parent directory to define the autoscaler configuration:apiVersion: crema/v1 kind: CremaConfig metadata: name: gh-demo spec: pollingInterval: 10 triggerAuthentications: - metadata: name: github-trigger-auth spec: gcpSecretManager: secrets: - parameter: personalAccessToken id: github_runner_token version: latest scaledObjects: - spec: scaleTargetRef: name: projects/PROJECT_ID/locations/us-central1/workerpools/WORKER_POOL_NAME triggers: - type: github-runner name: GITHUB_RUNNER metadata: owner: REPOSITORY_OWNER runnerScope: repo repos: REPOSITORY_NAME targetWorkflowQueueLength: 1 authenticationRef: name: github-trigger-auth advanced: horizontalPodAutoscalerConfig: behavior: scaleDown: stabilizationWindowSeconds: 10 policies: - type: Pods value: 100 periodSeconds: 10 scaleUp: stabilizationWindowSeconds: 10 policies: - type: Pods value: 2 periodSeconds: 10Replace the following:
- PROJECT_ID: the Google Cloud project ID
- WORKER_POOL_NAME: the name of the worker pool you deployed
- GITHUB_RUNNER: the name of the GitHub runner you configured
- REPOSITORY_OWNER: the owner of the GitHub repository
- REPOSITORY_NAME: the GitHub repository name
Upload your local YAML file as a new parameter version:
LOCAL_YAML_CONFIG_FILE=my-crema-config.yaml PARAMETER_VERSION=1 gcloud parametermanager parameters versions create $PARAMETER_VERSION \ --location=$PARAMETER_REGION \ --parameter=$PARAMETER_ID \ --payload-data-from-file=$LOCAL_YAML_CONFIG_FILE
Grant additional permissions to your custom service account
To scale the worker pool you specified in your YAML configuration, grant the following permissions on the custom service account:
Grant your CREMA service account permission to read from the Parameter Manager:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="serviceAccount:$CREMA_SERVICE_ACCOUNT_NAME" \ --role="roles/parametermanager.parameterViewer"Grant your CREMA service account the
roles/run.developerrole on the worker pool:WORKER_POOL_NAME=WORKER_POOL_NAME WORKER_POOL_REGION=us-central1 gcloud beta run worker-pools add-iam-policy-binding $WORKER_POOL_NAME \ --region=$WORKER_POOL_REGION \ --member="serviceAccount:$CREMA_SERVICE_ACCOUNT_NAME" \ --role="roles/run.developer"Replace WORKER_POOL_NAME with the name of the worker pool.
Grant your CREMA service account permission to write metrics:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="serviceAccount:$CREMA_SERVICE_ACCOUNT_NAME" \ --role="roles/monitoring.metricWriter"Grant your CREMA service account the service account user role:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="serviceAccount:$CREMA_SERVICE_ACCOUNT_NAME" \ --role="roles/iam.serviceAccountUser"
Deploy the service to scale your workloads
To deploy the service to scale your worker pool, run the following command with a prebuilt container image:
SERVICE_NAME=my-crema-service SERVICE_REGION=us-central1 CREMA_CONFIG_PARAM_VERSION=projects/$PROJECT_ID/locations/$PARAMETER_REGION/parameters/$PARAMETER_ID/versions/$PARAMETER_VERSION IMAGE=us-central1-docker.pkg.dev/cloud-run-oss-images/crema-v1/autoscaler:1.0 gcloud beta run deploy $SERVICE_NAME \ --image=${IMAGE} \ --region=${SERVICE_REGION} \ --service-account="${CREMA_SERVICE_ACCOUNT_NAME}" \ --no-allow-unauthenticated \ --no-cpu-throttling \ --base-image=us-central1-docker.pkg.dev/serverless-runtimes/google-22/runtimes/java21 \ --labels=created-by=crema \ --set-env-vars="CREMA_CONFIG=${CREMA_CONFIG_PARAM_VERSION},OUTPUT_SCALER_METRICS=True" Create webhook secret value
To create a secret value to access the GitHub webhook, do the following:
Create a Secret Manager secret to manage access to your GitHub webhook.
echo -n "WEBHOOK_SECRET" | gcloud secrets create github_webhook_secret --data-file=-Replace WEBHOOK_SECRET with an arbitrary string value.
Grant access to the secret to the autoscaler service account:
gcloud secrets add-iam-policy-binding github_webhook_secret \ --member "serviceAccount:$CREMA_SERVICE_ACCOUNT_NAME" \ --role "roles/secretmanager.secretAccessor"
Create GitHub webhook
To create the GitHub webhook, follow these steps:
- Ensure you are logged into your GitHub account.
- Navigate to your GitHub repository.
- Click Settings.
- Under Code and automation, click Webhooks.
- Click Add webhook.
Enter the following:
- In Payload URL, enter the URL of the Cloud Run CREMA service you deployed,
my-crema-service. - For Content type, select application/json.
- For Secret, enter the WEBHOOK_SECRET value you created previously.
- For SSL verification, select Enable SSL verification.
- For Which events would you like to trigger this webhook?, select Let me select individual events.
- In the event selection, select Workflow jobs. Unselect any other option.
- Click Add webhook.
- In Payload URL, enter the URL of the Cloud Run CREMA service you deployed,
Test your CREMA service
To verify your autoscaling service is working correctly, check the Logs tab of the Cloud Run service.
You should see the following logs in your service's logs each time metrics are refreshed:
Each log message is labeled with the component that emitted it.
[INFO] [METRIC-PROVIDER] Starting metric collection cycle [INFO] [METRIC-PROVIDER] Successfully fetched scaled object metrics ... [INFO] [METRIC-PROVIDER] Sending scale request ... [INFO] [SCALER] Received ScaleRequest ... [INFO] [SCALER] Current instances ... [INFO] [SCALER] Recommended instances ... Clean up
To avoid additional charges to your Google Cloud account, delete all the resources you deployed with this tutorial.
Delete the project
If you created a new project for this tutorial, delete the project. If you used an existing project and need to keep it without the changes you added in this tutorial, delete resources that you created for the tutorial.
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete tutorial resources
Delete the Cloud Run service you deployed in this tutorial. Cloud Run services don't incur costs until they receive requests.
To delete your Cloud Run service, run the following command:
gcloud run services delete SERVICE-NAME
Replace SERVICE-NAME with the name of your service.
You can also delete Cloud Run services from the Google Cloud console.
Remove the
gclouddefault region configuration you added during tutorial setup:gcloud config unset run/regionRemove the project configuration:
gcloud config unset projectDelete other Google Cloud resources created in this tutorial:
What's next
- Learn more about Cloud Run worker pools.
- Explore other Cloud Run demos, tutorials, and samples.
- Deploy your CREMA service using a custom container image you build from the source code with Cloud Build.