pipeline { agent any environment { REGISTRY_CREDENTIALS = credentials('registry-credentials-id') PORTAINER_CREDENTIALS = credentials('portainer-credentials-id') } stages { stage('Installation') { steps { echo "start Installation" echo "finish Installation" } } stage('Build') { steps { echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}" echo 'Building..' echo "docker build -t registry.proxyq.co/jenkins/website:${env.BUILD_ID} ." sh "docker build -t registry.proxyq.co/jenkins/website:${env.BUILD_ID} ." } } stage('Testing') { steps { echo "start Testing" sh 'sleep 5' echo "finish Testing" } } stage('Publish') { steps { sh "docker login https://registry.proxyq.co -u ${REGISTRY_CREDENTIALS_USR} --password ${REGISTRY_CREDENTIALS_PSW}" sh "docker push registry.proxyq.co/jenkins/website:${env.BUILD_ID}" sh "docker tag registry.proxyq.co/jenkins/website:${env.BUILD_ID} registry.proxyq.co/jenkins/website:latest" sh "docker push registry.proxyq.co/jenkins/website:latest" } } stage('Redeploy') { steps { script { def json = """ {"username": "${PORTAINER_CREDENTIALS_USR}", "password": "${PORTAINER_CREDENTIALS_PSW}"} """ def jwtResponse = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', validResponseCodes: '200', httpMode: 'POST', ignoreSslErrors: true, consoleLogResponseBody: true, requestBody: json, url: "http://192.168.4.8:9000/api/auth" def jwtObject = new groovy.json.JsonSlurper().parseText(jwtResponse.getContent()) env.JWTTOKEN = "Bearer ${jwtObject.jwt}" } echo "Authenticated" script { def json = """ """ httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', validResponseCodes: '204', httpMode: 'POST', ignoreSslErrors: true, consoleLogResponseBody: true, requestBody: json, customHeaders: [["name": "Authorization", "value": "${env.JWTTOKEN}"]], url: "http://192.168.4.8:9000/api/endpoints/26/docker/containers/145772505f27bcdef1fbd807ecbb7b4ccbe6fb86dbb3a2bd0e22951557a15a10/restart" } echo "Restarting Container" } } } }