Deploying a website to a server via SSH using GitHub Actions involves a similar process to using FTP, but instead, you'll be using SSH to connect to the server and transfer files. Here's how you can set up a GitHub Actions workflow to deploy your website to a server via SSH:
-
Set up SSH key authentication: Instead of using FTP credentials, you'll use SSH key authentication to connect to your server. Generate an SSH key pair (if you haven't already) and add the public key to the
~/.ssh/authorized_keysfile on your server. -
Store SSH private key as a secret: In your GitHub repository, go to "Settings" -> "Secrets" and add your SSH private key as a secret. You can copy the contents of your private key file and paste it into a secret named
SSH_PRIVATE_KEY. -
Create a deployment workflow: In your GitHub repository, create a new workflow file under the
.github/workflowsdirectory. For example, you can name itdeploy.yml. -
Define the deployment workflow: Configure the workflow to run on the
pushevent and specify the steps to deploy your website via SSH. Here's an example workflow YAML file:yamlCopy codename: Deploy Website on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Install SSH key uses: webfactory/ssh-agent@v0.5.3 with: ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} - name: Deploy via SSH run: | scp -r ./path/to/your/website/* username@your_server:/path/on/serverReplace
mainwith the branch you want to trigger the deployment on,./path/to/your/websitewith the path to your website files within your repository,usernamewith your SSH username, andyour_serverwith the IP address or hostname of your server. Adjust the destination path (/path/on/server) as needed. -
Commit and push the workflow file: Commit the workflow file changes and push them to your repository.
-
Monitor the workflow: After pushing changes to your repository, go to the "Actions" tab in your GitHub repository to monitor the deployment workflow. You should see the workflow listed there, and GitHub will automatically trigger it when changes are pushed to the specified branch.
-
Check the deployed website: Once the workflow completes successfully, check your server to verify that the website has been deployed.
By following these steps, you can set up GitHub Actions to automatically deploy your website to a server via SSH whenever changes are pushed to your repository's specified branch.