From cd7020169dbbc29717df622eb210d01ff720602d Mon Sep 17 00:00:00 2001 From: Aly Sewelam Date: Thu, 11 Sep 2025 15:41:01 +0300 Subject: [PATCH] first commit --- README.md | 2 ++ action.yml | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 README.md create mode 100644 action.yml diff --git a/README.md b/README.md new file mode 100644 index 0000000..8f78cf6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Deploy Compose +This is a gitea action for deploying docker compose files onto a remote server using ssh,scp and docker-compose up etc diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..208e865 --- /dev/null +++ b/action.yml @@ -0,0 +1,95 @@ +name: Deploy Docker Compose via SSH + +on: + push: + paths: + - ${{ inputs.trigger_path }} + branches: [ prod ] + +inputs: + docker_compose_path: + description: 'Path to deploy docker-compose.yml (e.g. /home/user/app/docker-compose.yml)' + required: false + default: 'docker-compose.yml' + remote_host: + description: 'Remote host (e.g. 192.168.1.100)' + required: false + default: '185.218.126.87' + remote_user: + description: 'Remote user (e.g. ubuntu)' + required: false + default: 'linly' + trigger_path: + description: 'The path pattern that will trigger teh action' + required: true + deployment_path: + description: 'the path where the docker-compose file will be copied to' + required: true + default: '/home/linly/services/gitea' + + +jobs: + deploy: + runs-on: ubuntu-latest + + env: + # These are provided via GitHub Secrets + SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} + SSH_KNOWN_HOSTS: ${{ secrets.SSH_KNOWN_HOSTS }} + DEPLOYMENT_PATH: ${{ inputs.deployment_path || 'docker-compose.yml' }} + REMOTE_HOST: ${{ inputs.remote_host || 'your-server.example.com' }} + REMOTE_USER: ${{ inputs.remote_user || 'ubuntu' }} + DOCKER_COMPOSE_PATH: ${{ inputs.docker_compose_path }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up SSH agent (to use private key) + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + # Optional: add known hosts + echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts + chmod 644 ~/.ssh/known_hosts + # Ensure SSH config + echo "Host ${REMOTE_HOST}" >> ~/.ssh/config + echo " HostName ${REMOTE_HOST}" >> ~/.ssh/config + echo " User ${REMOTE_USER}" >> ~/.ssh/config + echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config + echo " StrictHostKeyChecking yes" >> ~/.ssh/config + echo " UserKnownHostsFile ~/.ssh/known_hosts" >> ~/.ssh/config + + - name: Copy docker-compose.yml to remote host + run: | + # Ensure the remote path exists + mkdir -p "$(dirname "${DEPLOYMENT_PATH}")" + # Use scp to copy the file + scp -o StrictHostKeyChecking=yes -o UserKnownHostsFile= ~/.ssh/known_hosts \ + -i ~/.ssh/id_rsa \ + ${COMPOSE_PATH} \ + ${REMOTE_USER}@${REMOTE_HOST}:"${DEPLOYMENT_PATH}" + shell: bash + + - name: Deploy services via SSH + run: | + # Run docker-compose up -d on the remote host + ssh -o StrictHostKeyChecking=yes \ + -o UserKnownHostsFile= ~/.ssh/known_hosts \ + -i ~/.ssh/id_rsa \ + ${REMOTE_USER}@${REMOTE_HOST} \ + "docker-compose -f ${DEPLOYMENT_PATH} up -d --timeout 300" + shell: bash + + - name: Verify services are running + run: | + ssh -o StrictHostKeyChecking=yes \ + -o UserKnownHostsFile= ~/.ssh/known_hosts \ + -i ~/.ssh/id_rsa \ + ${REMOTE_USER}@${REMOTE_HOST} \ + "docker-compose -f ${DEPLOYMENT_PATH} ps" + shell: bash + + - name: Final success message + run: echo "✅ Docker Compose deployed successfully on ${REMOTE_HOST}"