
To calculate pi using a Bash script, you can use the bc command, which is a tool that can perform arithmetic operations in the terminal.
Learn how to automate Google Cloud CLI tasks using Bash scripts in your CI/CD pipelines. Includes CLI examples, GitHub Actions integration, and optimization tips.
In today’s fast-paced software development landscape, automation is key to achieving efficient and reliable Continuous Integration and Continuous Deployment (CI/CD) pipelines. Google Cloud Platform (GCP) offers a suite of tools that can be seamlessly integrated with Bash scripts to automate various tasks, from provisioning resources to deploying applications.
Bash scripts provide a lightweight and flexible approach to automation. They are particularly useful for:
|
|
|
|
By integrating these scripts into CI/CD pipelines, teams can ensure consistent and repeatable deployments, reduce human error, and accelerate development cycles.
Before diving into automation, ensure the following prerequisites are in place:
Requirement | Command / Link |
---|---|
Install gcloud CLI | Google Cloud SDK Installation Guide |
Authenticate CLI | gcloud auth login |
Set default project | gcloud config set project [PROJECT_ID] |
Enable required APIs | gcloud services enable compute.googleapis.com (and others as needed) |
Let’s break down a sample Bash script that automates the following:
|
|
|
|
📄 |
#!/bin/bash
# Set variables
PROJECT_ID="your-project-id"
ZONE="us-central1-a"
INSTANCE_NAME="ci-cd-instance"
MACHINE_TYPE="e2-medium"
IMAGE_FAMILY="debian-11"
IMAGE_PROJECT="debian-cloud"
# Authenticate
gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS
gcloud config set project $PROJECT_ID
# Create VM instance
gcloud compute instances create $INSTANCE_NAME \
--zone=$ZONE \
--machine-type=$MACHINE_TYPE \
--image-family=$IMAGE_FAMILY \
--image-project=$IMAGE_PROJECT
# Deploy application (example: copy files and start server)
gcloud compute scp ./app/* $INSTANCE_NAME:/home/debian/ --zone=$ZONE
gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --command="sudo systemctl restart my-app"
# Output instance IP
EXTERNAL_IP=$(gcloud compute instances describe $INSTANCE_NAME --zone=$ZONE --format='get(networkInterfaces[0].accessConfigs[0].natIP)')
echo "App deployed at http://$EXTERNAL_IP"
Integrating with CI/CD Platforms |
Bash scripts and gcloud
commands can be seamlessly embedded in popular CI/CD platforms. Here’s an example for GitHub Actions.
🧬 GitHub Actions Workflow ( |
name: Deploy to GCP
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup gcloud CLI
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
export_default_credentials: true
- name: Run deployment script
run: |
chmod +x ./deploy.sh
./deploy.sh
📊 Common |
Task | Command Example |
---|---|
Set project | gcloud config set project [PROJECT_ID] |
Create VM | gcloud compute instances create |
Deploy to App Engine | gcloud app deploy |
Enable a service/API | gcloud services enable [API_NAME] |
View VM external IP | gcloud compute instances describe |
Authenticate with service account | gcloud auth activate-service-account |
🧠 Tips for Effective Bash Scripting in CI/CD |
Use Functions: Structure scripts with reusable functions for readability and maintenance.
Logging & Error Handling: Use set -e
and trap
for robust error handling.
Use Environment Variables: Store secrets securely using CI/CD platform’s secret store.
Use JSON/YAML Output: Many gcloud
commands support structured output for scripting.
📁 Directory Structure Example |
project-root/
│
├── deploy.sh
├── app/
│ ├── index.js
│ └── Dockerfile
└── .github/
└── workflows/
└── deploy.yml
Automating Google Cloud CLI tasks with Bash scripts is not only practical but essential for modern DevOps teams. When integrated into CI/CD pipelines, it accelerates deployments, reduces human error, and ensures consistent environments across development and production.
By leveraging simple, modular Bash scripts and integrating them with tools like GitHub Actions, you can turn complex cloud workflows into repeatable, reliable automation that saves time and scales with your team.
Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.
To calculate pi using a Bash script, you can use the bc command, which is a tool that can perform arithmetic operations in the terminal.
Learn how to install Google Cloud CLI on CentOS 9 with this comprehensive guide. Includes step-by-step commands, configuration tips, and troubleshooting advice to help you