From 17eb8b566b60be69ce9045e7727cdb89222f6dd1 Mon Sep 17 00:00:00 2001 From: Danish Joseph Date: Tue, 16 Jul 2024 06:29:49 +0200 Subject: [PATCH] feat: cron schedule for data sync (#37) * feat: add cron expression for data sync service from env variable * fix: ci pipeline * fix: add condition for docker publish * chore: update CI workflow * fix: tag name in nx --- .github/workflows/ci.yml | 36 ++++++++++++++++++--- apps/backend/project.json | 2 +- libs/data-sync/src/lib/data-sync.service.ts | 2 +- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a4d6ca3..0dc19a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,6 +24,7 @@ jobs: uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} + cache: 'yarn' - name: Install test dependencies run: yarn install --production=false --only=dev @@ -34,14 +35,21 @@ jobs: - name: Run Unit tests run: yarn nx run-many -t test - build-and-push: - name: Build & push to DockerHub + build: + name: Build Docker image needs: test runs-on: ubuntu-latest - if: github.event_name == 'push' && github.ref == 'refs/heads/main' steps: - uses: actions/checkout@v4 + - name: Set up NodeJS + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + + - name: Install test dependencies + run: yarn install --production=false --only=dev + - name: Get version number id: version run: echo "VERSION=$(cat ./package.json | jq -r '.version')" >> $GITHUB_ENV @@ -49,6 +57,26 @@ jobs: - name: Build Docker image run: | yarn nx run backend:docker-build --tag=${{ secrets.DOCKER_HUB_USERNAME }}/stockdog:v-${{ env.VERSION }} + docker save ${{ secrets.DOCKER_HUB_USERNAME }}/stockdog:v-${{ env.VERSION }} > docker_image.tar + + - name: Upload Docker image as artifact + uses: actions/upload-artifact@v3 + with: + name: docker-image + path: docker_image.tar + push: + name: Push Docker image to DockerHub + needs: build + runs-on: ubuntu-latest + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + steps: + - name: Download Docker image + uses: actions/download-artifact@v3 + with: + name: docker-image + + - name: Load Docker image + run: docker load < docker_image.tar - name: Login to DockerHub uses: docker/login-action@v3 @@ -57,4 +85,4 @@ jobs: password: ${{ secrets.DOCKER_HUB_TOKEN }} - name: Push Docker image - run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/stockdog:v-${{ env.VERSION }} + run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/stockdog:v-${{ needs.build.outputs.VERSION }} diff --git a/apps/backend/project.json b/apps/backend/project.json index f77ea67..9b6fc64 100644 --- a/apps/backend/project.json +++ b/apps/backend/project.json @@ -21,7 +21,7 @@ }, "docker-build": { "dependsOn": ["build"], - "command": "docker build -f {projectRoot}/Dockerfile -t {projectName}:{args.tag} ." + "command": "docker build -f {projectRoot}/Dockerfile -t {args.tag} ." }, "docker": { "executor": "nx:run-commands", diff --git a/libs/data-sync/src/lib/data-sync.service.ts b/libs/data-sync/src/lib/data-sync.service.ts index 3071fdc..e4465ce 100644 --- a/libs/data-sync/src/lib/data-sync.service.ts +++ b/libs/data-sync/src/lib/data-sync.service.ts @@ -17,7 +17,7 @@ export class DataSyncService { ) {} private readonly logger = new Logger(DataSyncService.name); - @Cron(CronExpression.EVERY_DAY_AT_7PM, { timeZone: 'Asia/Kolkata' }) + @Cron(process.env.CRON_SCHEDULE, { timeZone: process.env.TZ }) async execute() { const currentDate = getCurrentDate(); this.logger.log(