diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml new file mode 100644 index 0000000..b856d41 --- /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 2c67259..92fbd1d 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -2,51 +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] - 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