Skip to content

Latest commit

 

History

History
103 lines (66 loc) · 3.58 KB

README.md

File metadata and controls

103 lines (66 loc) · 3.58 KB

Google Compute Engine - GitHub Actions

An example workflow that uses GitHub Actions to deploy a container to an existing Google Compute Engine (GCE) instance.

This code is intended to be an example. You will likely need to change or update values to match your setup.

Workflow description

For pushes to the main branch, this workflow will:

  1. Download and configure the Google Cloud SDK with the provided credentials.

  2. Build, tag, and push a container image to Google Container Registry.

  3. Deploy the container image to a Google Compute Engine instance. Note that a GCE deployment requires an existing container-optimized VM.

Setup

  1. Create a new Google Cloud Project (or select an existing project) and enable the Container Registry and Compute APIs.

  2. Create a container-optimized GCE VM or use an existing container-optimized VM. Note the VM name and zone.

  3. Create or reuse a GitHub repository for the example workflow:

    1. Create a repository.

    2. Move into the repository directory:

      $ cd <repo>
      
    3. Copy the example into the repository:

      $ cp -r <path_to>/github-actions/example-workflows/gce/ .
      
  4. Create a Google Cloud service account if one does not already exist.

  5. Add the following Cloud IAM roles to your service account:

    • Compute Instance Admin - allows administering GCE VMs

    • Storage Admin - allows pushing to Container Registry

    • Service Account User - run operations as the compute storage account

    Note: These permissions are overly broad to favor a quick start. They do not represent best practices around the Principle of Least Privilege. To properly restrict access, you should create a custom IAM role with the most restrictive permissions.

  6. Create a JSON service account key for the service account.

  7. Add the following secrets to your repository's secrets:

    • GCE_PROJECT: Google Cloud project ID

    • GCE_SA_KEY: the content of the service account JSON file

  8. Update .github/workflows/gce.yml to match the values corresponding to your VM:

    • GCE_INSTANCE - the instance name of the VM

    • GCE_INSTANCE_ZONE - the zone your VM resides

Run the workflow

  1. Add and commit your changes:

    $ git add .
    $ git commit -m "Set up GitHub workflow"
    
  2. Push to the main branch:

    $ git push -u origin main
    
  3. View the GitHub Actions Workflow by selecting the Actions tab at the top of your repository on GitHub. Then click on the Build and Deploy to Google Compute Engine element to see the details.