Skip to content

Using Dev Containers, Cloud Run, and GitHub Actions, learn how to setup your projects for a more efficient, consistent, and automated software development process.

License

Notifications You must be signed in to change notification settings

inteagent/devcontainter-cloudrun-actions

Repository files navigation

Dev Containers + Cloud Run + GitHub Actions

Overview

This repository contains a simple Flask application that demonstrates how to use Dev Containers, Cloud Run, and GitHub Actions.

Pre-requisites

  1. Docker installed on your local machine with at least 4GB of free resources.
  2. A GCP project.
  3. A GitHub account.

Run the application locally on a dev container using IntelliJ IDEA

For instructions on how to use a dev container with Visual Studio Code, check out this tutorial.

  1. Clone this repository to your development machine.
  2. Select the .devcontainer/devcontainer.json file and right click.
  3. Select Dev Containers → Create Dev Container and Mount Sources. To restart an existing container, you can select Dev Containers → Show Dev Containers and choose a container to run.
  4. [Skip this step if you have already built the container] After creating a dev container, select a JetBrains IDE from the list (you might only see one) and click Continue. The selected IDE will be downloaded and the client will connect to it. Note that building the dev container and installing the IDE can take a little while, but you only need to do this once.
  5. Once ready, you will see a new window pop up with the IDE running in the dev container.
  6. You can now run the application by opening the terminal in the dev container IDE and type the following command:
python3 main.py
  1. Open a browser window and navigate to the url shown in the terminal logs to see the application running locally. Alternatively, you can use the dropdown menu labeled Your application is listening on port 8080: right on the terminal window, and select Forward Port and Open in Browser to open the application in a browser window. If you see the message "Hello, World!" then the application is running successfully. If you see an error, make sure you are forwarding the port.

Set Up Your Cloud Run Service

  1. In the devcontainer.json file, update the GOOGLE_CLOUD_PROJECT and CLOUD_RUN_SERVICE environment variables with your GCP Project ID and the name of the Cloud Run service. You don't need to set up the service in advance, just pick a unique name. Rebuild the container.
  2. Open a new terminal in the dev container IDE and run the following command to authenticate with GCP:
gcloud init
  1. Click on the link and follow the sign-in prompts. Once finished, copy the verification code and paste it in the terminal.
  2. [Skip this step if you have already set up a repository in Artifact Registry] Enable the Artifact Registry API on your GCP project and create a repository:
gcloud artifacts repositories create YOUR-GCR-ARTIFACTS-REPO
  --repository-format docker 
  --project YOUR-PROJECT-ID 
  --location us-central1
  1. Build the container image and push it to Artifact Registry by running the following command:
gcloud builds submit --config=cloudbuild.yaml --project YOUR-PROJECT-ID .
  1. Make sure your image appears in the Artifact Registry.
  2. Test your app locally by running the following command:
pytest

Deploy to Cloud Run Using GitHub Actions

  1. In your GCP dashboard, create a service account and generate a key for it in JSON format. Give the service account the following roles: artifactregistry.writer, run.admin, and iam.serviceAccountUser.
  2. In your GitHub repository settings, add the key to Secrets & Variables → Actions → Repository Secrets. Name it GCP_KEY.
  3. Inside the .github/workflows directory you will find a yaml file named cloud-run-deploy.yaml. This file contains the required instructions to deploy the application every time you push to the main branch.
  4. Push to the main branch and check the Actions tab in your GitHub repository.
  5. Once the deployment is successful, you can access the application by clicking on the Cloud Run Service URL in the GitHub Actions logs.

About

Using Dev Containers, Cloud Run, and GitHub Actions, learn how to setup your projects for a more efficient, consistent, and automated software development process.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published