@@ -2,81 +2,84 @@ pipeline {
22 agent any
33
44 environment {
5- IMAGE_NAME = " static-site" // change to your image name
6- CONTAINER_NAME = " static-site" // name of the running container serving traffic
7- REGISTRY = " " // leave "" if you're not pushing to a registry
5+ // host paths
6+ PROD_DIR = " /opt/docker/site" // where site/compose.yml lives
7+ SITE_DIR = " /opt/docker/site/static-site/app" // where Dockerfile + app code should live/build from
8+
9+ IMAGE_NAME = " static-site"
10+ IMAGE_LATEST = " static-site:latest"
11+ SERVICE_NAME = " static-site"
12+ CONTAINER_NAME = " static-site"
813 }
914
1015 triggers {
11- // Fallback in case webhook fails.
12- pollSCM(' H/5 * * * *' ) // checks every 5 mins
16+ // optional: poll main in case webhook fails
17+ pollSCM(' H/5 * * * *' )
1318 }
1419
1520 stages {
16- stage(' Checkout' ) {
17- when {
18- branch ' main' // Only run on main branch
19- }
21+ stage(' Checkout main' ) {
22+ when { branch ' main' }
2023 steps {
2124 checkout scm
2225 }
2326 }
2427
25- stage(' Build Image' ) {
26- when {
27- branch ' main' // Only run on main branch
28- }
28+ stage(' Sync code into host build dir' ) {
29+ when { branch ' main' }
2930 steps {
30- script {
31- // use short commit SHA as version tag
32- COMMIT_SHA = sh(returnStdout : true , script : " git rev-parse --short HEAD" ). trim()
33- env. IMAGE_TAG = " ${ IMAGE_NAME} :${ COMMIT_SHA} "
34- env. LATEST_TAG = " ${ IMAGE_NAME} :latest"
31+ /*
32+ We need the latest app code to be present in /opt/docker/site/static-site
33+ so we can build the image FROM that directory using the host docker daemon.
3534
36- // Build from the app directory
37- sh """
38- cd app
39- docker build -t ${ IMAGE_TAG } -t ${ LATEST_TAG } .
40- """
41- }
35+ rsync copies the checked-out repo (current workspace) into SITE_DIR on the host.
36+ Adjust the source path ("./") if your actual app code is under ./app in git.
37+ */
38+ sh """
39+ rsync -a --delete ./ ${ SITE_DIR } /
40+ """
4241 }
4342 }
4443
45- stage(' Deploy Container' ) {
46- when {
47- branch ' main' // Only run on main branch
48- }
44+ stage(' Build image on host daemon' ) {
45+ when { branch ' main' }
4946 steps {
50- script {
51- // 1. Stop old container (ignore error if it isn't running yet)
52- sh """
53- docker ps --format '{{.Names}}' | grep -w ${ CONTAINER_NAME} && \
54- docker stop ${ CONTAINER_NAME} || \
55- echo 'Container not running, continue'
56- """
47+ /*
48+ Build the Docker image as static-site:latest using the host docker socket.
49+ After this, the host's Docker daemon now has an updated static-site:latest.
50+ */
51+ sh """
52+ cd ${ SITE_DIR}
53+ docker build -t ${ IMAGE_LATEST} -f Dockerfile .
54+ """
55+ }
56+ }
5757
58- // 2. Remove old container if it exists
59- sh """
60- docker ps -a --format '{{.Names}}' | grep -w ${ CONTAINER_NAME} && \
61- docker rm ${ CONTAINER_NAME} || \
62- echo 'No old container to remove'
63- """
58+ stage(' Redeploy via docker compose' ) {
59+ when { branch ' main' }
60+ steps {
61+ /*
62+ Now tell docker compose (in /opt/docker/site/compose.yml) to recreate the service.
63+ compose.yml MUST define the service like:
64+
65+ static-site:
66+ image: static-site:latest
67+ container_name: static-site
68+ ...
6469
65- // 3. Start new container with the rebuilt image
66- sh """
67- docker run -d \
68- --name ${ CONTAINER_NAME} \
69- -p 3000:3000 \
70- ${ LATEST_TAG}
71- """
72- }
70+ and MUST NOT have a 'build:' block anymore.
71+ */
72+ sh """
73+ cd ${ PROD_DIR}
74+ docker compose up -d ${ SERVICE_NAME}
75+ """
7376 }
7477 }
7578 }
7679
7780 post {
7881 success {
79- echo " Deploy succeeded: ${ env.IMAGE_TAG } is now live "
82+ echo " Deploy succeeded. ${ IMAGE_LATEST } is now running as ${ CONTAINER_NAME } . "
8083 }
8184 failure {
8285 echo " Deploy failed. Check build logs."
0 commit comments