config.yml 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ---
  2. spec:
  3. inputs:
  4. as:
  5. default: config-docker
  6. stage:
  7. default: config
  8. config_dir:
  9. default: ${CI_PROJECT_DIR}
  10. description: 'Config directory to copy.'
  11. project_file:
  12. default: 'compose.yaml'
  13. description: 'Docker Compose file to use.'
  14. remote_host:
  15. description: 'Remote host'
  16. remote_user:
  17. description: 'Remote user'
  18. remote_ssh:
  19. description: 'Remote ssh'
  20. remote_config:
  21. default: ${CI_PROJECT_DIR}
  22. description: 'Target directory on the remote server for the config.'
  23. remote_dir:
  24. default: ${CI_PROJECT_DIR}
  25. description: 'Directory on the remote server for the Docker Compose project.'
  26. restart:
  27. default: 'false'
  28. description: 'Restart the remote compose project after config update?'
  29. ---
  30. '$[[ inputs.as ]]':
  31. stage: '$[[ inputs.stage ]]'
  32. image: alpine:latest
  33. variables:
  34. CONFIG_DIR: "$[[ inputs.config_dir ]]"
  35. PROJECT_FILE: "$[[ inputs.project_file ]]"
  36. SSH_KEY: "$[[ inputs.remote_ssh ]]"
  37. REMOTE_HOST: "$[[ inputs.remote_host ]]"
  38. REMOTE_USER: "$[[ inputs.remote_user ]]"
  39. REMOTE_CONFIG: "$[[ inputs.remote_config ]]"
  40. REMOTE_PATH: "$[[ inputs.remote_dir ]]"
  41. RESTART: "$[[ inputs.restart ]]"
  42. before_script: |
  43. echo "Before → Executing..."
  44. echo "Before → Installing dependencies"
  45. apk add --no-cache openssh-client
  46. echo "Before → Adding ssh key"
  47. echo "$SSH_KEY" > id_rsa && chmod 600 id_rsa
  48. eval $(ssh-agent -s)
  49. ssh-add id_rsa
  50. script: |
  51. echo "Script → Executing..."
  52. echo "Script → Copying config file to remote host"
  53. ssh -o StrictHostKeyChecking=no $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_CONFIG"
  54. scp -o StrictHostKeyChecking=no $CONFIG_DIR/* $REMOTE_USER@$REMOTE_HOST:$REMOTE_CONFIG
  55. echo "Script → Executing remote commands"
  56. ssh -o StrictHostKeyChecking=no $REMOTE_USER@$REMOTE_HOST<<EOF
  57. if [ '$RESTART' = 'true' ]; then
  58. echo "Script → Restarting services"
  59. docker compose -f $REMOTE_PATH/$PROJECT_FILE down --remove-orphans
  60. docker compose -f $REMOTE_PATH/$PROJECT_FILE up -d
  61. fi
  62. EOF
  63. echo "Script ✓ Done"
  64. rules:
  65. - if: '$CI_COMMIT_REF_NAME == "main"'
  66. changes:
  67. - '$[[ inputs.config_dir ]]/**'