Create and Deploy Cloud Run Job Script


Cloud Run Job Deployment Script

This repository contains a script to build and deploy a Python application as a Cloud Run Job using Google Cloud Build. The script dynamically generates a cloudbuild.yaml file and submits it to Google Cloud Build.

Prerequisites

Before using the deployment script, ensure you have the following:

  • Google Cloud SDK: Installed and configured.
  • Docker: Installed.
  • Google Cloud Project: Created and configured.
  • Service Account Key: A service account key JSON file with appropriate permissions stored at keys/service-account-key.json.

Files

  • deploy_cloud_run_job.sh: The main script to build, deploy, and run the Cloud Run Job.
  • Dockerfile: The Dockerfile to build the Docker image for your Python application.
  • requirements.txt: List of dependencies for your Python application (if any).
  • cloudbuild.yaml: Dynamically generated by the script; defines the build steps for Cloud Build.

Usage

1. Clone the Repository

Clone this repository to your local machine:

git clone <repository-url>
cd <repository-directory>

2. Prepare Your Environment

Ensure your Python application and Dockerfile are prepared in the current directory. Also, ensure your service account key JSON file is located at keys/service-account-key.json.

3. Make the Script Executable

Make the deployment script executable:

chmod +x deploy_cloud_run_job.sh

4. Run the Script

Run the script with the required arguments:

./deploy_cloud_run_job.sh <PROJECT_NAME> <IMAGE_NAME> <JOB_NAME>

Replace `<PROJECT_NAME>`, `<IMAGE_NAME>`, and `<JOB_NAME>` with your actual Google Cloud project name, desired Docker image name, and Cloud Run Job name.

Example

./deploy_cloud_run_job.sh smart-axis-421517 my-python-job my-cloud-run-job

How It Works

  1. Docker Image Build: The script builds a Docker image using the provided Dockerfile.
  2. Push to Container Registry: The Docker image is pushed to Google Container Registry.
  3. Deploy as Cloud Run Job: The script deploys the Docker image as a Cloud Run Job using the dynamically generated cloudbuild.yaml file.
  4. Execute the Job: Finally, the script executes the Cloud Run Job.

Notes

  • Ensure you have the necessary permissions to deploy and execute jobs in your Google Cloud Project.
  • The region for the Cloud Run Job deployment is set to us-central1 by default. You can change it in the script if needed.

Troubleshooting

  • If the script fails, check the error messages for details.
  • Ensure all prerequisites are met and paths are correctly set.
  • Verify that the service account key has the necessary permissions.

License

This project is licensed under the MIT License. See the LICENSE file for details.