|
| 1 | +name: "Docker Build and Test" |
| 2 | +description: "Reusable action to build and test Docker images for Cal.com" |
| 3 | + |
| 4 | +inputs: |
| 5 | + platform: |
| 6 | + description: "Target platform (linux/amd64 or arm64)" |
| 7 | + required: true |
| 8 | + platform-suffix: |
| 9 | + description: "Suffix to add to image tags (e.g., -arm)" |
| 10 | + required: false |
| 11 | + default: "" |
| 12 | + dockerhub-username: |
| 13 | + description: "Docker Hub username" |
| 14 | + required: true |
| 15 | + dockerhub-token: |
| 16 | + description: "Docker Hub token" |
| 17 | + required: true |
| 18 | + github-token: |
| 19 | + description: "GitHub token for GHCR" |
| 20 | + required: true |
| 21 | + postgres-user: |
| 22 | + description: "PostgreSQL user" |
| 23 | + required: true |
| 24 | + postgres-password: |
| 25 | + description: "PostgreSQL password" |
| 26 | + required: true |
| 27 | + postgres-db: |
| 28 | + description: "PostgreSQL database name" |
| 29 | + required: true |
| 30 | + database-host: |
| 31 | + description: "Database host" |
| 32 | + required: true |
| 33 | + push-image: |
| 34 | + description: "Whether to push the built image" |
| 35 | + required: false |
| 36 | + default: "false" |
| 37 | + |
| 38 | +runs: |
| 39 | + using: "composite" |
| 40 | + steps: |
| 41 | + - name: Log in to the Docker Hub registry |
| 42 | + uses: docker/login-action@v3 |
| 43 | + with: |
| 44 | + username: ${{ inputs.dockerhub-username }} |
| 45 | + password: ${{ inputs.dockerhub-token }} |
| 46 | + logout: true |
| 47 | + |
| 48 | + - name: Log in to the Github Container registry |
| 49 | + uses: docker/login-action@v3 |
| 50 | + with: |
| 51 | + registry: ghcr.io |
| 52 | + username: ${{ github.actor }} |
| 53 | + password: ${{ inputs.github-token }} |
| 54 | + |
| 55 | + - name: Docker meta |
| 56 | + id: meta |
| 57 | + uses: docker/metadata-action@v5 |
| 58 | + with: |
| 59 | + images: | |
| 60 | + docker.io/calendso/calendso |
| 61 | + docker.io/calcom/cal.com |
| 62 | + ghcr.io/calcom/cal.com |
| 63 | + flavor: | |
| 64 | + latest=${{ !github.event.release.prerelease }} |
| 65 | + suffix=${{ inputs.platform-suffix }} |
| 66 | +
|
| 67 | + - name: Copy env |
| 68 | + shell: bash |
| 69 | + run: | |
| 70 | + grep -o '^[^#]*' .env.example > .env |
| 71 | + cat .env >> $GITHUB_ENV |
| 72 | + echo "DATABASE_HOST=localhost:5432" >> $GITHUB_ENV |
| 73 | + eval $(sed -e '/^#/d' -e 's/^/export /' -e 's/$/;/' .env) ; |
| 74 | +
|
| 75 | + - name: Start database |
| 76 | + shell: bash |
| 77 | + run: | |
| 78 | + docker compose up -d database |
| 79 | +
|
| 80 | + - name: Show database logs and container status |
| 81 | + shell: bash |
| 82 | + run: | |
| 83 | + echo "--- Container Status ---" |
| 84 | + docker compose ps database |
| 85 | +
|
| 86 | + echo "--- Container Logs ---" |
| 87 | + docker compose logs database |
| 88 | +
|
| 89 | + - name: Set up Docker Buildx |
| 90 | + uses: docker/setup-buildx-action@v3 |
| 91 | + with: |
| 92 | + driver-opts: | |
| 93 | + network=container:database |
| 94 | + buildkitd-flags: | |
| 95 | + --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host |
| 96 | +
|
| 97 | + - name: Build image |
| 98 | + id: docker_build |
| 99 | + uses: docker/build-push-action@v6 |
| 100 | + with: |
| 101 | + context: ./ |
| 102 | + file: ./Dockerfile |
| 103 | + load: true |
| 104 | + push: false |
| 105 | + platforms: ${{ inputs.platform }} |
| 106 | + tags: ${{ steps.meta.outputs.tags }} |
| 107 | + labels: ${{ steps.meta.outputs.labels }} |
| 108 | + build-args: | |
| 109 | + NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000 |
| 110 | + NEXT_PUBLIC_API_V2_URL=http://localhost:5555/api/v2 |
| 111 | + NEXT_PUBLIC_LICENSE_CONSENT=agree |
| 112 | + DATABASE_URL=postgresql://${{ inputs.postgres-user }}:${{ inputs.postgres-password }}@${{ inputs.database-host }}/${{ inputs.postgres-db }} |
| 113 | + DATABASE_DIRECT_URL=postgresql://${{ inputs.postgres-user }}:${{ inputs.postgres-password }}@${{ inputs.database-host }}/${{ inputs.postgres-db }} |
| 114 | +
|
| 115 | + - name: Test runtime |
| 116 | + shell: bash |
| 117 | + run: | |
| 118 | + tags="${{ steps.meta.outputs.tags }}" |
| 119 | + IFS=',' read -ra ADDR <<< "$tags" |
| 120 | + tag=${ADDR[0]} |
| 121 | +
|
| 122 | + docker run --rm --network stack \ |
| 123 | + -p 3000:3000 \ |
| 124 | + -e DATABASE_URL=postgresql://${{ inputs.postgres-user }}:${{ inputs.postgres-password }}@database/${{ inputs.postgres-db }} \ |
| 125 | + -e DATABASE_DIRECT_URL=postgresql://${{ inputs.postgres-user }}:${{ inputs.postgres-password }}@database/${{ inputs.postgres-db }} \ |
| 126 | + -e NEXTAUTH_SECRET=${{ env.NEXTAUTH_SECRET }} \ |
| 127 | + -e CALENDSO_ENCRYPTION_KEY=${{ env.CALENDSO_ENCRYPTION_KEY }} \ |
| 128 | + $tag & |
| 129 | +
|
| 130 | + server_pid=$! |
| 131 | +
|
| 132 | + echo "Waiting for the server to start..." |
| 133 | + sleep 120 |
| 134 | +
|
| 135 | + echo http://localhost:3000/auth/login |
| 136 | +
|
| 137 | + for i in {1..60}; do |
| 138 | + echo "Checking server health ($i/60)..." |
| 139 | + response=$(curl -o /dev/null -s -w "%{http_code}" http://localhost:3000/auth/login) |
| 140 | + echo "HTTP Status Code: $response" |
| 141 | + if [[ "$response" == "200" ]] || [[ "$response" == "307" ]]; then |
| 142 | + echo "Server is healthy" |
| 143 | + kill $server_pid |
| 144 | + exit 0 |
| 145 | + fi |
| 146 | + sleep 1 |
| 147 | + done |
| 148 | +
|
| 149 | + echo "Server health check failed" |
| 150 | + kill $server_pid |
| 151 | + exit 1 |
| 152 | + env: |
| 153 | + NEXTAUTH_SECRET: "EI4qqDpcfdvf4A+0aQEEx8JjHxHSy4uWiZw/F32K+pA=" |
| 154 | + CALENDSO_ENCRYPTION_KEY: "0zfLtY99wjeLnsM7qsa8xsT+Q0oSgnOL" |
| 155 | + |
| 156 | + - name: Push image |
| 157 | + id: docker_push |
| 158 | + uses: docker/build-push-action@v6 |
| 159 | + if: ${{ inputs.push-image == 'true' && !github.event.release.prerelease }} |
| 160 | + with: |
| 161 | + context: ./ |
| 162 | + file: ./Dockerfile |
| 163 | + push: true |
| 164 | + platforms: ${{ inputs.platform }} |
| 165 | + tags: ${{ steps.meta.outputs.tags }} |
| 166 | + labels: ${{ steps.meta.outputs.labels }} |
| 167 | + build-args: | |
| 168 | + NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000 |
| 169 | + NEXT_PUBLIC_API_V2_URL=http://localhost:5555/api/v2 |
| 170 | + NEXT_PUBLIC_LICENSE_CONSENT=agree |
| 171 | + DATABASE_URL=postgresql://${{ inputs.postgres-user }}:${{ inputs.postgres-password }}@${{ inputs.database-host }}/${{ inputs.postgres-db }} |
| 172 | + DATABASE_DIRECT_URL=postgresql://${{ inputs.postgres-user }}:${{ inputs.postgres-password }}@${{ inputs.database-host }}/${{ inputs.postgres-db }} |
| 173 | +
|
| 174 | + - name: Image digest |
| 175 | + shell: bash |
| 176 | + run: echo ${{ steps.docker_build.outputs.digest }} |
| 177 | + |
| 178 | + - name: Cleanup |
| 179 | + shell: bash |
| 180 | + if: always() |
| 181 | + run: | |
| 182 | + docker compose down |
0 commit comments