Skip to content

Commit 55e868b

Browse files
committed
Switch to docker compose deployment.
1 parent 9569e75 commit 55e868b

File tree

4 files changed

+148
-156
lines changed

4 files changed

+148
-156
lines changed

.deploy/docker-compose-template.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: "3.9"
2+
services:
3+
${APP_NAME}:
4+
image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION}
5+
restart: always
6+
network_mode: bridge
7+
ports:
8+
- "80"
9+
environment:
10+
VIRTUAL_HOST: ${HOST_DOMAIN}
11+
LETSENCRYPT_HOST: ${HOST_DOMAIN}
12+
LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL}
13+
SERVICESTACK_LICENSE: ${SERVICESTACK_LICENSE}
14+
FORUMS_DB: ${FORUMS_DB}
15+
TECHSTACKS_SMTP_USER: ${TECHSTACKS_SMTP_USER}
16+
TECHSTACKS_SMTP_PASS: ${TECHSTACKS_SMTP_PASS}
17+
volumes:
18+
- ${APP_NAME}-mydb:/app/App_Data
19+
${APP_NAME}-migration:
20+
image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION}
21+
restart: "no"
22+
profiles:
23+
- migration
24+
command: --AppTasks=migrate
25+
volumes:
26+
- ${APP_NAME}-mydb:/app/App_Data
27+
28+
volumes:
29+
${APP_NAME}-mydb:

.github/workflows/release.yml

Lines changed: 119 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,151 @@
11
name: Release
2+
permissions:
3+
packages: write
4+
contents: write
25
on:
6+
# Triggered on new GitHub Release
37
release:
48
types: [published]
9+
# Triggered on every successful Build action
10+
workflow_run:
11+
workflows: ["Build"]
12+
branches: [main,master]
13+
types:
14+
- completed
15+
# Manual trigger for rollback to specific release or redeploy latest
16+
workflow_dispatch:
17+
inputs:
18+
version:
19+
default: latest
20+
description: Tag you want to release.
21+
required: true
22+
523
jobs:
6-
push_to_ecr:
7-
runs-on: ubuntu-20.04
24+
push_to_registry:
25+
runs-on: ubuntu-22.04
26+
if: ${{ github.event.workflow_run.conclusion != 'failure' }}
827
steps:
9-
- name: Checkout
10-
uses: actions/checkout@v2
28+
# Checkout latest or specific tag
29+
- name: checkout
30+
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
31+
uses: actions/checkout@v3
32+
- name: checkout tag
33+
if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }}
34+
uses: actions/checkout@v3
35+
with:
36+
ref: refs/tags/${{ github.event.inputs.version }}
1137

12-
- name: repository name fix
38+
# Assign environment variables used in subsequent steps
39+
- name: Env variable assignment
1340
run: echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
41+
# TAG_NAME defaults to 'latest' if not a release or manual deployment
42+
- name: Assign version
43+
run: |
44+
echo "TAG_NAME=latest" >> $GITHUB_ENV
45+
if [ "${{ github.event.release.tag_name }}" != "" ]; then
46+
echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
47+
fi;
48+
if [ "${{ github.event.inputs.version }}" != "" ]; then
49+
echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV
50+
fi;
1451
15-
- name: Configure AWS credentials
16-
uses: aws-actions/configure-aws-credentials@v1
52+
- name: Login to GitHub Container Registry
53+
uses: docker/login-action@v2
1754
with:
18-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
19-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
20-
aws-region: ${{ secrets.AWS_REGION }}
21-
22-
- name: Login to Amazon ECR
23-
id: login_ecr
24-
uses: aws-actions/amazon-ecr-login@v1
25-
26-
- name: Create ECR repo if not exists.
27-
env:
28-
ECR_REPOSITORY: ${{ env.image_repository_name }}
29-
run: aws ecr describe-repositories --repository-names ${ECR_REPOSITORY} || aws ecr create-repository --repository-name ${ECR_REPOSITORY}
55+
registry: ghcr.io
56+
username: ${{ github.actor }}
57+
password: ${{ secrets.GITHUB_TOKEN }}
3058

31-
- name: Build and push to ECR
32-
id: push_image_to_ecr
33-
uses: docker/build-push-action@v2.2.2
59+
# Build and push new docker image, skip for manual redeploy other than 'latest'
60+
- name: Build and push Docker images
61+
uses: docker/build-push-action@v3
62+
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
3463
with:
3564
file: Dockerfile
3665
context: .
3766
push: true
38-
tags: ${{ steps.login_ecr.outputs.registry }}/${{ env.image_repository_name }}:${{ github.event.release.tag_name }}
67+
tags: ghcr.io/${{ env.image_repository_name }}:${{ env.TAG_NAME }}
3968

40-
deploy_ecs:
41-
needs: push_to_ecr
42-
runs-on: ubuntu-20.04
69+
deploy_via_ssh:
70+
needs: push_to_registry
71+
runs-on: ubuntu-22.04
72+
if: ${{ github.event.workflow_run.conclusion != 'failure' }}
4373
steps:
74+
# Checkout latest or specific tag
4475
- name: checkout
45-
uses: actions/checkout@v2
46-
47-
- name: Configure AWS credentials
48-
uses: aws-actions/configure-aws-credentials@v1
76+
if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
77+
uses: actions/checkout@v3
78+
- name: checkout tag
79+
if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }}
80+
uses: actions/checkout@v3
4981
with:
50-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
51-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
52-
aws-region: ${{ secrets.AWS_REGION }}
53-
54-
- name: Login to Amazon ECR
55-
id: login_ecr
56-
uses: aws-actions/amazon-ecr-login@v1
82+
ref: refs/tags/${{ github.event.inputs.version }}
5783

