From 75ecd9e71327b3f142eb23b6d4ea1d66f362610f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Thom?= Date: Thu, 19 Sep 2024 15:00:45 +0200 Subject: [PATCH] Add lint, build, deploy jobs to github action --- .github/workflows/cicd.yml | 90 ++++++++++++++++++++++++++++++++++++ .github/workflows/deploy.yml | 44 ------------------ Dockerfile | 4 ++ 3 files changed, 94 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/cicd.yml delete mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..901f516 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,90 @@ +name: Continuous Integration and Continuous Delivery + +on: + push: + +permissions: + id-token: write + contents: read + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout to the branch + uses: actions/checkout@v4 + + - name: Cache node modules + id: cache-npm + uses: actions/cache@v3 + env: + cache-name: cache-node-modules + with: + # npm cache files are stored in `~/.npm` on Linux/macOS + path: ~/.npm + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + + - if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }} + name: List the state of node modules + continue-on-error: true + run: npm list + + - name: Install dependencies + run: npm install --prefer-offline + + # - name: Typecheck + # run: npm run typecheck + + - name: Lint ESLint + run: npm run lint + + - name: Lint Prettier + run: npm run prettier + + build: + runs-on: ubuntu-latest + steps: + - name: Checkout to the branch + uses: actions/checkout@v4 + + - name: Docker Login + uses: docker/login-action@v3 + with: + registry: ${{ secrets.ACR_URL }} + username: ${{ secrets.ACR_USERNAME }} + password: ${{ secrets.ACR_PASSWORD }} + + - name: Build and Push to ACR + uses: docker/build-push-action@v2 + with: + push: ${{ github.ref == 'refs/heads/main' }} + tags: ${{ secrets.ACR_URL }}/krokelo:${{ github.sha }} + file: Dockerfile + build-args: | + DATABASE_URL=${{ secrets.DATABASE_URL }} + + deploy: + if: ${{ github.ref == 'refs/heads/main' }} + runs-on: ubuntu-latest + needs: [lint, build] + steps: + - name: Azure Login + uses: azure/login@v2 + with: + client-id: ${{ secrets.AZURE_CLIENT_ID }} + tenant-id: ${{ secrets.AZURE_TENANT_ID }} + subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} + + - name: Redeploy Azure Container App + + run: | + az containerapp revision copy \ + --name krokelo \ + --resource-group sbu-public-krokelo-nww-nea-rg \ + --cpu 1 \ + --memory 2.0 \ + --image ${{ secrets.ACR_URL }}/krokelo:${{ github.sha }} diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index a249609..0000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: Trigger auto deployment - -# When this action will be executed -on: - # Uncomment the code below for automatically trigger the workflow when detected changes in the main branch - push: - branches: [main] - - # Allow manual trigger - workflow_dispatch: - -jobs: - build-and-deploy: - runs-on: ubuntu-latest - - steps: - - name: Checkout to the branch - uses: actions/checkout@v2 - - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: "20.x" - - - name: Set up Postgresql with prisma - run: | - DATABASE_URL=${{secrets.DATABASE_URL}} npx prisma generate - DATABASE_URL=${{secrets.DATABASE_URL}} npx prisma migrate deploy - - - name: Azure Login - uses: azure/login@v2 - with: - client-id: ${{ secrets.AZURE_CLIENT_ID }} - tenant-id: ${{ secrets.AZURE_TENANT_ID }} - subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} - - - name: Build and push container image to registry - uses: azure/container-apps-deploy-action@v1 - with: - appSourcePath: ${{ github.workspace }} - registryUrl: sbukrokeloneacr.azurecr.io - registryUsername: ${{ secrets.CONTAINER_REGISTRY_USERNAME }} - registryPassword: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }} - imageToBuild: sbukrokeloneacr.azurecr.io/krokelo:${{ github.sha }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 4649b1a..b247ee2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,6 +37,10 @@ RUN npx prisma generate ADD . . RUN npm run build +# Run migrations +ARG DATABASE_URL +RUN npm run deploy:db + # Finally, build the production image with minimal footprint FROM base