Skip to content

Staging #29

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 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
54c0063
added jekinsfile
Apr 24, 2025
49a66e8
added jekinsfile
Apr 24, 2025
73e1433
added jekinsfile
Apr 24, 2025
62a2c92
added jekinsfile
Apr 24, 2025
efb1580
added jekinsfile
Apr 24, 2025
5dc1080
added jekinsfile
Apr 24, 2025
bd7c5af
added jekinsfile
Apr 25, 2025
9d86f5c
added dockerfile and new jenkinsfile
Apr 25, 2025
d371c54
added dockerfile and new jenkinsfile
Apr 25, 2025
4c766f2
added dockerfile and new jenkinsfile
Apr 25, 2025
29539ae
modified dockerfile as its a html app
Apr 25, 2025
80b03f4
modified dockerfile as its a html app
Apr 25, 2025
1bfa4b4
modified dockerfile as its a html app
Apr 25, 2025
6a5b15e
modified dockerfile as its a html app
Apr 25, 2025
72e2e18
modified dockerfile as its a html app
Apr 25, 2025
6bec41d
docker restart
Apr 26, 2025
f0e4da5
docker restart
Apr 26, 2025
80d58ab
docker restart
Apr 26, 2025
6a647a8
docker restart
Apr 26, 2025
b5efa97
docker restart
Apr 26, 2025
9e692a0
docker restart
Apr 26, 2025
f583c26
docker restart
Apr 26, 2025
7233351
docker restart
Apr 26, 2025
f43bf58
docker restart
Apr 26, 2025
9ae238e
docker restart
Apr 26, 2025
c750aad
docker restart
Apr 26, 2025
e5b2b5b
Update index.html
DevOps-Topics Apr 28, 2025
2751e19
Update index.html
DevOps-Topics Apr 28, 2025
2242370
Update index.html
DevOps-Topics Apr 28, 2025
bfb01a9
multipbranch setups
Apr 28, 2025
105f90e
Delete Jenkinsfile
DevOps-Topics Apr 28, 2025
3f9e38f
Merge pull request #1 from DevOps-Topics/newbranch
DevOps-Topics Apr 28, 2025
b2829cc
multipbranch setups
Apr 29, 2025
c362f8e
modified jenkinsfile with multistage no erro
Apr 29, 2025
8ad9f62
1 jenkinsfile for all branch
Apr 29, 2025
9017e9c
1 jenkinsfile for all branch
Apr 29, 2025
b25e437
trying parameter build
May 1, 2025
02f07f5
trying parameter build
May 1, 2025
d4d22ec
trying parameter build
May 1, 2025
b6ae0cc
Merge branch 'master' into staging
DevOps-Topics May 1, 2025
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
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Dockerfile
Jenkinsfile*
Jenkinsfile
jjenkinsfileold
README.MD
LICENSE.MD
*.scss
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM nginx:alpine
RUN rm -rf /usr/share/nginx/html/*
COPY . /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
38 changes: 38 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pipeline {
agent any

parameters {
string(
name: 'TAG',
defaultValue: 'origin/staging',
description: 'For example, to select a staging branch write "origin/staging".\nTo select tag v2014.4.1 write "v2014.4.1".'
)
string(
name: 'NODE_ENV',
defaultValue: 'production',
description: 'Node environment for the build.'
)
choice(
name: 'DEPLOY_CONFIG_BRANCH',
choices: ['preprod', 'master'],
description: 'If empty, it will skip all the custom repo and build LMS the old way.'
)
}

stages {
stage('Checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "*/${params.BRANCH}"]],
userRemoteConfigs: [[url: 'https://github.com/DevOps-Topics/static-website-example.git']]])
}
}
stage('Example') {
steps {
echo "TAG: ${params.TAG}"
echo "NODE_ENV: ${params.NODE_ENV}"
echo "DEPLOY_CONFIG_BRANCH: ${params.DEPLOY_CONFIG_BRANCH}"
}
}
}
}
Empty file added Jenkinsfile_
Empty file.
57 changes: 57 additions & 0 deletions Jenkinsfile_Deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// this has the structure on how to deploy the docker to by stopping the previous running container and will automatically update the tags with build number.


