|
| 1 | +name: Test, Build and Push Docker Image |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [ main, develop, test ] |
| 5 | + pull_request: |
| 6 | + branches: [ main, develop, test ] |
| 7 | + |
| 8 | +defaults: |
| 9 | + run: |
| 10 | + working-directory: ./labman |
| 11 | + |
| 12 | +jobs: |
| 13 | + |
| 14 | + playwright: |
| 15 | + |
| 16 | + runs-on: ubuntu-latest |
| 17 | + # Docker Hub image |
| 18 | + container: node:20-bookworm-slim |
| 19 | + |
| 20 | + # Service containers |
| 21 | + services: |
| 22 | + # Label used to access the service container |
| 23 | + postgres: |
| 24 | + # Docker Hub image |
| 25 | + image: postgres |
| 26 | + # Provide the password for postgres |
| 27 | + env: |
| 28 | + POSTGRES_PASSWORD: postgres |
| 29 | + # Set health checks to wait until postgres has started |
| 30 | + options: >- |
| 31 | + --health-cmd pg_isready |
| 32 | + --health-interval 10s |
| 33 | + --health-timeout 5s |
| 34 | + --health-retries 5 |
| 35 | +
|
| 36 | + steps: |
| 37 | + - name: Check out repository code |
| 38 | + uses: actions/checkout@v5 |
| 39 | + |
| 40 | + - name: Cache node modules |
| 41 | + id: cache-npm |
| 42 | + uses: actions/cache@v4 |
| 43 | + with: |
| 44 | + path: ~/.npm |
| 45 | + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} |
| 46 | + restore-keys: | |
| 47 | + ${{ runner.os }}-npm- |
| 48 | + ${{ runner.os }}- |
| 49 | +
|
| 50 | + - name: Install dependencies |
| 51 | + run: npm ci |
| 52 | + |
| 53 | + - name: Create database |
| 54 | + run: npm run migrate:test |
| 55 | + |
| 56 | + - name: Create test user |
| 57 | + run: npx tsx src/lib/db/testDb.ts |
| 58 | + env: |
| 59 | + NODE_ENV: test |
| 60 | + |
| 61 | + - name: Install Playwright system dependencies |
| 62 | + run: npx playwright install-deps |
| 63 | + |
| 64 | + - name: Cache Playwright browsers |
| 65 | + uses: actions/cache@v4 |
| 66 | + id: playwright-cache |
| 67 | + with: |
| 68 | + path: /root/.cache/ms-playwright |
| 69 | + key: ${{ runner.os }}-playwright-${{ hashFiles('**/package-lock.json') }} |
| 70 | + restore-keys: | |
| 71 | + ${{ runner.os }}-playwright- |
| 72 | +
|
| 73 | + - name: Install Playwright Browsers |
| 74 | + env: |
| 75 | + HOME: /root |
| 76 | + if: steps.playwright-cache.outputs.cache-hit != 'true' |
| 77 | + run: npx playwright install |
| 78 | + |
| 79 | + - name: Run tests |
| 80 | + env: |
| 81 | + NODE_ENV: test |
| 82 | + HOME: /root |
| 83 | + run: npx playwright test --project=firefox |
| 84 | + - uses: actions/upload-artifact@v4 |
| 85 | + if: ${{ !cancelled() }} |
| 86 | + with: |
| 87 | + name: playwright-report |
| 88 | + path: playwright-report/ |
| 89 | + retention-days: 30 |
| 90 | + |
| 91 | + vitest: |
| 92 | + runs-on: ubuntu-latest |
| 93 | + steps: |
| 94 | + - uses: actions/checkout@v4 |
| 95 | + |
| 96 | + - uses: actions/setup-node@v4 |
| 97 | + with: |
| 98 | + node-version: 22 |
| 99 | + |
| 100 | + - name: Install dependencies |
| 101 | + run: npm install |
| 102 | + |
| 103 | + - name: Run Vitest |
| 104 | + run: npm run test |
| 105 | + |
| 106 | + |
| 107 | + build-and-push: |
| 108 | + runs-on: ubuntu-latest |
| 109 | + needs: [vitest, playwright] |
| 110 | + if: ${{ needs.vitest.result == 'success' && needs.playwright.result == 'success' && github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/test')}} |
| 111 | + |
| 112 | + steps: |
| 113 | + - name: Checkout |
| 114 | + uses: actions/checkout@v5 |
| 115 | + |
| 116 | + - name: Log in to container registry |
| 117 | + uses: docker/login-action@v3 |
| 118 | + with: |
| 119 | + registry: ghcr.io |
| 120 | + username: olabekkevold |
| 121 | + password: ${{ secrets.PAT }} |
| 122 | + |
| 123 | + - name: Get version from package.json |
| 124 | + id: version |
| 125 | + run: | |
| 126 | + VERSION=$(jq -r '.version' ./labman/package.json) |
| 127 | + echo "VERSION=$VERSION" >> $GITHUB_OUTPUT |
| 128 | + echo "Version: $VERSION" |
| 129 | +
|
| 130 | + - name: Determine image tag |
| 131 | + id: tag |
| 132 | + run: | |
| 133 | + |
| 134 | + if [ "${{ github.ref }}" = "refs/heads/main" ]; then |
| 135 | + echo "env_tag=latest" >> $GITHUB_OUTPUT |
| 136 | + else |
| 137 | + echo "env_tag=test" >> $GITHUB_OUTPUT |
| 138 | + fi |
| 139 | + echo "Image tag: ${{ steps.tag.outputs.env_tag }}" |
| 140 | +
|
| 141 | + - name: Build and push Docker image |
| 142 | + uses: docker/build-push-action@v6 |
| 143 | + with: |
| 144 | + context: ./labman |
| 145 | + push: true |
| 146 | + tags: | |
| 147 | + ghcr.io/olabekkevold/labmanager:${{ steps.tag.outputs.env_tag }} |
| 148 | + ghcr.io/olabekkevold/labmanager:${{ github.sha }} |
| 149 | + build-args: | |
| 150 | + NEXT_PUBLIC_VERSION=${{ steps.version.outputs.VERSION }} |
| 151 | + NEXT_PUBLIC_ENV=${{ steps.tag.outputs.env_tag }} |
0 commit comments