Skip to content

Develop #1674

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Develop #1674

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
109 changes: 109 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
pipeline {
agent any

environment {
BRANCH = "${env.BRANCH_NAME}"
SSH_USER = 'TaiKhau'
GCP_VM_DEV = '34.87.70.80'
GCP_VM_PROD = '34.87.10.98'
DEPLOY_DIR = "/home/TaiKhau/app"
// Adding Docker Hub variables
DOCKER_HUB_CREDS = credentials('dockerhub-credentials')
DOCKER_HUB_USERNAME = "${DOCKER_HUB_CREDS_USR}"
DOCKER_IMAGE_PREFIX = "${DOCKER_HUB_USERNAME}"
// Adding GitHub credentials
GITHUB_TOKEN = credentials('github-token')
}

stages {
stage('Checkout') {
steps {
git branch: "${BRANCH}", url: 'https://Qu11et:${GITHUB_TOKEN}@github.com/Qu11et/full-stack-fastapi-template.git'
}
}

stage('Build and Test') {
steps {
echo "Running on branch: ${BRANCH}"

script {
if (BRANCH == 'Dev') {
withCredentials([file(credentialsId: 'env-dev', variable: 'ENV_FILE')]) {
sh 'cp $ENV_FILE backend/.env'
echo "Development environment file copied from credentials"
}
} else if (BRANCH == 'Main') {
withCredentials([file(credentialsId: 'env-prod', variable: 'ENV_FILE')]) {
sh 'cp $ENV_FILE backend/.env'
echo "Production environment file copied from credentials"
}
}
}

// Run unit tests, lint, etc.
}
}

stage('Build Docker Images') {
steps {
script {
docker.build("${DOCKER_IMAGE_PREFIX}/my-frontend:${BRANCH}", 'frontend')
docker.build("${DOCKER_IMAGE_PREFIX}/my-backend:${BRANCH}", 'backend')
}
}
}

stage('Push Images to Docker Hub') {
steps {
// Login to Docker Hub
sh "echo ${DOCKER_HUB_CREDS_PSW} | docker login -u ${DOCKER_HUB_CREDS_USR} --password-stdin"

// Tag images with latest tag in addition to branch tag
sh """
docker tag ${DOCKER_IMAGE_PREFIX}/my-backend:${BRANCH} ${DOCKER_IMAGE_PREFIX}/my-backend:latest
docker tag ${DOCKER_IMAGE_PREFIX}/my-frontend:${BRANCH} ${DOCKER_IMAGE_PREFIX}/my-frontend:latest

# Push images with branch tag
docker push ${DOCKER_IMAGE_PREFIX}/my-backend:${BRANCH}
docker push ${DOCKER_IMAGE_PREFIX}/my-frontend:${BRANCH}

# Push images with latest tag
docker push ${DOCKER_IMAGE_PREFIX}/my-backend:latest
docker push ${DOCKER_IMAGE_PREFIX}/my-frontend:latest
"""

// Logout from Docker Hub
sh "docker logout"
}
}

stage('Deploy') {
steps {
sshagent(['gcp-vm-ssh-key']) {
script {
def target_ip = (BRANCH == 'Dev') ? GCP_VM_DEV : GCP_VM_PROD

sh """
ssh -o StrictHostKeyChecking=no $SSH_USER@$target_ip << EOF
cd $DEPLOY_DIR
docker pull ${DOCKER_IMAGE_PREFIX}/my-backend:${BRANCH}
docker pull ${DOCKER_IMAGE_PREFIX}/my-frontend:${BRANCH}
docker compose down
docker compose up -d
EOF
"""
}
}
}
}
}

post {
success {
echo "Deployment succeeded on branch ${BRANCH}"
}
failure {
echo "Deployment failed on branch ${BRANCH}"
}
}
}
Loading