A memo: Using GitHub actions to deploy a project to your VPS
Today, I created another workflow file to deploy my side project to the production environment. Here's a simple memo for what I have done.
-
Setup a new domain name on <cloudflare.com>
-
Setup a GitHub runner on the target VPS (this step is not really necessary, I can use an existing runner, but then I'll need to cope with coping built result to the target VPS )
-
Setup the runner as a service, in the runner folder, run
sudo ./svc.sh install
sudo ./svc.sh start
-
-
Create a work-flow file in the
.github/workflow
folder, and seton: workflow_dispatch
-
Manually run the work flow and deploy the project to the target place
-
Setup that project as a systemd service so we can easily restart it in the workflow file
For your reference, here the whole workflow file, and as I mentioned before, this article helped me a lot.
name: Deploy to the production env
# Controls when the workflow will run
on:
workflow_dispatch:
jobs:
deploy:
# Our previously created self-hosted runner
runs-on: [self-hosted, linux, racknerd]
strategy:
matrix:
dotnet: ["8.0.x"]
# A sequence of tasks that will execute as part of the job
steps:
# Checks out repository so our job can access it
- uses: actions/checkout@v4
- name: Setup .NET Core SDK ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v4
with:
ref: ${{ github.event.inputs.tag }}
dotnet-version: ${{ matrix.dotnet-version }}
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Publish
run: dotnet publish -c Release --property:PublishDir=/a-target-folder/HappyNotes.Api
- name: Replace credentials
run: |
pwd
sed -i "s/password-placeholder/${{ secrets.PRODUCTION_MYSQL_PASSWORD }}/g" /a-target-folder/HappyNotes.Api/appsettings.json
sed -i "s/symmetric-security-key-placeholder/${{ secrets.PRODUCTION_SYMMETRIC_SECURITY_KEY }}/g" /a-target-folder/HappyNotes.Api/appsettings.json
sed -i "s/staging-happynotes-api.dev/happynotes-api/g" /a-target-folder/HappyNotes.Api/appsettings.json
- name: Restart the app
run: |
echo $XDG_RUNTIME_DIR
export XDG_RUNTIME_DIR=/run/user/$(id -u)
systemctl --user restart HappyNotes.Api.service