This repository contains a simple Flask application that demonstrates how to use Dev Containers, Cloud Run, and GitHub Actions.
- Docker installed on your local machine with at least 4GB of free resources.
- A GCP project.
- A GitHub account.
For instructions on how to use a dev container with Visual Studio Code, check out this tutorial.
- Clone this repository to your development machine.
- Select the
.devcontainer/devcontainer.json
file and right click. - 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.
- [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.
- Once ready, you will see a new window pop up with the IDE running in the dev container.
- You can now run the application by opening the terminal in the dev container IDE and type the following command:
python3 main.py
- 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.
- In the
devcontainer.json
file, update theGOOGLE_CLOUD_PROJECT
andCLOUD_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. - Open a new terminal in the dev container IDE and run the following command to authenticate with GCP:
gcloud init
- Click on the link and follow the sign-in prompts. Once finished, copy the verification code and paste it in the terminal.
- [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
- 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 .
- Make sure your image appears in the Artifact Registry.
- Test your app locally by running the following command:
pytest
- 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
, andiam.serviceAccountUser
. - In your GitHub repository settings, add the key to Secrets & Variables → Actions → Repository Secrets. Name it
GCP_KEY
. - Inside the
.github/workflows
directory you will find a yaml file namedcloud-run-deploy.yaml
. This file contains the required instructions to deploy the application every time you push to the main branch. - Push to the main branch and check the Actions tab in your GitHub repository.
- Once the deployment is successful, you can access the application by clicking on the Cloud Run
Service URL
in the GitHub Actions logs.