Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deployment pipeline #551

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions .github/workflows/deploy-to-gcp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Build and Deploy to GKE

on:
push:
branches:
- main

env:
PROJECT_ID: ${{ secrets.GCP_PROJECT }}
GKE_CLUSTER: timeoff-mgmt-cluster
GKE_REGION: us-central1
GKE_ZONE: us-central1-a
DEPLOYMENT_NAME: timeoff-mgmt-deploy
IMAGE: timeoff-mgmt-image
CHART_NAME: timeoff-mgmt-chart
REPO_NAME: timeoff-mgmt-repo
NAMESPACE: timeoff-mgmt-dev

jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@v3

# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@94337306dda8180d967a56932ceb4ddcf01edae7
with:
service_account_key: ${{ secrets.GKE_SA_KEY }}
project_id: ${{ secrets.GCP_PROJECT }}

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.14

- name: Apply Terraform
env:
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GCP_DEPLOY_SA }}
run: |
cd ../../terraform/dev/
terraform init -backend-config=backend.tf
terraform apply -auto-approve

# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- run: |-
gcloud --quiet auth configure-docker

# Get the GKE credentials so we can deploy to the cluster
- uses: google-github-actions/get-gke-credentials@fb08709ba27618c31c09e014e1d8364b02e5042e
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_ZONE }}
credentials: ${{ secrets.GCP_DEPLOY_SA }}

# Build the Docker image
- name: Build
run: |-
docker build \
--tag "$GKE_REGION-docker.pkg.dev/$PROJECT_ID/$REPO_NAME/$IMAGE:$GITHUB_SHA" \
--build-arg GITHUB_SHA="$GITHUB_SHA" \
--build-arg GITHUB_REF="$GITHUB_REF"

# Push the Docker image to Google Container Registry
- name: Publish
run: |-
docker push "$GKE_REGION-docker.pkg.dev/$PROJECT_ID/$REPO_NAME/$IMAGE:$GITHUB_SHA"

# Set up Helm
- name: Set up Helm
uses: helm/setup-helm@v1
with:
version: 3.11.0

# Create a new tag for the chart version
- name: Create Tag
run: |
git tag ${GITHUB_SHA} && git push --tags

# Package the chart
- name: Package Chart
run: |
helm package --version ${GITHUB_SHA} ../../k8s/timeoff-mgmt

# Publish the chart to the chart repo
- name: Publish Chart
run: |
helm push ${CHART_NAME}-${GITHUB_SHA}.tgz oci://$GKE_REGION-docker.pkg.dev/$PROJECT_ID/$REPO_NAME

# Deploy the chart to the GKE cluster
- name: Deploy to GKE
run: |
helm upgrade ${DEPLOYMENT_NAME} ${CHART_NAME}-${GITHUB_SHA}.tgz --install --wait --namespace ${NAMESPACE} \
--set image.tag=$GITHUB_SHA,image.repository=$GKE_REGION-docker.pkg.dev/$PROJECT_ID/$REPO_NAME/$IMAGE
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
*.swm
node_modules/
*.sqlite
.idea/
.terraform.lock.hcl
.terraform/
terraform.tfstate
terraform.tfstate.backup
18 changes: 11 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
# =============
# 1. Create an empty directory and copy this file into it.
#
# 2. Create image with:
# 2. Create image with:
# docker build --tag timeoff:latest .
#
# 3. Run with:
# 3. Run with:
# docker run -d -p 3000:3000 --name alpine_timeoff timeoff
#
# 4. Login to running container (to update config (vi config/app.json):
# 4. Login to running container (to update config (vi config/app.json):
# docker exec -ti --user root alpine_timeoff /bin/sh
# --------------------------------------------------------------------
FROM alpine:latest as dependencies
FROM node:14-alpine as dependencies

RUN apk add --no-cache \
nodejs npm
nodejs npm \
python3 \
make g++

ENV PYTHON python3

COPY package.json .
RUN npm install
RUN npm install

FROM alpine:latest
FROM node:14-alpine

LABEL org.label-schema.schema-version="1.0"
LABEL org.label-schema.docker.cmd="docker run -d -p 3000:3000 --name alpine_timeoff"
Expand Down
23 changes: 23 additions & 0 deletions k8s/timeoff-mgmt/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
8 changes: 8 additions & 0 deletions k8s/timeoff-mgmt/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v2
name: timeoff-mgmt
version: 0.1.0
description: A Helm chart for deploying a Node.js application on GKE
maintainers:
- name: Juan Pablo Rivas
email: [email protected]
appVersion: 1.0.0
26 changes: 26 additions & 0 deletions k8s/timeoff-mgmt/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.deployment.name }}
spec:
replicas: {{ .Values.deployment.replicas }}
selector:
matchLabels:
app: {{ .Values.deployment.name }}
template:
metadata:
labels:
app: {{ .Values.deployment.name }}
spec:
containers:
- name: {{ .Values.deployment.name }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
ports:
- containerPort: {{ .Values.service.port }}
resources:
limits:
cpu: {{ .Values.resources.limits.cpu }}
memory: {{ .Values.resources.limits.memory }}
requests:
cpu: {{ .Values.resources.requests.cpu }}
memory: {{ .Values.resources.requests.memory }}
14 changes: 14 additions & 0 deletions k8s/timeoff-mgmt/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.service.name }}
labels:
app: {{ .Values.deployment.name }}
spec:
ports:
- name: {{ .Values.portName }}
port: {{ .Values.service.port }}
targetPort: {{ .Values.service.port }}
selector:
app: {{ .Values.deployment.name }}
type: ClusterIP
17 changes: 17 additions & 0 deletions k8s/timeoff-mgmt/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
deployment:
name: timeoff-mgmt-deployment
replicas: 1
image:
repository: us-central1-docker.pkg.dev/timeoff-mgmt/timeoff-mgmt-registry/timeoff-mgmt
tag: latest
service:
port: 80
name: timeoff-mgmt-svc
portName: http
resources:
limits:
cpu: "100m"
memory: "256Mi"
requests:
cpu: "100m"
memory: "256Mi"
Loading