Skip to content

Commit

Permalink
feat: cron schedule for data sync (#37)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
danishjoseph authored Jul 16, 2024
1 parent 293925b commit 17eb8b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
36 changes: 32 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,21 +35,48 @@ 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

- 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
Expand All @@ -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 }}
2 changes: 1 addition & 1 deletion apps/backend/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion libs/data-sync/src/lib/data-sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 17eb8b5

Please sign in to comment.