diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..da36a59 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,47 @@ +name: Build & Push Climate Mediator image + +on: + push: + tags: + - "*.*.*" + branches: + - main + +jobs: + build-and-push: + environment: dockerhub + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - run: npm run build + + - name: Build and push tag + if: ${{ github.ref_name != 'main' }} + uses: docker/build-push-action@v5 + with: + platforms: linux/amd64,linux/arm64 + push: true + tags: jembi/climate-mediator:${{ github.ref_name }} + + - name: Build and push latest + if: ${{ github.ref_name == 'main' }} + uses: docker/build-push-action@v5 + with: + platforms: linux/amd64,linux/arm64 + push: true + tags: jembi/climate-mediator:latest + \ No newline at end of file diff --git a/.github/workflows/scan-image.yml b/.github/workflows/scan-image.yml new file mode 100644 index 0000000..0fe6dc8 --- /dev/null +++ b/.github/workflows/scan-image.yml @@ -0,0 +1,30 @@ +name: Scan latest climate mediator image + +on: + schedule: + - cron: '0 5 * * *' + +jobs: + scan-images: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Build an image from Dockerfile + run: | + docker build -t jembi/climate-mediator:${{ github.sha }} . + + - name: Run trivy vulnerability scanner for the climate mediator image + uses: aquasecurity/trivy-action@master + with: + image-ref: jembi/climate-mediator:${{ github.sha }} + format: 'sarif' + output: 'trivy-results.sarif' + + - name: Upload Trivy scan results to Github Security tab + uses: github/codeql-action/upload-sarif@v2 + if: always() + with: + sarif_file: 'trivy-results.sarif' + \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..bf511f4 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,14 @@ +name: Run Climate Mediator Tests +on: push + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install modules + run: npm i + + - name: Run tests + run: npm run test + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a5e534b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM node:16-alpine + +WORKDIR /usr/src/app + +COPY . . + +RUN npm run build + + +EXPOSE 3000 + +CMD [ "node", "dist/index.js" ] diff --git a/tests/services/mediator.test.ts b/tests/services/mediator.test.ts index 883d76c..a4a3e85 100644 --- a/tests/services/mediator.test.ts +++ b/tests/services/mediator.test.ts @@ -42,19 +42,25 @@ describe('Mediator Service', () => { }); }); - // Make the actual request to register - const response = await fetch('https://localhost:8080/mediators', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({}), - }).catch((err) => { - console.error('Fetch error:', err); - throw err; + // Use the existing nock scope to make the request + const response = await new Promise((resolve) => { + https + .request( + 'https://localhost:8080/mediators', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + }, + (res) => { + resolve(res); + } + ) + .end(JSON.stringify({})); }); - // Assert the response status - expect(response.status).to.equal(401); + // Ensure the mocked request was made + expect(scope.isDone()).to.be.true; }); });