58-
- name: Repository name fix and env values setup
84+
- name: repository name fix and env
5985
run: |
6086
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
61-
echo "domain=${{ secrets.HOST_DOMAIN }}" >> $GITHUB_ENV
87+
echo "domain=${{ secrets.DEPLOY_HOST }}" >> $GITHUB_ENV
6288
echo "letsencrypt_email=${{ secrets.LETSENCRYPT_EMAIL }}" >> $GITHUB_ENV
63-
echo "app_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]' | cut -d'/' -f2)" >> $GITHUB_ENV
64-
echo "cluster_name=${{ secrets.AWS_ECS_CLUSTER }}" >> $GITHUB_ENV
65-
echo "image_url=${{ steps.login_ecr.outputs.registry }}/$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]'):${{ github.event.release.tag_name }}" >> $GITHUB_ENV
66-
echo "aws_region=${{ secrets.AWS_REGION }}" >> $GITHUB_ENV
89+
echo "TAG_NAME=latest" >> $GITHUB_ENV
90+
if [ "${{ github.event.release.tag_name }}" != "" ]; then
91+
echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
92+
fi;
93+
if [ "${{ github.event.inputs.version }}" != "" ]; then
94+
echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV
95+
fi;
6796
68-
- name: Populate task definition template
97+
# Populate docker-compose.yml with variables from build process, including TAG_NAME.
98+
- name: docker-compose file prep
6999
uses: danielr1996/envsubst-action@1.0.0
70100
env:
71-
RELEASE_VERSION: ${{ github.event.release.tag_name }}
72-
APP_NAME: ${{ env.app_name }}
73-
IMAGE_URL: ${{ env.image_url }}
101+
RELEASE_VERSION: ${{ env.TAG_NAME }}
102+
IMAGE_REPO: ${{ env.image_repository_name }}
103+
APP_NAME: ${{ github.event.repository.name }}
74104
HOST_DOMAIN: ${{ env.domain }}
75105
LETSENCRYPT_EMAIL: ${{ env.letsencrypt_email }}
76-
AWS_REGION: ${{ env.aws_region }}
77-
CLUSTER_NAME: ${{ env.cluster_name }}
78106
with:
79-
input: deploy/task-definition-template.json
80-
output: deploy/task-definition.json
107+
input: .deploy/docker-compose-template.yml
108+
output: .deploy/${{ github.event.repository.name }}-docker-compose.yml
81109

82-
- name: Create task definition if doesn't exist
83-
run: aws ecs describe-task-definition --task-definition ${{ env.app_name }} || aws ecs register-task-definition --cli-input-json file://deploy/task-definition.json
84-
85-
- name: Create ECS Service if not exists.
86-
run: aws ecs describe-services --cluster ${{ env.cluster_name }} --services ${{ env.app_name }} | jq '.services[0]' -e || aws ecs create-service --cluster ${{ env.cluster_name }} --service-name ${{ env.app_name }} --task-definition ${{ env.app_name }} --desired-count 1
110+
# Copy only the docker-compose.yml to remote server home folder
111+
- name: copy compose file via scp
112+
uses: appleboy/scp-action@v0.1.3
113+
with:
114+
host: ${{ secrets.DEPLOY_HOST }}
115+
username: ${{ secrets.DEPLOY_USERNAME }}
116+
port: 22
117+
key: ${{ secrets.DEPLOY_KEY }}
118+
source: ".deploy/${{ github.event.repository.name }}-docker-compose.yml"
119+
target: "~/"
120+
- name: Run remote db migrations
121+
uses: appleboy/ssh-action@v0.1.5
122+
env:
123+
APPTOKEN: ${{ secrets.GITHUB_TOKEN }}
124+
USERNAME: ${{ secrets.DEPLOY_USERNAME }}
125+
with:
126+
host: ${{ secrets.DEPLOY_HOST }}
127+
username: ${{ secrets.DEPLOY_USERNAME }}
128+
key: ${{ secrets.DEPLOY_KEY }}
129+
port: 22
130+
envs: APPTOKEN,USERNAME
131+
script: |
132+
echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
133+
docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
134+
docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up ${{ github.event.repository.name }}-migration
87135
88-
- name: Deploy new revision of the task definition
89-
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
136+
# Deploy Docker image with ServiceStack application using `docker compose up` remotely
137+
- name: remote docker-compose up via ssh
138+
uses: appleboy/ssh-action@v0.1.5
139+
env:
140+
APPTOKEN: ${{ secrets.GITHUB_TOKEN }}
141+
USERNAME: ${{ secrets.DEPLOY_USERNAME }}
90142
with:
91-
task-definition: deploy/task-definition.json
92-
service: ${{ env.app_name }}
93-
cluster: ${{ env.cluster_name }}
94-
force-new-deployment: true
143+
host: ${{ secrets.DEPLOY_HOST }}
144+
username: ${{ secrets.DEPLOY_USERNAME }}
145+
key: ${{ secrets.DEPLOY_KEY }}
146+
port: 22
147+
envs: APPTOKEN,USERNAME
148+
script: |
149+
echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
150+
docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
151+
docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up -d

deploy/nginx-proxy-compose.yml

Lines changed: 0 additions & 45 deletions
This file was deleted.

deploy/task-definition-template.json

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)