From 39a1bf8818fca2e6bcb69043ac164491650e1ce2 Mon Sep 17 00:00:00 2001 From: olha-dev-fullstack Date: Thu, 27 Feb 2025 17:38:29 +0200 Subject: [PATCH 1/3] add reusable workflow --- .github/workflows/deployment.yml | 64 ++++++++++++++++++++++++++++++++ .github/workflows/dev.yml | 57 ++++------------------------ .github/workflows/production.yml | 1 + 3 files changed, 72 insertions(+), 50 deletions(-) create mode 100644 .github/workflows/deployment.yml diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml new file mode 100644 index 0000000..462ec91 --- /dev/null +++ b/.github/workflows/deployment.yml @@ -0,0 +1,64 @@ +name: Deployment flow +on: + workflow_call: + inputs: + environment: + type: string + required: true + runner: + type: string + required: true + env_file: + type: string + required: true +jobs: + checkout: + runs-on: "self-hosted,${{inputs.runner}}" + environment: ${{inputs.environment}} + steps: + - name: Get code + uses: actions/checkout@v4 + - name: Cache dependencies + id: cache + uses: actions/cache@v4 + with: + path: node_modules + key: deps-node-modules-${{ hashFiles('**/package-lock.json') }} + - name: Install dependencies + if: steps.cache.outputs.cache-hit != 'true' + run: npm ci + create_env: + needs: checkout + runs-on: "self-hosted,${{inputs.runner}}" + environment: ${{inputs.environment}} + steps: + - name: Create .env file + run: | + echo "DB_HOST=${{ secrets.DB_HOST }}" >> ${{inputs.env_file}} + echo "DB_PORT=${{ secrets.DB_PORT }}" >> ${{inputs.env_file}} + echo "DB_USER=${{ secrets.DB_USER }}" >> ${{inputs.env_file}} + echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> ${{inputs.env_file}} + echo "DB_NAME=${{ secrets.DB_NAME }}" >> ${{inputs.env_file}} + echo "DB_DROP_SCHEMA=${{ secrets.DB_DROP_SCHEMA }}" >> ${{inputs.env_file}} + echo "AUTH_SECRET=${{ secrets.AUTH_SECRET }}" >> ${{inputs.env_file}} + echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" >> ${{inputs.env_file}} + echo "PORT=${{ secrets.PORT }}" >> ${{inputs.env_file}} + test: + runs-on: "self-hosted,${{inputs.runner}}" + needs: create_env + steps: + - name: Run tests + run: npm run test + build: + needs: [create_env, test] + runs-on: "self-hosted,${{inputs.runner}}" + steps: + - name: Build website + id: build-website + run: npm run build + deploy: + needs: build + runs-on: "self-hosted,${{inputs.runner}}" + steps: + - name: Run deployment + run: NODE_ENV=${{inputs.environment}} pm2 restart dist/main.js --name=events-api diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 4a5447b..16ca3b3 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -5,53 +5,10 @@ on: - dev workflow_dispatch: jobs: - checkout: - runs-on: [self-hosted, dev] - environment: dev - steps: - - name: Get code - uses: actions/checkout@v4 - - name: Cache dependencies - id: cache - uses: actions/cache@v4 - with: - path: node_modules - key: deps-node-modules-${{ hashFiles('**/package-lock.json') }} - - name: Install dependencies - if: steps.cache.outputs.cache-hit != 'true' - run: npm ci - create_env: - needs: checkout - runs-on: [self-hosted, dev] - environment: dev - steps: - - name: Create .env file - run: | - echo "DB_HOST=${{ secrets.DB_HOST }}" >> dev.env - echo "DB_PORT=${{ secrets.DB_PORT }}" >> dev.env - echo "DB_USER=${{ secrets.DB_USER }}" >> dev.env - echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> dev.env - echo "DB_NAME=${{ secrets.DB_NAME }}" >> dev.env - echo "DB_DROP_SCHEMA=${{ secrets.DB_DROP_SCHEMA }}" >> dev.env - echo "AUTH_SECRET=${{ secrets.AUTH_SECRET }}" >> dev.env - echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" >> dev.env - echo "PORT=${{ secrets.PORT }}" >> dev.env - test: - runs-on: [self-hosted, dev] - needs: create_env - steps: - - name: Run tests - run: npm run test - build: - needs: [create_env, test] - runs-on: [self-hosted, dev] - steps: - - name: Build website - id: build-website - run: npm run build - deploy: - needs: build - runs-on: [self-hosted, dev] - steps: - - name: Run deployment - run: NODE_ENV=dev pm2 restart dist/main.js --name=events-api \ No newline at end of file + run-deployment: + uses: ./.github/workflows/deployment.yml + with: + environment: "dev" + runner: "dev" + env_file: "dev.env" + secrets: inherit \ No newline at end of file diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 82d58bc..eb98844 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -34,6 +34,7 @@ jobs: echo "PORT=${{ vars.PORT }}" >> production.env test: runs-on: [self-hosted, prod] + needs: checkout steps: - name: Run tests run: npm run test From 0d433a14702f7e02fe489077cfb494a3c1651340 Mon Sep 17 00:00:00 2001 From: olha-dev-fullstack Date: Thu, 27 Feb 2025 17:49:05 +0200 Subject: [PATCH 2/3] fix runner --- .github/workflows/deployment.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 462ec91..b856d41 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -13,7 +13,7 @@ on: required: true jobs: checkout: - runs-on: "self-hosted,${{inputs.runner}}" + runs-on: [self-hosted, "${{inputs.runner}}"] environment: ${{inputs.environment}} steps: - name: Get code @@ -29,7 +29,7 @@ jobs: run: npm ci create_env: needs: checkout - runs-on: "self-hosted,${{inputs.runner}}" + runs-on: [self-hosted, "${{inputs.runner}}"] environment: ${{inputs.environment}} steps: - name: Create .env file @@ -44,21 +44,21 @@ jobs: echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" >> ${{inputs.env_file}} echo "PORT=${{ secrets.PORT }}" >> ${{inputs.env_file}} test: - runs-on: "self-hosted,${{inputs.runner}}" + runs-on: [self-hosted, "${{inputs.runner}}"] needs: create_env steps: - name: Run tests run: npm run test build: needs: [create_env, test] - runs-on: "self-hosted,${{inputs.runner}}" + runs-on: [self-hosted, "${{inputs.runner}}"] steps: - name: Build website id: build-website run: npm run build deploy: needs: build - runs-on: "self-hosted,${{inputs.runner}}" + runs-on: [self-hosted, "${{inputs.runner}}"] steps: - name: Run deployment run: NODE_ENV=${{inputs.environment}} pm2 restart dist/main.js --name=events-api From f91bf6f6f41d86fb0054aa0063056450d5137003 Mon Sep 17 00:00:00 2001 From: olha-dev-fullstack Date: Thu, 27 Feb 2025 17:55:52 +0200 Subject: [PATCH 3/3] reusable for production deployment --- .github/workflows/production.yml | 56 ++++---------------------------- 1 file changed, 7 insertions(+), 49 deletions(-) diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index c7259ae..92fbd1d 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -2,52 +2,10 @@ name: Production deployment on: workflow_dispatch: jobs: - checkout: - runs-on: [self-hosted, prod] - environment: production - steps: - - name: Get code - uses: actions/checkout@v4 - - name: Cache dependencies - id: cache - uses: actions/cache@v4 - with: - path: node_modules - key: deps-node-modules-${{ hashFiles('**/package-lock.json') }} - - name: Install dependencies - if: steps.cache.outputs.cache-hit != 'true' - run: npm ci - create_env: - needs: checkout - runs-on: [self-hosted, prod] - environment: production - steps: - - name: Create .env file - run: | - echo "DB_HOST=${{ secrets.DB_HOST }}" >> production.env - echo "DB_PORT=${{ secrets.DB_PORT }}" >> production.env - echo "DB_USER=${{ secrets.DB_USER }}" >> production.env - echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> production.env - echo "DB_NAME=${{ secrets.DB_NAME }}" >> production.env - echo "AUTH_SECRET=${{ secrets.AUTH_SECRET }}" >> production.env - echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" >> production.env - echo "PORT=${{ vars.PORT }}" >> production.env - test: - runs-on: [self-hosted, prod] - needs: checkout - steps: - - name: Run tests - run: npm run test - build: - needs: [create_env, test] - runs-on: [self-hosted, prod] - steps: - - name: Build website - id: build-website - run: npm run build - deploy: - needs: build - runs-on: [self-hosted, prod] - steps: - - name: Run deployment - run: NODE_ENV=production pm2 restart dist/main.js --name=events-api \ No newline at end of file + run-deployment: + uses: ./.github/workflows/deployment.yml + with: + environment: "production" + runner: "prod" + env_file: "production.env" + secrets: inherit \ No newline at end of file