setup-gcloud GitHub Action

repository·main·Indexed 23 days ago

https://github.com/google-github-actions/setup-gcloud

A GitHub Action that installs and configures the Google Cloud SDK (gcloud CLI) within a runner environment. It supports version pinning, installation of additional gcloud components, and integration with google-github-actions/auth for authentication via Workload Identity Federation or Service Account keys.

Tokens
2.3K
Snippets
5
Records
10
Agent score
34%

What's inside setup-gcloud

  1. Authenticate setup-gcloud with Service Account Key JSON

    main

    You can authenticate using a Service Account key stored in a GitHub Secret.

    jobs:
      job_id:
        steps:
        - id: 'auth'
          uses: 'google-github-actions/auth@v2'
          with:
            credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
    
        - name: 'Set up Cloud SDK'
          uses: 'google-github-actions/setup-gcloud@v3'
    
        - name: 'Use gcloud CLI'
          run: 'gcloud info'
  2. Use multiple service accounts in a single workflow

    main

    To switch between multiple service accounts, you must run the google-github-actions/auth action before each setup-gcloud step to update the credentials.

    jobs:
      job_id:
        permissions:
          contents: 'read'
          id-token: 'write'
    
        steps:
          - id: 'auth service account 1'
            uses: 'google-github-actions/auth@v2'
            with:
              workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
              service_account: 'service-account-1@my-project.iam.gserviceaccount.com'
    
          - name: 'Set up Cloud SDK'
            uses: 'google-github-actions/setup-gcloud@v3'
    
          - name: 'Use gcloud CLI'
            run: 'gcloud auth list --filter=status:ACTIVE --format="value(account)"
            # service-account-1@my-project.iam.gserviceaccount.com
    
          - id: 'auth service account 2'
            uses: 'google-github-actions/auth@v2'
            with:
              credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
    
          - name: 'Set up Cloud SDK'
            uses: 'google-github-actions/setup-gcloud@v3'
    
          - name: 'Use gcloud CLI'
            run: 'gcloud auth list --filter=status:ACTIVE --format="value(account)"
            # service-account-2@my-project.iam.gserviceaccount.com
  3. Authenticate setup-gcloud on self-hosted runners on GCP

    main

    If your self-hosted runners are hosted on Google Cloud Platform, they automatically obtain credentials from the service account attached to the runner. In this case, you do not need to run the google-github-actions/auth action.

    jobs:
      job_id:
        steps:
        - name: 'Set up Cloud SDK'
          uses: 'google-github-actions/setup-gcloud@v3'
    
        - name: 'Use gcloud CLI'
          run: 'gcloud info'
  4. Authenticate setup-gcloud with Workload Identity Federation

    main

    Workload Identity Federation is the preferred method for authentication. You must grant the id-token: 'write' permission to your job.

    Note: To authenticate the bq tool using Workload Identity Federation, you must use Cloud SDK version 390.0.0 or later.

    jobs:
      job_id:
        permissions:
          contents: 'read'
          id-token: 'write'
    
        steps:
        - id: 'auth'
          uses: 'google-github-actions/auth@v2'
          with:
            workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
            service_account: 'my-service-account@my-project.iam.gserviceaccount.com'
    
        - name: 'Set up Cloud SDK'
          uses: 'google-github-actions/setup-gcloud@v3'
    
        - name: 'Use gcloud CLI'
          run: 'gcloud info'
  5. Install and configure the Google Cloud SDK

    main

    Use setup-gcloud to install the Google Cloud SDK (including the gcloud binary) in your GitHub Actions environment. This action is typically used in conjunction with google-github-actions/auth to provide credentials for the CLI.

    Prerequisites

    • Credentials: You must provide Google Cloud credentials (via google-github-actions/auth) to execute gcloud commands.
    • Node.js Runtime: This action runs using Node 24. Self-hosted runners must support Node 24 or newer.

    Basic Usage Example

    jobs:
      job_id:
        permissions:
          contents: 'read'
          id-token: 'write'
    
        steps:
        - id: 'auth'
          uses: 'google-github-actions/auth@v2'
          with:
            workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
            service_account: 'my-service-account@my-project.iam.gserviceaccount.com'
    
        - name: 'Set up Cloud SDK'
          uses: 'google-github-actions/setup-gcloud@v3'
          with:
            version: '>= 363.0.0'
    
        - name: 'Use gcloud CLI'
          run: 'gcloud info'
  6. Pinning versions for setup-gcloud

    main

    To prevent accidental breaking changes, you can pin to a specific version. Note that pinning to a specific version means you will not receive automatic security updates or new features.

    • Recommended (Major version): google-github-actions/setup-gcloud@v3
    • Strict (Specific version): google-github-actions/setup-gcloud@v3.0.0

    Note: The action only publishes MAJOR and MAJOR.MINOR.PATCH versions. There is no floating alias for MAJOR.MINOR.

  7. Authenticate the gcloud CLI

    main

    The setup-gcloud action does not perform authentication itself. It expects authentication to be handled by a preceding step, typically google-github-actions/auth.

    • Automatic Detection: The action checks for authentication by looking for the environment variable GOOGLE_GHA_CREDS_PATH (set by the auth action). If present, it uses that credential file to authenticate.
    • Manual Check: If no credential path is found, the action checks if the CLI is already authenticated. If not, it issues a warning suggesting you add the google-github-actions/auth step before this one.
  8. Install additional gcloud components

    main

    You can install specific gcloud components by providing a comma-separated list to the install_components input. The action will split the string and install each component listed.

    Warning: If you set skip_install: true and attempt to install components, the installation might fail if the system-provided gcloud version does not support the requested components. It is recommended to set skip_install: false to allow the action to manage a compatible version of the SDK.

  9. Configure setup-gcloud inputs

    main

    The setup-gcloud action accepts several optional inputs to customize the installation:

    InputTypeDescription
    versionstringVersion or constraint (e.g. "290.0.1" or ">= 197.0.1"). Defaults to latest. Note: Workload Identity Federation requires version 363.0.0 or newer.
    project_idstringThe Google Cloud project ID to use by default. Individual commands can still override this with --project.
    install_componentsstringA comma-separated list of additional gcloud components to install (e.g. 'alpha,cloud-datastore-emulator').
    skip_installbooleanIf true, skips installation and uses the system-supplied version. Note: You cannot install additional components if this is used.
    cachebooleanIf true, transfers downloaded artifacts to the runner's tool cache. Useful for improving performance on self-hosted runners.