ipeline {
agent any

environment {
IMAGE_NAME = "village-app"
CONTAINER_NAME = "village-app"
}

stages {
stage('Checkout') {
steps {
checkout scm
}
}

stage('Stop Previous Container') {
steps {
script {
sh """
echo "Checking for existing container..."
if [ \$(docker ps -q -f name=${CONTAINER_NAME}) ]; then
echo "Stopping and removing running container..."
docker stop ${CONTAINER_NAME}
docker rm ${CONTAINER_NAME}
elif [ \$(docker ps -aq -f name=${CONTAINER_NAME}) ]; then
echo "Container exists but not running — removing it..."
docker rm ${CONTAINER_NAME}
else
echo "No existing container found."
fi
"""
}
}
}

stage('Docker Build') {
steps {
script {
docker.build("${IMAGE_NAME}:${BUILD_NUMBER}")
}
}
}

stage('Run New Container') {
steps {
script {
sh """
docker run -d -p 8181:80 --restart unless-stopped --name ${CONTAINER_NAME} ${IMAGE_NAME}:${BUILD_NUMBER}
"""
}
}
}
}
}
103 changes: 103 additions & 0 deletions Jenkinsfile_build_deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
pipeline {
agent any

environment {
AWS_REGION = 'ap-south-1'
ECR_REPO = '905418203037.dkr.ecr.ap-south-1.amazonaws.com/frontend/village-app'
CONTAINER_NAME = "village-app"
}

stages {
stage('Checkout') {
steps {
checkout scm
}
}

stage('Docker Build and Push') {
steps {
withCredentials([usernamePassword(credentialsId: 'aws-jenkins-cred', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY')]) {
sh '''
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REPO

docker build -t village-app:${BUILD_NUMBER} .
docker tag village-app:${BUILD_NUMBER} $ECR_REPO:${BUILD_NUMBER}
docker push $ECR_REPO:${BUILD_NUMBER}
'''
}
}
}

stage('Stop Previous Containers') {
steps {
sh '''
if [ $(docker ps -q -f name=$CONTAINER_NAME) ]; then
echo "Stopping the running container"
docker stop $CONTAINER_NAME
docker rm $CONTAINER_NAME
elif [ $(docker ps -aq -f name=$CONTAINER_NAME) ]; then
echo "Checking non running containers"
docker rm $CONTAINER_NAME
else
echo "No existing container found."
fi
'''
}
}

stage('Deploy New Container') {
steps {
sh '''
docker pull $ECR_REPO:$BUILD_NUMBER
docker run -d -p 8181:80 --restart unless-stopped --name $CONTAINER_NAME $ECR_REPO:$BUILD_NUMBER
'''
}
}
}
}



/*

example 2

The below is a different approach with aws configure set in cli

pipeline {
agent any

environment {
AWS_REGION = 'ap-south-1' // Your region
}

stages {
stage('Checkout') {
steps {
checkout scm
}
}

stage('Build Docker Image') {
steps {
withCredentials([usernamePassword(credentialsId: 'aws-jenkins-creds', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY')]) {
sh '''
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set default.region $AWS_REGION

# Now Docker build and push to ECR
eval $(aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin <your_account_id>.dkr.ecr.$AWS_REGION.amazonaws.com)

docker build -t my-app .
docker tag my-app:latest <your_account_id>.dkr.ecr.$AWS_REGION.amazonaws.com/my-app:latest
docker push <your_account_id>.dkr.ecr.$AWS_REGION.amazonaws.com/my-app:latest
'''
}
}
}
}
}


*/
99 changes: 99 additions & 0 deletions Jenkinsfile_ex3
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* dev will be deployed in ecr registry and ran in same jenkins server

staging will be deployed in s3



*/

pipeline {
agent any

environment {
IMAGE_NAME = "village-app"
AWS_REGION = "ap-south-1"
CONTAINER_NAME = "village-app"
ECR_REPO = "905418203037.dkr.ecr.ap-south-1.amazonaws.com/frontend/village-app"
}

stages {
stage('CHECKOUT') {
steps {
checkout scm
echo "The code is checked out from ${env.BRANCH_NAME}"
}
}

stage('BUILD/DEPLOY') {
steps {
withCredentials([usernamePassword(
credentialsId: 'aws-jenkins-cred',
usernameVariable: 'AWS_ACCESS_KEY_ID',
passwordVariable: 'AWS_SECRET_ACCESS_KEY'
)]) {
script {
if (env.BRANCH_NAME == 'development') {
echo "Building and deploying Docker image for development"

sh """
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REPO
docker build -t $IMAGE_NAME:${env.BRANCH_NAME}-${BUILD_NUMBER} .
docker tag $IMAGE_NAME:${env.BRANCH_NAME}-${BUILD_NUMBER} $ECR_REPO:${env.BRANCH_NAME}-${BUILD_NUMBER}
docker push $ECR_REPO:${env.BRANCH_NAME}-${BUILD_NUMBER}
"""
} else if (env.BRANCH_NAME == 'staging') {
echo "Syncing to S3 for staging"
sh """
aws s3 sync . s3://jenkins-static123/ --region $AWS_REGION --delete --exclude ".git/*" --exclude "Jenkinsfile"
"""
} else if (env.BRANCH_NAME == 'master') {
echo "Master branch — no deployment configured"
} else {
echo "Branch ${env.BRANCH_NAME} is not configured for deployment"
}
}
}
}
}

stage('Stop Previous Containers') {
when {
expression { env.BRANCH_NAME == 'development' }
}
steps {
sh '''
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" ]; then
echo "Stopping the running container"
docker stop $CONTAINER_NAME
docker rm $CONTAINER_NAME
elif [ "$(docker ps -aq -f name=$CONTAINER_NAME)" ]; then
echo "Removing non-running container"
docker rm $CONTAINER_NAME
else
echo "No existing container found."
fi
'''
}
}

stage('RUN CONTAINER') {
when {
expression { env.BRANCH_NAME == 'development' }
}
steps {
sh """
docker run -d -p 8181:80 --restart unless-stopped --name $CONTAINER_NAME $ECR_REPO:${env.BRANCH_NAME}-${BUILD_NUMBER}
"""
}
}
}

post {
success {
echo "Deployment was successful for ${env.BRANCH_NAME}"
}
failure {
echo "Deployment failed for ${env.BRANCH_NAME}"
}
}
}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="content">
<div class="inner">
<h1>Dimension</h1>
<p><!--[-->A fully responsive site template designed by <a href="https://html5up.net">HTML5 UP</a> and released<!--]--><br />
<p><!--[-->A fully responsive staging site template designed by <a href="https://html5up.net">HTML5 UP</a> and released<!--]--><br />
<!--[-->for free under the <a href="https://html5up.net/license">Creative Commons</a> license.<!--]--></p>
</div>
</div>
Expand Down
32 changes: 32 additions & 0 deletions jenkinsfile_S3
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pipeline {
agent any

stages {
stage ('checkout') {
steps {
checkout scm
}

}

stage ('deploy to s3') {
steps {
sh '''
aws s3 sync . s3://jenkins-static123/ --region ap-south-1 --delete --exclude ".git/*" --exclude "Jenkinsfile"
'''
}
}
}

post {
success {
echo 'Deployment is successfull'
}

failure {
echo 'Deployment is failed'
}

}
}