|
| 1 | +name: Build and Push Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, develop ] |
| 6 | + tags: [ 'v*' ] |
| 7 | + pull_request: |
| 8 | + branches: [ main ] |
| 9 | + |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_NAME: ${{ github.repository }} |
| 13 | + |
| 14 | +jobs: |
| 15 | + build-and-push: |
| 16 | + name: Build and Push Docker Image |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + packages: write |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout code |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Setup Docker Buildx |
| 27 | + uses: docker/setup-buildx-action@v3 |
| 28 | + |
| 29 | + - name: Log in to Container Registry |
| 30 | + if: github.event_name != 'pull_request' |
| 31 | + uses: docker/login-action@v3 |
| 32 | + with: |
| 33 | + registry: ${{ env.REGISTRY }} |
| 34 | + username: ${{ github.actor }} |
| 35 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 36 | + |
| 37 | + - name: Extract metadata |
| 38 | + id: meta |
| 39 | + uses: docker/metadata-action@v5 |
| 40 | + with: |
| 41 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 42 | + tags: | |
| 43 | + type=ref,event=branch |
| 44 | + type=ref,event=pr |
| 45 | + type=semver,pattern={{version}} |
| 46 | + type=semver,pattern={{major}}.{{minor}} |
| 47 | + type=semver,pattern={{major}} |
| 48 | + type=sha,prefix={{branch}}- |
| 49 | + type=raw,value=latest,enable={{is_default_branch}} |
| 50 | +
|
| 51 | + - name: Build Docker image |
| 52 | + uses: docker/build-push-action@v5 |
| 53 | + with: |
| 54 | + context: . |
| 55 | + file: ./Dockerfile |
| 56 | + target: runner |
| 57 | + push: false |
| 58 | + tags: ${{ steps.meta.outputs.tags }} |
| 59 | + labels: ${{ steps.meta.outputs.labels }} |
| 60 | + cache-from: type=gha |
| 61 | + cache-to: type=gha,mode=max |
| 62 | + platforms: linux/amd64,linux/arm64 |
| 63 | + |
| 64 | + - name: Test Docker image |
| 65 | + run: | |
| 66 | + # Get the first tag for testing |
| 67 | + IMAGE_TAG=$(echo "${{ steps.meta.outputs.tags }}" | head -n1) |
| 68 | + echo "Testing image: $IMAGE_TAG" |
| 69 | + |
| 70 | + # Run a quick test to ensure the image starts correctly |
| 71 | + docker run --rm -d --name devlog-test -p 3000:3000 \ |
| 72 | + -e NODE_ENV=production \ |
| 73 | + -e DEVLOG_STORAGE_TYPE=json \ |
| 74 | + $IMAGE_TAG |
| 75 | + |
| 76 | + # Wait for the application to start |
| 77 | + sleep 15 |
| 78 | + |
| 79 | + # Check if the application is responding |
| 80 | + if curl -f http://localhost:3000/ -I 2>/dev/null; then |
| 81 | + echo "✅ Application is responding" |
| 82 | + else |
| 83 | + echo "❌ Application not responding" |
| 84 | + docker logs devlog-test |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + |
| 88 | + # Test health endpoint if available |
| 89 | + if curl -f http://localhost:3000/api/health 2>/dev/null; then |
| 90 | + echo "✅ Health check passed" |
| 91 | + else |
| 92 | + echo "⚠️ Health check endpoint not available, but application is running" |
| 93 | + fi |
| 94 | + |
| 95 | + # Cleanup |
| 96 | + docker stop devlog-test |
| 97 | +
|
| 98 | + - name: Push Docker image |
| 99 | + if: github.event_name != 'pull_request' |
| 100 | + uses: docker/build-push-action@v5 |
| 101 | + with: |
| 102 | + context: . |
| 103 | + file: ./Dockerfile |
| 104 | + target: runner |
| 105 | + push: true |
| 106 | + tags: ${{ steps.meta.outputs.tags }} |
| 107 | + labels: ${{ steps.meta.outputs.labels }} |
| 108 | + cache-from: type=gha |
| 109 | + cache-to: type=gha,mode=max |
| 110 | + platforms: linux/amd64,linux/arm64 |
| 111 | + |
| 112 | + - name: Generate deployment summary |
| 113 | + if: github.event_name != 'pull_request' |
| 114 | + run: | |
| 115 | + echo "## 🐳 Docker Image Published" >> $GITHUB_STEP_SUMMARY |
| 116 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 117 | + echo "**Registry:** \`${{ env.REGISTRY }}\`" >> $GITHUB_STEP_SUMMARY |
| 118 | + echo "**Repository:** \`${{ env.IMAGE_NAME }}\`" >> $GITHUB_STEP_SUMMARY |
| 119 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 120 | + echo "### 📦 Tags Published:" >> $GITHUB_STEP_SUMMARY |
| 121 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 122 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 123 | + echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY |
| 124 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 125 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 126 | + echo "### 🚀 Usage:" >> $GITHUB_STEP_SUMMARY |
| 127 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 128 | + echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY |
| 129 | + echo "# Pull and run the latest image" >> $GITHUB_STEP_SUMMARY |
| 130 | + echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" >> $GITHUB_STEP_SUMMARY |
| 131 | + echo "docker run -p 3000:3000 ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" >> $GITHUB_STEP_SUMMARY |
| 132 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 133 | + echo "# Or use docker-compose" >> $GITHUB_STEP_SUMMARY |
| 134 | + echo "docker-compose up" >> $GITHUB_STEP_SUMMARY |
| 135 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 136 | +
|
| 137 | + security-scan: |
| 138 | + name: Security Scan |
| 139 | + runs-on: ubuntu-latest |
| 140 | + needs: build-and-push |
| 141 | + if: github.event_name != 'pull_request' |
| 142 | + permissions: |
| 143 | + contents: read |
| 144 | + packages: read |
| 145 | + security-events: write |
| 146 | + |
| 147 | + steps: |
| 148 | + - name: Checkout code |
| 149 | + uses: actions/checkout@v4 |
| 150 | + |
| 151 | + - name: Run Trivy vulnerability scanner |
| 152 | + uses: aquasecurity/trivy-action@master |
| 153 | + with: |
| 154 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} |
| 155 | + format: 'sarif' |
| 156 | + output: 'trivy-results.sarif' |
| 157 | + |
| 158 | + - name: Upload Trivy scan results to GitHub Security tab |
| 159 | + uses: github/codeql-action/upload-sarif@v3 |
| 160 | + if: always() |
| 161 | + with: |
| 162 | + sarif_file: 'trivy-results.sarif' |
0 commit comments