GitHub Actions and rsync
Automated Deployment of a Raydium Project using GitHub Actions and rsync.
A GitHub Actions workflow that uses rsync
to deploy your Raydium project. This method involves synchronizing the files from your local machine to the remote server. This is efficient for updating only the changed files and maintaining file permissions.
Here's a detailed guide to set up deployment using the rsync
strategy.
Prerequisites
- A GitHub account.
- A remote server with SSH access.
rsync
installed on your remote server.- Git installed on both your local machine and the remote server.
- Raydium installed and configured on your local machine.
- Basic knowledge of Git, SSH, and Raydium's WPFramework.
Step-by-Step Guide
1. Set Up Your Local Raydium Project
Initialize a Raydium Project:
bashraydium init my-project cd my-project
Initialize a Git Repository:
bashgit init git add . git commit -m "Initial commit"
Create a Repository on GitHub:
- Go to GitHub and create a new repository.
- Follow the instructions to add the remote repository to your local Git setup:bash
git remote add origin https://github.com/yourusername/your-repo.git git branch -M main git push -u origin main
2. Generate and Add SSH Keys for GitHub Actions
Generate SSH Key Pair:
bashssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Follow the prompts and save the keys to the default location (
~/.ssh/id_rsa
).Add the Public Key to the Remote Server:
bashssh-copy-id user@yourserver.com
Add the Private Key to GitHub Secrets:
- Display the private key:bash
cat ~/.ssh/id_rsa
- Copy the output and add it as a secret in your GitHub repository settings:
- Go to
Settings
>Secrets and variables
>Actions
. - Click
New repository secret
. - Name the secret
SSH_PRIVATE_KEY
and paste the private key content. - Click
Add secret
.
- Go to
- Display the private key:
3. Set Up GitHub Actions Workflow
Create a GitHub Actions Workflow: In your repository, create a directory for GitHub Actions workflows:
bashmkdir -p .github/workflows
Create a Deployment Workflow File:
bashnano .github/workflows/deploy.yml
Add the Following Workflow Configuration:
yamlname: Deploy to Server on: push: branches: - main schedule: - cron: '0 0 * * 0' # Runs at 00:00 UTC every Sunday jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up SSH uses: webfactory/ssh-agent@v0.5.3 with: ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} - name: Sync files with rsync run: | rsync -avz --delete \ -e "ssh -o StrictHostKeyChecking=no" \ ./ user@yourserver.com:/path/to/your/web/directory - name: Set permissions (optional) run: | ssh user@yourserver.com 'chown -R www-data:www-data /path/to/your/web/directory'
Explanation of the Workflow
- Name: The name of the workflow is "Deploy to Server".
- On: Specifies the events that trigger the workflow.
- Push: The workflow runs every time there is a push to the
main
branch. - Schedule: The workflow runs automatically at a specified interval.
- Cron: The cron expression
0 0 * * 0
means "At 00:00 (midnight) UTC every Sunday".
- Cron: The cron expression
- Push: The workflow runs every time there is a push to the
Workflow Steps
Checkout Code:
- Uses the
actions/checkout@v2
action to check out the repository code.
- Uses the
Set up SSH:
- Uses the
webfactory/ssh-agent@v0.5.3
action to set up the SSH agent with the private key stored in theSSH_PRIVATE_KEY
GitHub secret.
- Uses the
Sync Files with
rsync
:- Uses
rsync
to synchronize the files from the GitHub runner to the remote server. - The
-avz
options enable archive mode, verbose output, and compression, respectively. - The
--delete
option ensures that files deleted locally are also deleted on the remote server. - The
-e
option specifies the remote shell to use for the connection (ssh
withStrictHostKeyChecking
disabled).
- Uses
Set Permissions (Optional):
- Sets the appropriate permissions for the files on the remote server, ensuring they are owned by the web server user (e.g.,
www-data
).
- Sets the appropriate permissions for the files on the remote server, ensuring they are owned by the web server user (e.g.,
Adding the Workflow File
Create the Workflow Directory: If you haven't already, create the
.github/workflows
directory in your repository:bashmkdir -p .github/workflows
Create the Workflow File: Create a new file named
deploy.yml
in the.github/workflows
directory and add the above workflow configuration:bashnano .github/workflows/deploy.yml
Commit and Push the Workflow File: Commit the new workflow file to your repository and push it to GitHub:
bashgit add .github/workflows/deploy.yml git commit -m "Add GitHub Actions workflow for automatic deployment using rsync" git push origin main
GitHub Actions workflow that uses rsync
to deploy your Raydium project. This method is efficient for updating only the changed files and maintaining file permissions, ensuring a smooth and automated deployment process.