An Interest In:
Web News this Week
- April 3, 2024
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
Deploying to Google Cloud Run with Github Actions: A Step-by-Step Guide
What is Google Cloud Run?
Google Cloud Run is a serverless container platform that enables developers to run applications in a fully managed environment. It allows you to deploy stateless containers on a pay-as-you-go basis and auto-scales your application based on incoming traffic.
What is Github Actions?
GitHub Actions is a powerful workflow automation tool that allows developers to automate their development workflows. It integrates well with Google Cloud Run, making it easy to deploy applications from GitHub to Cloud Run.
In this article, I will be deploying a containerized web application to Google Cloud Run using GitHub Actions.
NOTE: If you want to use Gitlab CI/CD instead of GitHub Actions, see my other article here.
Lets continue.
Prerequisites
Before we dive into the tutorial, make sure you have the following:
- A Google Cloud Platform account
- A GitHub account
- Docker installed on your local machine
Step 1: Set up your project on Google Cloud
Before we can deploy our application to Google Cloud Run, we need to create a new project on Google Cloud Platform and enable the Cloud Run API. Heres how to do it:
- Go to the Google Cloud Console.
- Click on the project dropdown menu and select New Project.
- Give your project a name and click Create.
- Once your project is created, click on the Activate Cloud Shell button on the top right corner of the page.
- Run the following command to enable the Cloud Run API:
gcloud services enable run.googleapis.com
An alternative way to enable Cloud Run API
- Go to the Google Cloud Console and select your project.
- In the left navigation menu, click on APIs & Services and then Dashboard.
- Click on the + ENABLE APIS AND SERVICES button.
- Search for Cloud Run API and click on it.
- Click the Enable button.
Step 2: Create a Dockerfile
Next, we need to create a Dockerfile for our application. This file will contain instructions on how to build a container image for our application.
Heres an example Dockerfile for a Node.js application:
\# Use the official Node.js image FROM node:14-alpine \# Set the working directory WORKDIR /app \# Copy the package.json and package-lock.json files COPY package\*.json ./ \# Install the dependencies RUN npm install --production \# Copy the rest of the application code COPY . . \# Expose port 8080 EXPOSE 8080 \# Start the application CMD \["npm", "start"\]
Save this file in the root directory of your project.
Step 3: Build and test the container locally
Before deploying our container to Google Cloud Run, lets build and test it locally. Run the following command to build the container image:
docker build -t <your-image-name> .
Replace <your-image-name>
with a name for your container image. Once the build is complete, run the container with the following command:
docker run -p 8080:8080 <your-image-name>
This will start the container and map port 8080 on your local machine to port 8080 inside the container. Open your web browser and go to http://localhost:8080
to test your application.
Step 4: Set up GitHub Actions
GitHub Actions is a powerful tool that allows you to automate your software development workflows. In this step, we will be creating a GitHub Actions workflow to build and deploy our container to Google Cloud Run.
- In your GitHub repository, click on the Actions tab.
- Click on the Set up a workflow yourself button.
- Replace the contents of the file with the following code:
name: "Deploy to Google Cloud Run" on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up Google Cloud SDK uses: google-github-actions/setup-gcloud@master with: project\_id: <your-project-id> service\_account\_key: ${{ secrets.GCP\_SA\_KEY }} export\_default\_credentials: true - name: Configure docker for GCP run: gcloud auth configure-docker - name: Build and push Docker image uses: docker/build-push-action@v2 with: context: . push: true tags: gcr.io/<your-project-id>/<your-image-name>:latest build-args: | HTTP\_PORT=8080 - name: Deploy to Cloud Run uses: google-github-actions/deploy-cloudrun@main with: image: gcr.io/<your-project-id>/<your-image-name>:latest service: <your-service-name> region: <your-region> platform: managed allow-unauthenticated: true env\_vars: | FOO=bar ZIP=zap
Replace <your-project-id>
, <your-image-name>
, <your-service-name>
, and <your-region>
with your own values.
You can see more here on how to use the google cloud run github actions.
4. Click on the Start commit button and commit the changes to the repository.
Step 5: Deploy to Google Cloud Run
Once the GitHub Actions workflow completes successfully, your container should be deployed to Google Cloud Run. To verify that your application is running, go to the Google Cloud Console, select your project, and click on Cloud Run in the sidebar. You should see your service listed there.
Click on the service to view its details, including the URL for your application. Open this URL in your web browser to test your deployed application.
Congratulations! You have successfully deployed a containerized web application to Google Cloud Run using GitHub Actions.
If you liked this article, please leave a clap or even a comment and dont forget to follow me to get updated when I publish another one. Thanks!
Original Link: https://dev.to/oluwafemiakind1/deploying-to-google-cloud-run-with-github-actions-a-step-by-step-guide-53nf
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To