pipline.yml 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. pipeline {
  2. agent {
  3. docker {
  4. image 'golang:1.15-alpine'
  5. args '-v /data/my-app-cache:/go/.cache'
  6. }
  7. }
  8. options {
  9. timeout(time: 20, unit: 'MINUTES')
  10. disableConcurrentBuilds()
  11. }
  12. stages {
  13. stage('Build') {
  14. steps {
  15. withCredentials(bindings: [
  16. usernamePassword(credentialsId: 'GITHUB_CREDENTIAL', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_ACCESS_TOKEN'
  17. )
  18. ]) {
  19. sh '''
  20. git config --global url."https://${GITHUB_ACCESS_TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/"
  21. go mod tidy
  22. go build -o bin/my-app main.go
  23. '''
  24. }
  25. }
  26. }
  27. stage('Deploy') {
  28. environment {
  29. DEPLOY_HOST = credentials('DEPLOY_HOST')
  30. DEPLOY_PORT = credentials('DEPLOY_PORT')
  31. }
  32. steps {
  33. withCredentials([
  34. sshUserPrivateKey(credentialsId: 'SSH_CREDENTIAL', keyFileVariable: 'SSH_KEY', usernameVariable: 'SSH_USERNAME'),
  35. ]) {
  36. sh """
  37. mkdir -p ~/.ssh && chmod 700 ~/.ssh
  38. echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config
  39. cat ${SSH_KEY} > ~/.ssh/id_rsa && chmod 400 ~/.ssh/id_rsa
  40. scp -P ${DEPLOY_PORT} bin/my-app ${SSH_USER}@${DEPLOY_HOST}:/data/my-app
  41. ssh -p ${DEPLOY_PORT} ${SSH_USER}@${DEPLOY_HOST} \"nohup /data/my-app >> /data/my-app.log 2>&1 &\"
  42. """
  43. }
  44. }
  45. }
  46. }
  47. }