-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
394 additions
and
129 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,155 +1,111 @@ | ||
name: Release | ||
name: Release and Deploy | ||
permissions: | ||
packages: write | ||
contents: 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 | ||
|
||
# Only update envs here if you need to change them for this workflow | ||
env: | ||
DOCKER_BUILDKIT: 1 | ||
KAMAL_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} | ||
KAMAL_REGISTRY_USERNAME: ${{ github.actor }} | ||
|
||
# Standard steps for building and deploying a .NET app via Kamal | ||
jobs: | ||
push_to_registry: | ||
runs-on: ubuntu-22.04 | ||
if: ${{ github.event.workflow_run.conclusion != 'failure' }} | ||
build-and-deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# Checkout latest or specific tag | ||
- name: checkout | ||
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }} | ||
uses: actions/checkout@v3 | ||
- name: checkout tag | ||
if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }} | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
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 | ||
|
||
- name: Set up environment variables | ||
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; | ||
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | ||
echo "repository_name=$(echo ${{ github.repository }} | cut -d '/' -f 2)" >> $GITHUB_ENV | ||
if find . -maxdepth 2 -type f -name "Configure.Db.Migrations.cs" | grep -q .; then | ||
echo "HAS_MIGRATIONS=true" >> $GITHUB_ENV | ||
else | ||
echo "HAS_MIGRATIONS=false" >> $GITHUB_ENV | ||
fi | ||
if [ -n "${{ secrets.APPSETTINGS_PATCH }}" ]; then | ||
echo "HAS_APPSETTINGS_PATCH=true" >> $GITHUB_ENV | ||
else | ||
echo "HAS_APPSETTINGS_PATCH=false" >> $GITHUB_ENV | ||
fi | ||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v2 | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
|
||
- name: Setup dotnet | ||
username: ${{ env.KAMAL_REGISTRY_USERNAME }} | ||
password: ${{ env.KAMAL_REGISTRY_PASSWORD }} | ||
|
||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: '8.*' | ||
|
||
# Build and push new docker image, skip for manual redeploy other than 'latest' | ||
- name: Build and push Docker image | ||
run: | | ||
dotnet publish --os linux --arch x64 -c Release -p:ContainerRepository=${{ env.image_repository_name }} -p:ContainerRegistry=ghcr.io -p:ContainerImageTags=${{ env.TAG_NAME }} -p:ContainerPort=80 | ||
dotnet-version: '8.0' | ||
|
||
deploy_via_ssh: | ||
needs: push_to_registry | ||
runs-on: ubuntu-22.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@v3 | ||
- name: checkout tag | ||
if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }} | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: refs/tags/${{ github.event.inputs.version }} | ||
- name: Install x tool | ||
run: dotnet tool install -g x | ||
|
||
- name: repository name fix and env | ||
- name: Apply Production AppSettings | ||
if: env.HAS_APPSETTINGS_PATCH == 'true' | ||
working-directory: ./BlazorGalleryWasm | ||
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; | ||
- name: Create .env file | ||
cat <<EOF >> appsettings.json.patch | ||
${{ secrets.APPSETTINGS_PATCH }} | ||
EOF | ||
x patch appsettings.json.patch | ||
- name: Build and push Docker image | ||
run: | | ||
echo "Generating .env file" | ||
echo "# Autogenerated .env file" > .deploy/.env | ||
echo "HOST_DOMAIN=${{ secrets.DEPLOY_HOST }}" >> .deploy/.env | ||
echo "LETSENCRYPT_EMAIL=${{ secrets.LETSENCRYPT_EMAIL }}" >> .deploy/.env | ||
echo "APP_NAME=${{ github.event.repository.name }}" >> .deploy/.env | ||
echo "IMAGE_REPO=${{ env.image_repository_name }}" >> .deploy/.env | ||
echo "RELEASE_VERSION=${{ env.TAG_NAME }}" >> .deploy/.env | ||
# Copy only the docker-compose.yml to remote server home folder | ||
- name: copy files to target server via scp | ||
uses: appleboy/[email protected] | ||
dotnet publish --os linux --arch x64 -c Release -p:ContainerRepository=${{ env.image_repository_name }} -p:ContainerRegistry=ghcr.io -p:ContainerImageTags=latest -p:ContainerPort=80 | ||
- name: Set up SSH key | ||
uses: webfactory/[email protected] | ||
with: | ||
host: ${{ secrets.DEPLOY_HOST }} | ||
username: ${{ secrets.DEPLOY_USERNAME }} | ||
port: 22 | ||
key: ${{ secrets.DEPLOY_KEY }} | ||
strip_components: 2 | ||
source: "./.deploy/docker-compose.yml,./.deploy/.env" | ||
target: "~/.deploy/${{ github.event.repository.name }}/" | ||
|
||
- name: Run remote db migrations | ||
uses: appleboy/[email protected] | ||
env: | ||
APPTOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
USERNAME: ${{ secrets.DEPLOY_USERNAME }} | ||
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | ||
|
||
- name: Setup Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
host: ${{ secrets.DEPLOY_HOST }} | ||
username: ${{ secrets.DEPLOY_USERNAME }} | ||
key: ${{ secrets.DEPLOY_KEY }} | ||
port: 22 | ||
envs: APPTOKEN,USERNAME | ||
script: | | ||
set -e | ||
echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin | ||
cd ~/.deploy/${{ github.event.repository.name }} | ||
docker compose pull | ||
export APP_ID=$(docker compose run --entrypoint "id -u" --rm app) | ||
docker compose run --entrypoint "chown $APP_ID:$APP_ID /app/App_Data" --user root --rm app | ||
docker compose up app-migration --exit-code-from app-migration | ||
# Deploy Docker image with your application using `docker compose up` remotely | ||
- name: remote docker-compose up via ssh | ||
uses: appleboy/[email protected] | ||
env: | ||
APPTOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
USERNAME: ${{ secrets.DEPLOY_USERNAME }} | ||
ruby-version: 3.3.0 | ||
bundler-cache: true | ||
|
||
- name: Install Kamal | ||
run: gem install kamal -v 2.2.2 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
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 | ||
cd ~/.deploy/${{ github.event.repository.name }} | ||
docker compose pull | ||
docker compose up app -d | ||
driver-opts: image=moby/buildkit:master | ||
|
||
- name: Kamal bootstrap | ||
run: kamal server bootstrap | ||
|
||
- name: Check if first run and execute kamal app boot if necessary | ||
run: | | ||
FIRST_RUN_FILE=".${{ env.repository_name }}" | ||
if ! kamal server exec --no-interactive -q "test -f $FIRST_RUN_FILE"; then | ||
kamal server exec --no-interactive -q "touch $FIRST_RUN_FILE" || true | ||
kamal deploy -q -P --version latest > /dev/null 2>&1 || true | ||
else | ||
echo "Not first run, skipping kamal app boot" | ||
fi | ||
- name: Ensure file permissions | ||
run: kamal server exec --no-interactive "mkdir -p /opt/docker/${{ env.repository_name }}/App_Data && chown -R 1654:1654 /opt/docker/${{ env.repository_name }}" | ||
|
||
- name: Migration | ||
if: env.HAS_MIGRATIONS == 'true' | ||
run: kamal app exec --no-reuse --no-interactive --version=latest "--AppTasks=migrate" | ||
|
||
- name: Deploy with Kamal | ||
run: | | ||
kamal lock release -v | ||
kamal deploy -P --version latest |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
echo "Docker set up on $KAMAL_HOSTS..." |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/sh | ||
|
||
# A sample post-deploy hook | ||
# | ||
# These environment variables are available: | ||
# KAMAL_RECORDED_AT | ||
# KAMAL_PERFORMER | ||
# KAMAL_VERSION | ||
# KAMAL_HOSTS | ||
# KAMAL_ROLE (if set) | ||
# KAMAL_DESTINATION (if set) | ||
# KAMAL_RUNTIME | ||
|
||
echo "$KAMAL_PERFORMER deployed $KAMAL_VERSION to $KAMAL_DESTINATION in $KAMAL_RUNTIME seconds" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
echo "Rebooted kamal-proxy on $KAMAL_HOSTS" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/bin/sh | ||
|
||
# A sample pre-build hook | ||
# | ||
# Checks: | ||
# 1. We have a clean checkout | ||
# 2. A remote is configured | ||
# 3. The branch has been pushed to the remote | ||
# 4. The version we are deploying matches the remote | ||
# | ||
# These environment variables are available: | ||
# KAMAL_RECORDED_AT | ||
# KAMAL_PERFORMER | ||
# KAMAL_VERSION | ||
# KAMAL_HOSTS | ||
# KAMAL_ROLE (if set) | ||
# KAMAL_DESTINATION (if set) | ||
|
||
if [ -n "$(git status --porcelain)" ]; then | ||
echo "Git checkout is not clean, aborting..." >&2 | ||
git status --porcelain >&2 | ||
exit 1 | ||
fi | ||
|
||
first_remote=$(git remote) | ||
|
||
if [ -z "$first_remote" ]; then | ||
echo "No git remote set, aborting..." >&2 | ||
exit 1 | ||
fi | ||
|
||
current_branch=$(git branch --show-current) | ||
|
||
if [ -z "$current_branch" ]; then | ||
echo "Not on a git branch, aborting..." >&2 | ||
exit 1 | ||
fi | ||
|
||
remote_head=$(git ls-remote $first_remote --tags $current_branch | cut -f1) | ||
|
||
if [ -z "$remote_head" ]; then | ||
echo "Branch not pushed to remote, aborting..." >&2 | ||
exit 1 | ||
fi | ||
|
||
if [ "$KAMAL_VERSION" != "$remote_head" ]; then | ||
echo "Version ($KAMAL_VERSION) does not match remote HEAD ($remote_head), aborting..." >&2 | ||
exit 1 | ||
fi | ||
|
||
exit 0 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# A sample pre-connect check | ||
# | ||
# Warms DNS before connecting to hosts in parallel | ||
# | ||
# These environment variables are available: | ||
# KAMAL_RECORDED_AT | ||
# KAMAL_PERFORMER | ||
# KAMAL_VERSION | ||
# KAMAL_HOSTS | ||
# KAMAL_ROLE (if set) | ||
# KAMAL_DESTINATION (if set) | ||
# KAMAL_RUNTIME | ||
|
||
hosts = ENV["KAMAL_HOSTS"].split(",") | ||
results = nil | ||
max = 3 | ||
|
||
elapsed = Benchmark.realtime do | ||
results = hosts.map do |host| | ||
Thread.new do | ||
tries = 1 | ||
|
||
begin | ||
Socket.getaddrinfo(host, 0, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) | ||
rescue SocketError | ||
if tries < max | ||
puts "Retrying DNS warmup: #{host}" | ||
tries += 1 | ||
sleep rand | ||
retry | ||
else | ||
puts "DNS warmup failed: #{host}" | ||
host | ||
end | ||
end | ||
|
||
tries | ||
end | ||
end.map(&:value) | ||
end | ||
|
||
retries = results.sum - hosts.size | ||
nopes = results.count { |r| r == max } | ||
|
||
puts "Prewarmed %d DNS lookups in %.2f sec: %d retries, %d failures" % [ hosts.size, elapsed, retries, nopes ] |
Oops, something went wrong.