Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Created October 24, 2022 00:11
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/0275946465b22928efa6b992f3149c15 to your computer and use it in GitHub Desktop.
Save gistlyn/0275946465b22928efa6b992f3149c15 to your computer and use it in GitHub Desktop.
migration-upgrade-blazor
name: Release
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
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 }}
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0'
# Assign environment variables used in subsequent steps
- name: repository name fix
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: Run CI Prebuild Script
env:
deploy_api: ${{ secrets.DEPLOY_API }}
deploy_cdn: ${{ secrets.DEPLOY_CDN }}
run: |
if [ -e ./.deploy/ci.prebuild.sh ]
then
chmod +x ./.deploy/ci.prebuild.sh
./.deploy/ci.prebuild.sh
else
echo "Skipping CI prebuild"
fi
# Publish .NET Project
- name: Publish dotnet project
working-directory: ./MyApp
run: |
dotnet publish -c Release /p:DEPLOY_API=${{ secrets.DEPLOY_API }} /p:DEPLOY_CDN=${{ secrets.DEPLOY_CDN }} /p:APP_TASKS=prerender
# Authenticate, build and push to GitHub Container Registry (ghcr.io)
- 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 API Docker image
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 }}
- name: Create env.deploy_cdn
run: |
echo "deploy_cdn=${{ secrets.DEPLOY_CDN }}" >> $GITHUB_ENV
# Deploy UI to GitHub Pages
- name: Deploy to GitHub Pages
if: ${{ env.deploy_cdn != '' }}
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./MyApp/bin/Release/net6.0/publish/wwwroot
user_name: 'GitHub Action'
user_email: 'action@github.com'
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 }}
# Assign environment variables used in subsequent steps
- 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.1.0
env:
RELEASE_VERSION: ${{ env.TAG_NAME }}
IMAGE_REPO: ${{ env.image_repository_name }}
APP_NAME: ${{ github.event.repository.name }}
HOST_DOMAIN: ${{ secrets.DEPLOY_API }}
LETSENCRYPT_EMAIL: ${{ secrets.LETSENCRYPT_EMAIL }}
DEPLOY_API: ${{ secrets.DEPLOY_API }}
DEPLOY_CDN: ${{ secrets.DEPLOY_CDN }}
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_API }}
username: ${{ secrets.DEPLOY_USERNAME }}
port: 22
key: ${{ secrets.DEPLOY_KEY }}
source: ".deploy/${{ github.event.repository.name }}-docker-compose.yml"
target: "~/"
- 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_API }}
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 --exit-code-from ${{ 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: ${{ secrets.GITHUB_TOKEN }}
USERNAME: ${{ secrets.DEPLOY_USERNAME }}
with:
host: ${{ secrets.DEPLOY_API }}
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 -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment