diff --git a/.jenkins/Jenkinsfile b/.jenkins/Jenkinsfile index 041f3d6..2013144 100644 --- a/.jenkins/Jenkinsfile +++ b/.jenkins/Jenkinsfile @@ -10,7 +10,7 @@ pipeline { CLIENT_ID = 'c302726f-fafb-4143-94c1-67a70975574a' DOCKER_REGISTRY_URL = 'marcin00.azurecr.io' DOCKER_IMAGE = "${DOCKER_REGISTRY_URL}/user-microservice:${GIT_COMMIT}" - DEPLOY_REPO = 'https://gitea.marcin00.pl/pikram/user-microservice-deploy.git' + DEPLOY_REPO = 'ssh://git@srv22.mikr.us:20343/pikram/user-microservice-deploy.git' } stages { @@ -49,15 +49,23 @@ pipeline { stage('Commit new version to GitOps repo') { steps { container('git') { - sh ''' - git config --global user.name "jenkins[bot]" - git config --global user.email "jenkins@marcin00.pl" - ''' - sh 'git clone ${DEPLOY_REPO} --branch jenkins-kubernetes' + withCredentials([string(credentialsId: 'gitea-known-host', variable: 'GITEA_KNOWN_HOST')]) { + sh ''' + mkdir -p ~/.ssh + echo "$GITEA_KNOWN_HOST" >> ~/.ssh/known_hosts + chmod 644 ~/.ssh/known_hosts + + git config --global user.name "jenkins[bot]" + git config --global user.email "jenkins@marcin00.pl" + + git clone ${DEPLOY_REPO} --branch jenkins-kubernetes + ''' + } + dir('user-microservice-deploy') { sh ''' # Podmień tag obrazu w pliku deploy.yaml - awk -v commit="ssh-creds-id$GIT_COMMIT" ' + awk -v commit="$GIT_COMMIT" ' $0 ~ /name:[[:space:]]*api/ { in_api_container = 1; print; next } in_api_container && $0 ~ /^[[:space:]]*image:[[:space:]]*/ { sub(/:[^:[:space:]]+$/, ":" commit) @@ -68,11 +76,12 @@ pipeline { { print } ' deploy.yaml > deploy.tmp && mv deploy.tmp deploy.yaml ''' - sh 'git commit -am "JENKINS: Changed deployed version to $GIT_COMMIT"' + sshagent(['gitea-deploy-key']) { sh ''' - git remote set-url origin ssh://git@srv22.mikr.us:20343/pikram/user-microservice-deploy.git - git push + git add deploy.yaml + git diff-index --quiet HEAD || git commit -m "JENKINS: Changed deployed version to $GIT_COMMIT" + git push origin jenkins-kubernetes ''' } }