-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from luoziqing99/milestone6-yp
Milestone6 yp CI/CD app_deploy
- Loading branch information
Showing
11 changed files
with
222 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: Continuous Integration and Continuous Deployment | ||
run-name: ${{ github.actor }} submitted a CI CD Action | ||
on: | ||
push: | ||
branches: [ "main", "milestone6", "milestone6-yp"] | ||
|
||
jobs: | ||
Explore-GitHub-Actions: | ||
if: contains(github.event.head_commit.message, '/run-') | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: echo "Comment ${{ github.event.head_commit.message }}" | ||
- run: echo "Job was automatically triggered by a ${{ github.event_name }} event." | ||
- run: echo "Job is now running on a ${{ runner.os }} server hosted by GitHub!" | ||
- run: echo "Branch is ${{ github.ref }} and your repository is ${{ github.repository }}." | ||
- name: Check out repository code | ||
uses: actions/checkout@v4 | ||
- id: 'auth' | ||
uses: google-github-actions/auth@v1 | ||
with: | ||
credentials_json: '${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}' | ||
- name: Configure Google Cloud SDK | ||
uses: google-github-actions/setup-gcloud@v1 | ||
- name: Configure Docker Client | ||
run: |- | ||
gcloud auth configure-docker # --quiet #authenticate to gcr | ||
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." | ||
- run: echo "🖥️ The workflow is now ready to test your code on the runner." | ||
- name: List files in the repository | ||
run: | | ||
ls ${{ github.workspace }} | ||
- name: Build App Deploy Container | ||
run: |- | ||
cd ${{ github.workspace }}/src/app_deploy/ | ||
docker build -t app_deployment --platform=linux/amd64 -f Dockerfile . | ||
- name: Run Deploy App | ||
if: contains(github.event.head_commit.message, '/run-deploy-app') | ||
run: |- | ||
docker run --rm --name app_deployment \ | ||
-v /var/run/docker.sock:/var/run/docker.sock \ | ||
-v $HOME/.ssh:/home/app/.ssh \ | ||
-v ${{ github.workspace }}/src/frontend:/frontend-react \ | ||
-v ${{ github.workspace }}/src/api-service:/api-service \ | ||
--volume $GITHUB_WORKSPACE:/workspace \ | ||
--mount type=bind,source=$GOOGLE_APPLICATION_CREDENTIALS,target=/secrets/deployment.json \ | ||
--env GOOGLE_APPLICATION_CREDENTIALS=/secrets/deployment.json \ | ||
-e USE_GKE_GCLOUD_AUTH_PLUGIN=True \ | ||
-e GCP_PROJECT=ac215project-398401 \ | ||
-e GCP_ZONE=us-west3-b \ | ||
app_deployment ./deploy-app.sh | ||
- run: echo "Job's status is ${{ job.status }}." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Build and Push Docker Containers to GCR | ||
ansible-playbook deploy-docker-images.yml -i inventory.yml | ||
# Create and Deploy Cluster | ||
ansible-playbook deploy-k8s-cluster.yml -i inventory.yml --extra-vars cluster_state=present | ||
# Once the command runs go to http://<YOUR INGRESS IP>.sslip.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Build and Push Docker Containers to GCR | ||
ansible-playbook deploy-docker-images.yml -i inventory.yml | ||
# Update Cluster | ||
ansible-playbook update-k8s-cluster.yml -i inventory-prod.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
all: | ||
vars: | ||
gcp_service_account_file: "/secrets/deployment.json" | ||
gcp_service_account_email: "[email protected]" | ||
gcp_auth_kind: "serviceaccount" | ||
gcp_scopes: "https://www.googleapis.com/auth/compute" | ||
gcp_project: "ac215project-398401" | ||
gcp_region: "us-west3" | ||
gcp_zone: "us-west3-b" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
|
||
# exit immediately if a command exits with a non-zero status | ||
set -e | ||
|
||
# Define some environment variables | ||
export IMAGE_NAME="app_deployment" | ||
export BASE_DIR=$(pwd) | ||
export SECRETS_DIR=$(pwd)/../../../secrets/ | ||
export GCP_PROJECT="ac215project-398401" # Change to your GCP Project | ||
export GCP_ZONE="us-west3-b" | ||
export GOOGLE_APPLICATION_CREDENTIALS=/secrets/deployment.json | ||
|
||
# Build the image based on the Dockerfile | ||
#docker build -t $IMAGE_NAME -f Dockerfile . | ||
docker build -t $IMAGE_NAME --platform=linux/amd64 -f Dockerfile . | ||
|
||
# Run the container | ||
docker run --rm --name $IMAGE_NAME \ | ||
-v /var/run/docker.sock:/var/run/docker.sock \ | ||
-v "$BASE_DIR":/app \ | ||
-v "$SECRETS_DIR":/secrets \ | ||
-v "$HOME/.ssh":/home/app/.ssh \ | ||
-v "$BASE_DIR/../api-service":/api-service \ | ||
-v "$BASE_DIR/../frontend":/frontend-react \ | ||
-e GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS \ | ||
-e USE_GKE_GCLOUD_AUTH_PLUGIN=True \ | ||
-e GCP_PROJECT=$GCP_PROJECT \ | ||
-e GCP_ZONE=$GCP_ZONE \ | ||
$IMAGE_NAME ./deploy-app.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
- name: "Update Kubernetes Cluster" | ||
hosts: localhost | ||
gather_facts: false | ||
|
||
vars: | ||
cluster_name: "science-tutor-cluster" | ||
|
||
tasks: | ||
- name: "Connect to cluster (update kubeconfig)" | ||
shell: "gcloud container clusters get-credentials {{ cluster_name }} --zone {{ gcp_zone }} --project {{ gcp_project }}" | ||
|
||
- name: "Copy docker tag file" | ||
copy: | ||
src: .docker-tag | ||
dest: .docker-tag | ||
mode: 0644 | ||
|
||
- name: "Get docker tag" | ||
shell: "cat .docker-tag" | ||
register: tag | ||
|
||
- name: "Print tag" | ||
debug: | ||
var: tag | ||
|
||
- name: "Update Deployment for Frontend" | ||
k8s: | ||
state: present | ||
definition: | ||
apiVersion: v1 | ||
kind: Deployment | ||
metadata: | ||
name: frontend | ||
namespace: "{{cluster_name}}-namespace" | ||
spec: | ||
selector: | ||
matchLabels: | ||
run: frontend | ||
template: | ||
metadata: | ||
labels: | ||
run: frontend | ||
spec: | ||
containers: | ||
- image: "gcr.io/{{ gcp_project }}/science-tutor-frontend-react:{{ tag.stdout}}" | ||
imagePullPolicy: IfNotPresent | ||
name: frontend | ||
ports: | ||
- containerPort: 80 | ||
protocol: TCP | ||
restartPolicy: Always | ||
|
||
- name: "Update Deployment for API Service" | ||
k8s: | ||
state: present | ||
definition: | ||
apiVersion: v1 | ||
kind: Deployment | ||
metadata: | ||
name: api | ||
namespace: "{{cluster_name}}-namespace" | ||
spec: | ||
selector: | ||
matchLabels: | ||
run: api | ||
template: | ||
metadata: | ||
labels: | ||
run: api | ||
spec: | ||
volumes: | ||
- name: persistent-vol | ||
emptyDir: {} | ||
- name: google-cloud-key | ||
secret: | ||
secretName: gcp-service-key | ||
containers: | ||
- image: gcr.io/{{ gcp_project }}/science-tutor-api-service:{{ tag.stdout}} | ||
imagePullPolicy: IfNotPresent | ||
name: api | ||
restartPolicy: Always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters