先前用 Powershell 遠端建置測試機的 DockerDesktop,開發人員一多後,發現太麻煩了,最後還是弄了Jenkins,並整合 Github 憑證及遠端佈署 DockerDesktop
預期開發者 Push Commit 後,Jenkins 自動將 Github Repo 拉下來,並自動建置,最後佈署到最終的 Windows Docker Desktop 主機。
$workspacePath = "C:\jenkins_workspace" New-Item -ItemType Directory -Path $workspacePath -Force | Out-Null
docker run -d -p 8080:8080 -p 50000:50000 -v ${hostWorkspacePath}:/var/jenkins_home/workspace -v /var/run/docker.sock:/var/run/docker.sock --name jenkins --restart=always jenkins/jenkins:lts
P.S. hostWorkspacePath 為宿主主機的資料夾路徑
chown -R jenkins:jenkins ${hostWorkspacePath} docker logs -f jenkins // 獲得 init password docker exec -it -uroot jenkins bash apt-get update && apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common && curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") $(lsb_release -cs) stable" && apt-get update && apt-get -y install docker-ce // jenkins 權限不足 usermod -aG docker jenkins usermod -aG root jenkins chmod 777 /var/run/docker.sock
docker exec -it -uroot jenkins bash
apt-get update apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - echo "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list apt-get update apt-get install -y docker-ce-cli
ssh-keygen -t rsa -C "root"
cat /root/.ssh/id_rsa.pub
cat /root/.ssh/id_rsa
properties([pipelineTriggers([githubPush()])]) pipeline { agent any environment { tag = ':latest' imageShortName = 'de-next-ap' imageName = "${imageShortName}${tag}" containerName = "${imageShortName}-1" containerUrl = "192.168.50.49:2375" dockerfile = "./Dockerfile" port = "30000:80" } stages { stage("GitHub Pull") { steps { git branch: 'main', credentialsId: 'b2ef50dd-ad9d-4583-a4ef-761d3e24da72', url: '[email protected]:markku636/ec.git/' } } stage('Stop containers') { steps { script { containerStatus = sh(script: "docker -H=\"${containerUrl}\" ps -a --filter=name=${containerName} -q", returnStdout: true).trim() if (containerStatus != '') { echo "Stopping container ${containerName}" sh "docker -H=\"${containerUrl}\" stop ${containerName}" } else { echo "Container ${containerName} does not exist" } } } } stage('Remove containers') { steps { script { containerStatus = sh(script: "docker -H=\"${containerUrl}\" ps -a --filter=name=${containerName} -q", returnStdout: true).trim() if (containerStatus != '') { echo "Removing container ${containerName}" sh "docker -H=\"${containerUrl}\" rm -f ${containerName}" } else { echo "Container ${containerName} does not exist" } } } } stage('Remove image') { steps { script { existingImages = sh(script: "docker -H=\"${containerUrl}\" images --filter=reference='${imageName}' -q", returnStdout: true).trim() if (existingImages != '') { echo "[Removing image] Removing the existing image.." sh "docker -H=\"${containerUrl}\" rmi -f '${imageName}'" } else { echo "[Removing image] The image does not exist" } } } } stage('Build image remotely') { steps { sh 'docker -H="${containerUrl}" build -t "${imageName}" . -f "${dockerfile}"' } } stage('Create and start container application') { steps { sh 'docker -H="${containerUrl}" run -d --name "${containerName}" --restart=always -p "${port}" "${imageShortName}"' } } } }
cat /var/jenkins_home/secrets/initialAdminPassword