Build and deploy to VPS #85
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
# Copy confg file | |
- name: Copy config file | |
run: | | |
ssh vps << EOF | |
cp /srv/www/aoe2deapi/appsettings.Production.json /srv/www/aoe2deapi/publish/appsettings.Production.json | |
EOF | |
# 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__SeqSink__Args__apiKey="$seqApiKey" \ | |
-e SteamWebApiKey="$steamWebApiKey" \ | |
-v /var/log/www/aoe2deapi:/app/logs \ | |
-v /srv/www/aoe2deapi/data:/app/data \ | |
--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 | |