Skip to content

Commit

Permalink
[FINISH] finished
Browse files Browse the repository at this point in the history
  • Loading branch information
ismoilovdevml committed Jan 18, 2024
1 parent 0119174 commit ecf1331
Showing 1 changed file with 35 additions and 83 deletions.
118 changes: 35 additions & 83 deletions pages/guides/ci-cd/jenkins-docker-ci-cd.en-UZ.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -792,12 +792,12 @@ Bu qismda esa `Secret`ga Serverimiz IP manzilini yozishimiz kerak.
### Dockerhub

<Callout type="info" emoji="">
**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)
</Callout>

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'
Expand All @@ -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 '
"""
}
}
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 '
"""
}
}
}
Expand Down Expand Up @@ -1122,4 +1064,14 @@ pipeline {
}
}
}
```
```

<Callout type="info" emoji="">

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**

</Callout>

0 comments on commit ecf1331

Please sign in to comment.