Skip to content

Build and deploy to VPS #72

Build and deploy to VPS

Build and deploy to VPS #72

Workflow file for this run

name: Build and deploy to VPS
on:
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
env:
projectDir: AoE2DELobbyBrowser.WebApi
containerName: aoe2deapi
targetFolder: /srv/www/aoe2deapi
# Use the same ssh-agent socket value across all jobs
# Useful when a GH action is using SSH behind-the-scenes
# SSH_AUTH_SOCK: /tmp/ssh_agent.sock
steps:
- uses: actions/checkout@v4
- name: Set up .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
# build and publish
- name: Build and publish
run: dotnet publish "$projectDir/AoE2DELobbyBrowser.WebApi.csproj" -c Release -o "$projectDir/publish" -r linux-x64
- name: Configure SSH
run: |
mkdir -p ~/.ssh/
chmod 700 ~/.ssh
echo "$SSH_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
cat >>~/.ssh/config <<END
Host vps
HostName $SSH_HOST
User $SSH_USER
IdentityFile ~/.ssh/id_ed25519
StrictHostKeyChecking no
IdentitiesOnly yes
PasswordAuthentication no
END
env:
SSH_USER: ${{ secrets.SERVER_USERNAME }}
SSH_KEY: ${{ secrets.SERVER_KEY }}
SSH_HOST: ${{ secrets.SERVER_HOST }}
# Stop container
- name: Stop container
run: |
ssh vps << EOF
echo "Stopping and removing container $containerName"
docker container stop $containerName
docker container rm $containerName
echo "Container $containerName removed"
EOF
# Transfer Dockerfile
- name: Copy Dockerfile
run: |
ssh vps "mkdir -p /srv/www/aoe2deapi"
scp "$projectDir/Dockerfile" vps:"/srv/www/aoe2deapi"
# Transfer publish folder
- name: Transfer publish folder
run: |
ssh vps "mkdir -p /srv/www/aoe2deapi/publish"
rsync -rzvh --delete "$projectDir/publish" vps:"/srv/www/aoe2deapi"
# Start container
- name: Start container
run: |
ssh vps << EOF
if docker inspect "$containerName" > /dev/null 2>&1; then
echo "The container $containerName exists. Start container."
docker container start "$containerName"
else
echo "The container $containerName does not exist. Build image and run container."
cd /srv/www/aoe2deapi
docker build -t aoe2delobbybrowser .
docker run -d \
-p 127.0.0.1:5100:8080 \
-e ASPNETCORE_URLS="http://*:8080" \
-e Serilog__WriteTo__1__Args__apiKey="$seqApiKey" \
-e SteamWebApiKey="$steamWebApiKey" \
-v /var/log/www/aoe2deapi:/app/logs \
--network=seq-network \
--restart on-failure:10 \
--name aoe2deapi aoe2delobbybrowser
fi
EOF
env:
seqApiKey: ${{ secrets.SEQ_API_KEY }}
steamWebApiKey: ${{ secrets.SteamWebApiKey }}
# Remove shh files
- name: Remove ssh files
run: |
rm ~/.ssh/id_ed25519
rm ~/.ssh/config