From ecf1331dfd5549d6cb56193704485697aee7867d Mon Sep 17 00:00:00 2001 From: ismoilovdevml Date: Thu, 18 Jan 2024 17:49:58 +0500 Subject: [PATCH] [FINISH] finished --- .../ci-cd/jenkins-docker-ci-cd.en-UZ.mdx | 118 ++++++------------ 1 file changed, 35 insertions(+), 83 deletions(-) diff --git a/pages/guides/ci-cd/jenkins-docker-ci-cd.en-UZ.mdx b/pages/guides/ci-cd/jenkins-docker-ci-cd.en-UZ.mdx index 56862df..cc79c1e 100644 --- a/pages/guides/ci-cd/jenkins-docker-ci-cd.en-UZ.mdx +++ b/pages/guides/ci-cd/jenkins-docker-ci-cd.en-UZ.mdx @@ -792,12 +792,12 @@ Bu qismda esa `Secret`ga Serverimiz IP manzilini yozishimiz kerak. ### Dockerhub -**ESLATMA->** Ushbu **CD** qism asosan Dockerhub va boshqa container registilarda ishash uchun moslashtirilgan! [GCR uchun quyidagi qismga o'ting](#gcr) +**ESLATMA->** Ushbu **CD** qism asosan Dockerhub va boshqa container registrylarda ishash uchun moslashtirilgan! [GCR uchun quyidagi qismga o'ting](#gcr) -Endi Serverimizga kerakli `environment`larni qo'shib chiqishimiz kerak. +**1->** Endi Serverimizga kerakli `environment`larni qo'shib chiqishimiz kerak. -```groovy {4-6,8-9} +```groovy {4-6,8-10} environment { DISCORD_WEBHOOK = credentials('discord-webhook') GIT_URL = 'https://github.com/ismoilovdevml/devops-journey.git' @@ -807,30 +807,24 @@ environment { GIT_TOKEN = credentials('git-token') SERVER_USERNAME = credentials('server-username') SERVER_IP = credentials('server-ip') + SSH_CREDENTIALS = credentials('server-ssh') BRANCH_NAME = 'main' } ``` - +**2-> Deploy Server** bosqichi ```groovy stage('Deploy Server') { steps { - withCredentials([usernamePassword( - credentialsId: 'dockerhub', - usernameVariable: 'DOCKER_USERNAME', - passwordVariable: 'DOCKER_PASSWORD' - )]) { + withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) { script { - sshagent(credentials: ['server-ssh']) { + sshagent (credentials: ['server-ssh']) { sh """ - ssh -o StrictHostKeyChecking=no ${SERVER_USERNAME}@${SERVER_IP} \ - '/home/username/deployer.sh \ - --image=${REGISTRY_URL}/${CONTAINER_NAME}:${BUILD_NUMBER} \ - --container-port=8080 \ - --system-port=8080 \ - --registry-host=${REGISTRY_URL} \ - --container-name=${CONTAINER_NAME} \ - --registry-token=${DOCKER_PASSWORD} \ - --registry-user=${DOCKER_USERNAME}' + ssh -o StrictHostKeyChecking=no ${SERVER_USERNAME}@${SERVER_IP} '\ + docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} && \ + docker pull ${REGISTRY_URL}/${CONTAINER_NAME}:latest && \ + docker stop ${CONTAINER_NAME} || true && \ + docker rm ${CONTAINER_NAME} || true && \ + docker run -d -p 4001:4001 --name ${CONTAINER_NAME} --restart always ${REGISTRY_URL}/${CONTAINER_NAME}:latest ' """ } } @@ -839,66 +833,6 @@ stage('Deploy Server') { } ``` -Shu qismda bizda bitta muammo bor yani oldindan shu nomli container ishlab turgan bo'lsa conflict kelib chiqadi. Buni oldini olish uchun serverda `deployer.sh` nomli bash script yozamiz. U script vazifasi quyidagicha unga Jenkins pipelineda tashqi argumentlar beriladi shu nomli ishlab turgan container bo'lsa uni o'chirib tozalab yangi containerni berilgan portda ishga tushirib qo'yadi. Bash script quyidagicha: - - -```bash -nano deployer.sh -``` - -```bash filename="deployer.sh" {36,44} -!/bin/bash -while [ $# -gt 0 ]; do - case "$1" in - --image=*) - IMAGE="${1#*=}" - ;; - --container-port=*) - CONTAINER_PORT="${1#*=}" - ;; - --system-port=*) - SYSTEM_PORT="${1#*=}" - ;; - --registry-token=*) - REGISTRY_TOKEN="${1#*=}" - ;; - --registry-host=*) - REGISTRY_HOST="${1#*=}" - ;; - --container-name=*) - CONTAINER_NAME="${1#*=}" - ;; - --registry-user=*) - REGISTRY_USER="${1#*=}" - ;; - *) - printf "***************************\n" - printf "* Error: Invalid argument.*\n" - printf "***************************\n" - exit 1 - esac - shift -done - - -echo y | docker container prune -echo $REGISTRY_TOKEN | docker login $REGISTRY_HOST -u $REGISTRY_USER --password-stdin -docker pull $IMAGE -echo "Clean temp Container" - -if $(docker ps | awk -v CONTAINER_NAME="$CONTAINER_NAME" 'NR > 1 && $NF == CONTAINER_NAME{ret=1; exit} END{exit !ret}' ); then - docker stop "$CONTAINER_NAME" - docker rm -f "$CONTAINER_NAME" -fi -docker run -d -p $SYSTEM_PORT:$CONTAINER_PORT --restart=always --name $CONTAINER_NAME $IMAGE -``` -Bash scriptni executable qilib qo'yamiz. - -```bash -sudo chmod +x deployer.sh -``` -Pipelinedagi `/home/username/deployer.sh` qismini deployer.sh scriptingiz joylashgan yo'lni(path)ni ko'rsatib qo'yishingiz kerak. Ushbu bosqich ssh bilan serverga kirib `deployer.sh` bash scriptga tashqi argumentlar berib uni ishga tushiradi. Pipelineni yangilab uni ishga tushiramiz agar sizda hammasi to'gri qilingan bo'lsa u muvaffiqyatli ishlashi kerak. - ### GCR **1->** **GCR** uchun `environment` qismi. @@ -948,9 +882,9 @@ stage('Deploy to Server') { ## Xulosa -Dockerhub bilan ishlaydigan to'liq CI/CD Jenkins pipeline +**Dockerhub** bilan ishlaydigan to'liq CI/CD Jenkins pipeline -```groovy +```groovy {11-13, 45-63} pipeline { agent any @@ -963,6 +897,7 @@ pipeline { GIT_TOKEN = credentials('git-token') SERVER_USERNAME = credentials('server-username') SERVER_IP = credentials('server-ip') + SSH_CREDENTIALS = credentials('server-ssh') BRANCH_NAME = 'main' } stages { @@ -999,7 +934,14 @@ pipeline { withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) { script { sshagent (credentials: ['server-ssh']) { - sh "ssh -o StrictHostKeyChecking=no ${SERVER_USERNAME}@${SERVER_IP} '/home/username/deployer.sh --image=${REGISTRY_URL}/${CONTAINER_NAME}:${BUILD_NUMBER} --container-port=3000 --system-port=3000 --registry-host=${REGISTRY_URL} --container-name=${CONTAINER_NAME} --registry-token=${DOCKER_PASSWORD} --registry-user=${DOCKER_USERNAME}'" + sh """ + ssh -o StrictHostKeyChecking=no ${SERVER_USERNAME}@${SERVER_IP} '\ + docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} && \ + docker pull ${REGISTRY_URL}/${CONTAINER_NAME}:latest && \ + docker stop ${CONTAINER_NAME} || true && \ + docker rm ${CONTAINER_NAME} || true && \ + docker run -d -p 4001:4001 --name ${CONTAINER_NAME} --restart always ${REGISTRY_URL}/${CONTAINER_NAME}:latest ' + """ } } } @@ -1122,4 +1064,14 @@ pipeline { } } } -``` \ No newline at end of file +``` + + + +Ushbu loyiha GitHub repositoriyasi: [github.com/ismoilovdevml/devops-journey](https://github.com/ismoilovdevml/devops-journey.git) + +**Sana:** 2024.01.18 (2024-yil 18-yanvar) + +**Muallif: Otabek Ismoilov** + + \ No newline at end of file