|
name: Release |
|
permissions: |
|
packages: write |
|
on: |
|
release: |
|
types: [published] |
|
jobs: |
|
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@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 |
|
run: echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
|
|
|
- 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; |
|
|
|
# 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 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-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@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; |
|
|
|
- name: docker-compose file prep |
|
uses: danielr1996/envsubst-action@1.0.0 |
|
env: |
|
APP_NAME: ${{ github.event.repository.name }} |
|
RELEASE_VERSION: ${{ env.TAG_NAME }} |
|
IMAGE_REPO: ${{ env.image_repository_name }} |
|
HOST_DOMAIN: ${{ secrets.DEPLOY_HOST }} |
|
LETSENCRYPT_EMAIL: ${{ secrets.LETSENCRYPT_EMAIL }} |
|
with: |
|
input: .deploy/docker-compose-template.yml |
|
output: .deploy/${{ github.event.repository.name }}-docker-compose.yml |
|
|
|
- name: copy compose file via scp |
|
uses: appleboy/scp-action@v0.1.3 |
|
with: |
|
host: ${{ secrets.DEPLOY_HOST }} |
|
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.5 |
|
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 |
|
|
|
- name: remote docker-compose up via ssh |
|
uses: appleboy/ssh-action@v0.1.5 |
|
env: |
|
APPTOKEN: ${{ secrets.GITHUB_TOKEN }} |
|
USERNAME: ${{ secrets.DEPLOY_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 |