jenkins-shared-library/vars/ansibleDeploy.groovy

24 lines
1.2 KiB
Groovy
Raw Permalink Normal View History

// Ansible se deploy. Deploy key sirf yahan bind hoti hai aur log me masked rehti hai.
def call(String target) {
checkout scm
withCredentials([
sshUserPrivateKey(credentialsId: 'deploy-key', keyFileVariable: 'DEPLOY_KEY', usernameVariable: 'DEPLOY_USER'),
string(credentialsId: 'vault-deployer-token', variable: 'VAULT_TOKEN')
]) {
withEnv(["TARGET=${target}"]) {
sh '''
set +x
curl -sf -H "X-Vault-Token: $VAULT_TOKEN" \
"$VAULT_ADDR/v1/$VAULT_MOUNT/data/registry" > /tmp/reg.$$.json
export REG_USER=$(python3 -c "import json;print(json.load(open('/tmp/reg.$$.json'))['data']['data']['username'])")
export REG_PASS=$(python3 -c "import json;print(json.load(open('/tmp/reg.$$.json'))['data']['data']['password'])")
rm -f /tmp/reg.$$.json
export ANSIBLE_HOST_KEY_CHECKING=False
ansible-galaxy collection install community.docker --force -q || true
ansible-playbook -i "$INVENTORY" "$PLAYBOOK" \
-u "$DEPLOY_USER" --private-key "$DEPLOY_KEY" -e target="$TARGET"
'''
}
}
}