diff --git a/.deploy/docker-compose-template.yml b/.deploy/docker-compose-template.yml index da35ded..76c92be 100644 --- a/.deploy/docker-compose-template.yml +++ b/.deploy/docker-compose-template.yml @@ -1,21 +1,23 @@ version: "3.9" services: - ${APP_NAME}: - image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION} - restart: always - network_mode: bridge - ports: - - "80" + app: + image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION} + restart: always + ports: + - "8080" + container_name: ${APP_NAME}_app environment: + WS_HOST: ws://${HOST_DOMAIN} + WS_PORT: 80 VIRTUAL_HOST: ${HOST_DOMAIN} - LETSENCRYPT_HOST: ${HOST_DOMAIN} + VIRTUAL_PORT: 8080 # New default ASP.NET port -> https://learn.microsoft.com/en-us/dotnet/core/compatibility/containers/8.0/aspnet-port + LETSENCRYPT_HOST: ${HOST_DOMAIN} LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL} - SERVICESTACK_LICENSE: ${SERVICESTACK_LICENSE} - FORUMS_DB: ${FORUMS_DB} - TECHSTACKS_SMTP_USER: ${TECHSTACKS_SMTP_USER} - TECHSTACKS_SMTP_PASS: ${TECHSTACKS_SMTP_PASS} + LC_ALL: "en_US.UTF-8" + LANG: "en_US.UTF-8" volumes: - - ${APP_NAME}-mydb:/app/App_Data + - app-mydb:/app/App_Data + ${APP_NAME}-migration: image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION} restart: "no" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 10d4b29..fe46d25 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -34,7 +34,7 @@ jobs: 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 @@ -48,7 +48,7 @@ jobs: 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@v2 with: @@ -74,14 +74,9 @@ jobs: working-directory: ./TechStacks # Build and push new docker image, skip for manual redeploy other than 'latest' - - name: Build and push Docker images - uses: docker/build-push-action@v4 - 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: 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 deploy_via_ssh: needs: push_to_registry @@ -101,8 +96,6 @@ jobs: - name: repository name fix and env run: | echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV - echo "domain=${{ secrets.DEPLOY_HOST }}" >> $GITHUB_ENV - echo "letsencrypt_email=${{ secrets.LETSENCRYPT_EMAIL }}" >> $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 @@ -111,32 +104,32 @@ jobs: 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: ${{ env.domain }} - LETSENCRYPT_EMAIL: ${{ env.letsencrypt_email }} - FORUMS_DB: ${{ secrets.FORUMS_DB }} - TECHSTACKS_SMTP_USER: ${{ secrets.TECHSTACKS_SMTP_USER }} - TECHSTACKS_SMTP_PASS: ${{ secrets.TECHSTACKS_SMTP_PASS }} - with: - input: .deploy/docker-compose-template.yml - output: .deploy/${{ github.event.repository.name }}-docker-compose.yml + - name: Create .env file + 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 + echo "FORUMS_DB=${{ secrets.FORUMS_DB }}" >> .deploy/.env + echo "TECHSTACKS_SMTP_USER=${{ secrets.TECHSTACKS_SMTP_USER }}" >> .deploy/.env + echo "TECHSTACKS_SMTP_PASS=${{ secrets.TECHSTACKS_SMTP_PASS }}" >> .deploy/.env # Copy only the docker-compose.yml to remote server home folder - - name: copy compose file via scp + - name: copy files to target server 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: "~/" + strip_components: 2 + source: "./.deploy/docker-compose.yml,./.deploy/.env" + target: "~/.deploy/${{ github.event.repository.name }}/" + - name: Run remote db migrations uses: appleboy/ssh-action@v0.1.5 env: @@ -149,12 +142,16 @@ jobs: port: 22 envs: APPTOKEN,USERNAME script: | + set -e 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 + 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/ssh-action@v0.1.5 env: APPTOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -167,5 +164,6 @@ jobs: 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 \ No newline at end of file + cd ~/.deploy/${{ github.event.repository.name }} + docker compose pull + docker compose up app -d