From 3a39f967efd99283bec1a295bfea2e2be50a01e9 Mon Sep 17 00:00:00 2001 From: Max Schmidt Date: Wed, 7 Jun 2023 10:19:45 +0200 Subject: [PATCH] Update environment variables on deployment --- .env | 5 +-- .github/workflows/push.yml | 66 ++++++++++++++++++++++++++--------- docker-compose-dev.yml | 1 - docker-compose-prod.yml | 4 +-- payload/src/payload.config.ts | 2 +- 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/.env b/.env index cb04fe9..083ef1c 100644 --- a/.env +++ b/.env @@ -1,5 +1,6 @@ -ASTRO_URL=http://localhost:3000 -PAYLOAD_URL=http://localhost:3001 +ASTRO_HOST=localhost:3000 +PAYLOAD_HOST=payload:3001 +PAYLOAD_SERVER_URL=http://localhost:3001 PAYLOAD_PORT=3001 PAYLOAD_SECRET=supersecretkey MONGODB_URI=mongodb://payload:test@mongo:27017 diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 426ad53..d198742 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -1,38 +1,72 @@ -name: Clone or pull repository +name: Code Deployment and Environment Setup on: push: branches: - main jobs: build: - name: Run remote SSH command + name: Run remote SSH commands runs-on: ubuntu-latest steps: - - name: Trigger build via ssh - uses: appleboy/ssh-action@master + - name: Clone or pull repository + uses: appleboy/ssh-action@v0.1.10 with: host: ${{ secrets.HOST }} username: ${{ secrets.USER }} key: ${{ secrets.KEY }} + script_stop: false script: | if [ -d ${{ secrets.PATH }} ]; then cd ${{ secrets.PATH }} git pull else - mkdir ${{ secrets.PATH }} + mkdir -p ${{ secrets.PATH }} cd ${{ secrets.PATH }} git clone https://github.com/${{ github.repository }} . - sed -i "s/ASTRO_URL=.*/ASTRO_URL=${{ vars.ASTRO_URL }}/" .env - sed -i "s/PAYLOAD_URL=.*/PAYLOAD_URL=${{ vars.PAYLOAD_URL }}/" .env - sed -i "s/PAYLOAD_PORT=.*/PAYLOAD_PORT=${{ secrets.PAYLOAD_PORT }}/" .env - sed -i "s/PAYLOAD_SECRET=.*/PAYLOAD_SECRET=${{ secrets.PAYLOAD_SECRET }}/" .env - sed -i "s/MONGODB_URI=.*/MONGODB_URI=${{ secrets.MONGODB_URI }}/" .env - sed -i "s/MONGODB_USER=.*/MONGODB_USER=${{ secrets.MONGODB_USER }}/" .env - sed -i "s/MONGODB_PW=.*/MONGODB_PW=${{ secrets.MONGODB_PW }}/" .env - sed -i "s/TOKEN=.*/TOKEN=${{ secrets.TOKEN }}/" .env - sed -i "s/NAME=.*/NAME=${{ vars.NAME }}/" .env - cp .env ./astro/.env fi + + - name: Update environment variables + uses: appleboy/ssh-action@v0.1.10 + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USER }} + key: ${{ secrets.KEY }} + script_stop: false + script: | + # Bash function to replace env variables + replace_env() { + local env_var=$1 + local new_value=$2 + local file=$3 + sed -i "s/${env_var}=.*/${env_var}=${new_value}/" $file + } + + cd ${{ secrets.PATH }} + + # Replace .env with vars + replace_env 'NAME' '${{ vars.NAME }}' .env + replace_env 'ASTRO_HOST' '${{ vars.ASTRO_HOST }}' .env + replace_env 'PAYLOAD_HOST' '${{ vars.PAYLOAD_HOST }}' .env + replace_env 'PAYLOAD_SERVER_URL' '${{ vars.PAYLOAD_SERVER_URL }}' .env + replace_env 'PAYLOAD_PORT' '${{ vars.PAYLOAD_PORT }}' .env + + # Replace .env with secrets + replace_env 'PAYLOAD_SECRET' '${{ secrets.PAYLOAD_SECRET }}' .env + replace_env 'MONGODB_URI' '${{ secrets.MONGODB_URI }}' .env + replace_env 'MONGODB_USER' '${{ secrets.MONGODB_USER }}' .env + replace_env 'MONGODB_PW' '${{ secrets.MONGODB_PW }}' .env + replace_env 'TOKEN' '${{ secrets.TOKEN }}' .env + + # mkdir -p ./astro + # cp .env ./astro/.env + + - name: Start Production Services + uses: appleboy/ssh-action@v0.1.10 + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USER }} + key: ${{ secrets.KEY }} + script: | + cd ${{ secrets.PATH }} yarn prod payload - sleep 10 yarn prod astro diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index b66cedd..e09c316 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -16,7 +16,6 @@ services: volumes: - ./payload/src:/base/src - ./astro/src/types.ts:/types.ts - - ./data/media:/base/dist/media ports: - 3001:3001 diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml index e0da101..f6ae424 100644 --- a/docker-compose-prod.yml +++ b/docker-compose-prod.yml @@ -5,8 +5,6 @@ services: target: prod environment: PAYLOAD_URL: ${PAYLOAD_URL} - volumes: - - ./data/media:/prod/dist/media labels: - "traefik.enable=true" - "traefik.http.routers.${NAME}-astro.rule=Host(`${ASTRO_URL}`)" @@ -37,6 +35,8 @@ services: build: context: payload target: prod + volumes: + - ./data/media:/prod/dist/media labels: - traefik.enable=true - traefik.http.routers.${NAME}-payload.rule=Host(`${PAYLOAD_URL}`) diff --git a/payload/src/payload.config.ts b/payload/src/payload.config.ts index 5e26027..b03bae9 100644 --- a/payload/src/payload.config.ts +++ b/payload/src/payload.config.ts @@ -5,7 +5,7 @@ import Users from "./collections/Users"; import Media from "./collections/Media"; export default buildConfig({ - serverURL: process.env.PAYLOAD_URL, + serverURL: process.env.PAYLOAD_SERVER_URL, admin: { user: Users.slug, },