The seamless integration of GitLab with Google Cloud has ushered in a new era of software development efficiency and flexibility. GitLab, lauded as the leading DevOps platform by Gartner in 2024, has consistently demonstrated its commitment to pushing the boundaries of innovation. Having used GitLab extensively in a previous role, I was eager to explore the latest enhancements and integration capabilities, especially those offered by Google Cloud.
Input Project ID and Project Number Input Pool ID and Provider ID
Input details according to your configuration.
# Replace ${PROJECT_ID}, ${PROJECT_NUMBER}, ${LOCATION}, ${POOL_ID} with your values below
WORKLOAD_IDENTITY=principalSet://iam.googleapis.com/projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/${POOL_ID}/attribute.developer_access/true
PROJECT_ID=YOUR_PROJECT_ID
gcloud projects add-iam-policy-binding ${PROJECT_ID} --member="${WORKLOAD_IDENTITY}" --role="roles/run.sourceDeveloper"
gcloud projects add-iam-policy-binding ${PROJECT_ID} --member="${WORKLOAD_IDENTITY}" --role="roles/iam.serviceAccountUser"
Flask == 3.0.3
flask-cors == 5.0.0
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/")
def read_root():
return {"message": "Health Check OK"}, 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port = 8080)
FROM python:3.10-slim
ENV PYTHONUNBUFFERED True
ENV PYTHONDONTWRITEBYTECODE True
ENV PYTHONPATH="${PYTHONPATH}:/app"
WORKDIR /app
COPY . ./
# Install python library
RUN pip install -r requirements.txt
CMD ["python", "main.py"]
stages:
- build
- push
- deploy
variables:
IMAGE_TAG: latest
PROJECT: YOUR_PROJECT_ID
LOCATION: YOUR_LOCATION
SERVICE: gitlab-test-app
AR_IMAGE: $LOCATION-docker.pkg.dev/$PROJECT/my-repository/$SERVICE
build-job:
stage: build
services:
- docker:24.0.5-dind
image: docker:git
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build -t $CI_REGISTRY_IMAGE:$IMAGE_TAG --build-arg="name=Cloud Run" .
- docker push $CI_REGISTRY_IMAGE:$IMAGE_TAG
include:
- component: gitlab.com/google-gitlab-components/artifact-registry/upload-artifact-registry@0.1.0
inputs:
stage: push
source: $CI_REGISTRY_IMAGE:$IMAGE_TAG
target: $AR_IMAGE:$IMAGE_TAG
- component: gitlab.com/google-gitlab-components/cloud-run/deploy-cloud-run@0.1.0
inputs:
stage: deploy
image: $AR_IMAGE:$IMAGE_TAG
project_id: $PROJECT
region: $LOCATION
service: $SERVICE
stages: Defines each job stage within the pipeline. Here, we divide the pipeline into three stages: build, push, and deploy.variables: Defines variables used repeatedly in jobs to improve YAML readability and minimize typos. GitLab CI/CD provides predefined variables like CI_REGISTRY_USER, CI_REGISTRY, and CI_REGISTRY_PASSWORD.build-job: This is a user-defined job to build the Docker image, utilizing a specific Docker image defined within the services and image fields.include: Uses this clause to reference external CI YAML files within your jobs. GitLab provides specific example CI YAML files for Google Cloud integrations.
YOUR_REPOSITORY
├── .gitlab-ci.yml
├── Dockerfile
├── README.md
├── main.py
└── requirements.txt
0 comments:
Post a Comment