Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active August 31, 2022 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistlyn/99c0174ff43866171841105165a389b9 to your computer and use it in GitHub Desktop.
Save gistlyn/99c0174ff43866171841105165a389b9 to your computer and use it in GitHub Desktop.
litestream-azure
version: "3.9"
services:
${APP_NAME}:
image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION}
depends_on:
${APP_NAME}-litestream:
condition: service_healthy
restart: always
network_mode: bridge
ports:
- "80"
environment:
VIRTUAL_HOST: ${HOST_DOMAIN}
LETSENCRYPT_HOST: ${HOST_DOMAIN}
LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL}
volumes:
- ${APP_NAME}-mydb:/app/App_Data
${APP_NAME}-litestream:
image: litestream/litestream
entrypoint: ["/bin/sh", "-c"]
# Timeout of health check will need to depend on size of db, and speed of network to host.
healthcheck:
test: /usr/local/bin/litestream restore -if-db-not-exists -if-replica-exists -o /data/MyApp.sqlite abs://${AZURE_STORAGEACCOUNT}@${AZURE_CONTAINER}/MyApp.sqlite
timeout: 10m
retries: 1
command:
- /usr/local/bin/litestream replicate /data/MyApp.sqlite abs://${AZURE_STORAGEACCOUNT}@${AZURE_CONTAINER}/MyApp.sqlite
environment:
LITESTREAM_AZURE_ACCOUNT_KEY: ${AZURE_ACCOUNT_KEY}
volumes:
- ${APP_NAME}-mydb:/data
${APP_NAME}-migration:
image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION}
restart: "no"
profiles:
- migration
command: --AppTasks=migrate
volumes:
- ${APP_NAME}-mydb:/app/App_Data
volumes:
${APP_NAME}-mydb:
name: Release
permissions:
packages: write
on:
# Triggered on new GitHub Release
release:
types: [published]
# Triggered on every successful Build action
workflow_run:
workflows: ["Build"]
branches: [main,master]
types:
- completed
# Manual trigger for rollback to specific release or redeploy latest
workflow_dispatch:
inputs:
version:
default: latest
description: Tag you want to release.
required: true
jobs:
push_to_registry:
runs-on: ubuntu-20.04
if: ${{ github.event.workflow_run.conclusion != 'failure' }}
steps:
# Checkout latest or specific tag
- name: checkout
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
uses: actions/checkout@v2
- name: checkout tag
if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }}
uses: actions/checkout@v2
with:
ref: refs/tags/${{ github.event.inputs.version }}
# Assign environment variables used in subsequent steps
- name: Env variable assignment
run: echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
# TAG_NAME defaults to 'latest' if not a release or manual deployment
- name: Assign version
run: |
echo "TAG_NAME=latest" >> $GITHUB_ENV
if [ "${{ github.event.release.tag_name }}" != "" ]; then
echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
fi;
if [ "${{ github.event.inputs.version }}" != "" ]; then
echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV
fi;
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build and push new docker image, skip for manual redeploy other than 'latest'
- name: Build and push Docker images
uses: docker/build-push-action@v2.2.2
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
with:
file: Dockerfile
context: .
push: true
tags: ghcr.io/${{ env.image_repository_name }}:${{ env.TAG_NAME }}
deploy_via_ssh:
needs: push_to_registry
runs-on: ubuntu-20.04
if: ${{ github.event.workflow_run.conclusion != 'failure' }}
steps:
# Checkout latest or specific tag
- name: checkout
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
uses: actions/checkout@v2
- name: checkout tag
if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }}
uses: actions/checkout@v2
with:
ref: refs/tags/${{ github.event.inputs.version }}
- name: repository name fix and env
run: |
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
echo "TAG_NAME=latest" >> $GITHUB_ENV
if [ "${{ github.event.release.tag_name }}" != "" ]; then
echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
fi;
if [ "${{ github.event.inputs.version }}" != "" ]; then
echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV
fi;
# Populate docker-compose.yml with variables from build process, including TAG_NAME.
- name: docker-compose file prep
uses: danielr1996/envsubst-action@1.0.0
env:
RELEASE_VERSION: ${{ env.TAG_NAME }}
IMAGE_REPO: ${{ env.image_repository_name }}
APP_NAME: ${{ github.event.repository.name }}
HOST_DOMAIN: ${{ secrets.DEPLOY_HOST}}
LETSENCRYPT_EMAIL: ${{ secrets.LETSENCRYPT_EMAIL }}
AZURE_ACCOUNT_KEY: ${{ secrets.AZURE_ACCOUNT_KEY }}
AZURE_STORAGEACCOUNT: ${{ secrets.AZURE_STORAGEACCOUNT }}
AZURE_CONTAINER: ${{ secrets.AZURE_CONTAINER }}
with:
input: .deploy/docker-compose-template.yml
output: .deploy/${{ github.event.repository.name }}-docker-compose.yml
# Copy only the docker-compose.yml to remote server home folder
- name: copy compose file via scp
uses: appleboy/scp-action@v0.1.1
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USERNAME }}
port: ${{ secrets.DEPLOY_PORT }}
key: ${{ secrets.DEPLOY_KEY }}
source: ".deploy/${{ github.event.repository.name }}-docker-compose.yml"
target: "~/"
- name: Set the value
run: |
echo "GH_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV
echo "USERNAME=${{ secrets.DEPLOY_USERNAME }}" >> $GITHUB_ENV
- name: Run remote db migrations
uses: appleboy/ssh-action@v0.1.4
env:
APPTOKEN: ${{ secrets.GITHUB_TOKEN }}
USERNAME: ${{ secrets.DEPLOY_USERNAME }}
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USERNAME }}
key: ${{ secrets.DEPLOY_KEY }}
port: 22
envs: APPTOKEN,USERNAME
script: |
echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up ${{ github.event.repository.name }}-migration
# Deploy Docker image with ServiceStack application using `docker compose up` remotely
- name: remote docker-compose up via ssh
uses: appleboy/ssh-action@v0.1.4
env:
APPTOKEN: ${{ env.GH_TOKEN }}
USERNAME: ${{ env.USERNAME }}
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USERNAME }}
key: ${{ secrets.DEPLOY_KEY }}
port: ${{ secrets.DEPLOY_PORT }}
envs: APPTOKEN,USERNAME
script: |
echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up -d
dotnet add MyApp/MyApp.csproj package ServiceStack.OrmLite.Sqlite
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
[assembly: HostingStartup(typeof(MyApp.ConfigureDb))]
namespace MyApp
{
// Example Data Model
// public class MyTable
// {
// [AutoIncrement]
// public int Id { get; set; }
// public string Name { get; set; }
// }
public class ConfigureDb : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) => {
services.AddSingleton<IDbConnectionFactory>(new OrmLiteConnectionFactory(
context.Configuration.GetConnectionString("DefaultConnection")
?? "App_Data/MyApp.sqlite",
SqliteDialect.Provider));
})
.ConfigureAppHost(afterConfigure:appHost => {
appHost.ScriptContext.ScriptMethods.Add(new DbScriptsAsync());
// Create non-existing Table and add Seed Data Example
// using var db = appHost.Resolve<IDbConnectionFactory>().Open();
// if (db.CreateTableIfNotExists<MyTable>())
// {
// db.Insert(new MyTable { Name = "Seed Data for new MyTable" });
// }
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment