FairPluto
FairPluto

Reputation: 737

Manage environments with Github and Google Kubernetes Engine

I have a Github repo with 2 branches on it, develop and main. The first is the "test" environment and the other is the "production" environment. I am working with Google Kubernetes Engine and I have automated deployment from the push on Github to the deploy on GKE. So our workflow is :

  1. Pull develop
  2. Write code and test locally
  3. When everything is fine locally, push on develop (it will automatically deploy on GKE workload app_name_develop)
  4. QA tests on app_name_develop
  5. If QA tests passed, we create a pull request to put develop into main
  6. Automatically deploy on GKE workload app_name_production (from the main branch)

The deployment of the container is defined in Dockerfile and the Kubernetes deployment is defined in kubernetes/app.yaml. Those two files are tracked with Git inside the repo.

The problem here is when we create a pull request to put develop into main, it also take the two files app.yaml and Dockerfile from develop to main. We end up with the settings from develop in main, and it messes the whole thing.

I can't define environment variables in those files because it could end up in the wrong branch. My question is : How can I exclude those files from the pull request ? Or is there any way to manage multiple environments without having to manually modify the files after each pull request ?

I don't know if it can hlphere is my Dockerfile :

FROM python:3.8

RUN apt-get update && apt-get install -y --no-install-recommends

RUN python -m pip install --upgrade pip

WORKDIR /app/

COPY requirements.txt .
RUN python -m pip install -r requirements.txt

COPY . .

EXPOSE 8080
CMD ["gunicorn", "-b", ":8080", "main:app"]

And here is my YAML file to deploy on GKE (actually I took the one advised by GKE when creating automated deployment) :

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: app_name-api
  name: app_name-api
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app_name-api
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: app_name-api
    spec:
      containers:
        - image: gcr.io/path_to_image/github.com/company_name/app_name
          imagePullPolicy: IfNotPresent
          name: app_name-1
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  labels:
    app: app_name-api
  name: app_name-api-pod-autoscaler
  namespace: default
spec:
  maxReplicas: 3
  metrics:
    - resource:
        name: cpu
        targetAverageUtilization: 80
      type: Resource
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: app_name-api

Thanks a lot for any help you could provide !

Upvotes: 0

Views: 236

Answers (1)

Jyothi Kiranmayi
Jyothi Kiranmayi

Reputation: 2533

You can't ignore some files from a pull request selectively. But there are 2 simple workarounds for this :

First -
Create a new branch from ‘develop’

Replace the non-required files from 'main'

Create pull request from this new branch

Second -
Create a new branch from 'main'

Put changes of required files from 'develop'

Create pull request from this new branch

Any of these methods will work. Which will be easier depends on how many files are to be included / excluded.

Example :
Considering main as target and dev as source

root 
|-- src 
| -- app.py 
|-- .gitignore 
|-- settings.py 
|-- requirements.txt

Let's say, I would want to ignore the settings.py file from being merged First move to the target branch (the branch to which you want to merge the changes)

git checkout main

Then you can use the git checkout command to selective pick the files you want to merge

git checkout dev src/

This will only merge the files changed inside src/ folder

NOTE: You can also do it selectively for each file. 

Then push to remote repository

git push origin main

Bear in mind that this solution is useful only if the files to be excluded are small.

Note: "There are tools that are built to solve this problem like skaffold and kustomize, but they might take a bit of time and restructuring of your repository before everything works. So, in the meantime, this is a simple solution which requires manual work but can do while you study and decide which of the more advanced instrumentation is suitable ."

Upvotes: 2

Related Questions