|
| 1 | +name: Visual Template Diff |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'src/main/resources/templates/**' |
| 7 | + - 'src/main/resources/static/**' |
| 8 | + |
| 9 | +jobs: |
| 10 | + visual-diff: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - name: Checkout PR code |
| 14 | + uses: actions/checkout@v4 |
| 15 | + with: |
| 16 | + path: pr-code |
| 17 | + |
| 18 | + - name: Checkout main branch |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + ref: master |
| 22 | + path: main-code |
| 23 | + |
| 24 | + - name: Set up JDK 23 for PR build |
| 25 | + uses: actions/setup-java@v4 |
| 26 | + with: |
| 27 | + java-version: "23" |
| 28 | + distribution: "oracle" |
| 29 | + cache: "maven" |
| 30 | + |
| 31 | + - name: Extract PR version |
| 32 | + id: extract-pr-version |
| 33 | + working-directory: pr-code |
| 34 | + run: | |
| 35 | + echo "Extracting PR version..." |
| 36 | + chmod +x ./mvnw |
| 37 | + VERSION=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout) |
| 38 | + DOCKER_VERSION=${VERSION%-SNAPSHOT} |
| 39 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 40 | + echo "docker_version=$DOCKER_VERSION" >> $GITHUB_OUTPUT |
| 41 | + echo "PR version: $VERSION" |
| 42 | +
|
| 43 | + - name: Build PR version |
| 44 | + working-directory: pr-code |
| 45 | + run: | |
| 46 | + echo "Building PR version..." |
| 47 | + ./mvnw --no-transfer-progress clean package -DskipTests |
| 48 | + echo "PR JAR built successfully" |
| 49 | + docker build --build-arg argBasedVersion="${{ steps.extract-pr-version.outputs.docker_version }}" -t wrongsecrets-pr . |
| 50 | + echo "PR Docker image built successfully" |
| 51 | +
|
| 52 | + - name: Set up JDK 23 for main build |
| 53 | + uses: actions/setup-java@v4 |
| 54 | + with: |
| 55 | + java-version: "23" |
| 56 | + distribution: "oracle" |
| 57 | + cache: "maven" |
| 58 | + |
| 59 | + - name: Extract main version |
| 60 | + id: extract-main-version |
| 61 | + working-directory: main-code |
| 62 | + run: | |
| 63 | + echo "Extracting main version..." |
| 64 | + chmod +x ./mvnw |
| 65 | + VERSION=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout) |
| 66 | + DOCKER_VERSION=${VERSION%-SNAPSHOT} |
| 67 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 68 | + echo "docker_version=$DOCKER_VERSION" >> $GITHUB_OUTPUT |
| 69 | + echo "Main version: $VERSION" |
| 70 | +
|
| 71 | + - name: Build main version |
| 72 | + working-directory: main-code |
| 73 | + run: | |
| 74 | + echo "Building main version..." |
| 75 | + ./mvnw --no-transfer-progress clean package -DskipTests |
| 76 | + echo "Main JAR built successfully" |
| 77 | + docker build --build-arg argBasedVersion="${{ steps.extract-main-version.outputs.docker_version }}" -t wrongsecrets-main . |
| 78 | + echo "Main Docker image built successfully" |
| 79 | +
|
| 80 | + - name: Start both versions |
| 81 | + run: | |
| 82 | + docker run -d -p 8080:8080 --name pr-version wrongsecrets-pr |
| 83 | + docker run -d -p 8081:8080 --name main-version wrongsecrets-main |
| 84 | +
|
| 85 | + # Wait for containers to start |
| 86 | + echo "Waiting for containers to start..." |
| 87 | + sleep 30 |
| 88 | +
|
| 89 | + # Verify containers are running |
| 90 | + if ! docker ps --filter "name=pr-version" --filter "status=running" --quiet | grep -q .; then |
| 91 | + echo "PR version container failed to start" |
| 92 | + docker logs pr-version |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +
|
| 96 | + if ! docker ps --filter "name=main-version" --filter "status=running" --quiet | grep -q .; then |
| 97 | + echo "Main version container failed to start" |
| 98 | + docker logs main-version |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | +
|
| 102 | + # Wait for services to be ready |
| 103 | + echo "Waiting for services to be ready..." |
| 104 | + timeout 60 bash -c 'until curl -f http://localhost:8080/actuator/health 2>/dev/null; do sleep 2; done' || echo "PR version health check failed" |
| 105 | + timeout 60 bash -c 'until curl -f http://localhost:8081/actuator/health 2>/dev/null; do sleep 2; done' || echo "Main version health check failed" |
| 106 | +
|
| 107 | + - name: Setup Node.js |
| 108 | + uses: actions/setup-node@v4 |
| 109 | + with: |
| 110 | + node-version: '18' |
| 111 | + |
| 112 | + - name: Install Playwright |
| 113 | + run: | |
| 114 | + npm install playwright@latest |
| 115 | + npx playwright install --with-deps chromium |
| 116 | +
|
| 117 | + - name: Take screenshots |
| 118 | + run: | |
| 119 | + mkdir -p screenshots |
| 120 | +
|
| 121 | + # Verify services are running |
| 122 | + echo "Verifying services are running..." |
| 123 | + docker ps --filter "name=pr-version" --format "table {{.Names}}\t{{.Status}}" |
| 124 | + docker ps --filter "name=main-version" --format "table {{.Names}}\t{{.Status}}" |
| 125 | +
|
| 126 | + # Screenshot main pages with error handling |
| 127 | + node -e " |
| 128 | + const { chromium } = require('playwright'); |
| 129 | + (async () => { |
| 130 | + const browser = await chromium.launch({ headless: true }); |
| 131 | + const page = await browser.newPage(); |
| 132 | + await page.setViewportSize({ width: 1280, height: 1024 }); |
| 133 | +
|
| 134 | + try { |
| 135 | + // PR version screenshots |
| 136 | + console.log('Taking PR screenshots...'); |
| 137 | + await page.goto('http://localhost:8080', { waitUntil: 'networkidle', timeout: 30000 }); |
| 138 | + await page.screenshot({ path: 'screenshots/pr-home.png', fullPage: true }); |
| 139 | +
|
| 140 | + await page.goto('http://localhost:8080/about', { waitUntil: 'networkidle', timeout: 30000 }); |
| 141 | + await page.screenshot({ path: 'screenshots/pr-about.png', fullPage: true }); |
| 142 | +
|
| 143 | + // Main version screenshots |
| 144 | + console.log('Taking main branch screenshots...'); |
| 145 | + await page.goto('http://localhost:8081', { waitUntil: 'networkidle', timeout: 30000 }); |
| 146 | + await page.screenshot({ path: 'screenshots/main-home.png', fullPage: true }); |
| 147 | +
|
| 148 | + await page.goto('http://localhost:8081/about', { waitUntil: 'networkidle', timeout: 30000 }); |
| 149 | + await page.screenshot({ path: 'screenshots/main-about.png', fullPage: true }); |
| 150 | +
|
| 151 | + } catch (error) { |
| 152 | + console.error('Screenshot error:', error); |
| 153 | + process.exit(1); |
| 154 | + } finally { |
| 155 | + await browser.close(); |
| 156 | + } |
| 157 | + })(); |
| 158 | + " |
| 159 | +
|
| 160 | + - name: Upload screenshots |
| 161 | + uses: actions/upload-artifact@v4 |
| 162 | + with: |
| 163 | + name: visual-diff-${{ github.event.number }} |
| 164 | + path: screenshots/ |
| 165 | + |
| 166 | + - name: Comment with visual diff |
| 167 | + uses: actions/github-script@v7 |
| 168 | + with: |
| 169 | + script: | |
| 170 | + const comment = `📸 **Visual Diff Available!** |
| 171 | +
|
| 172 | + Screenshots have been generated comparing your changes with the main branch. |
| 173 | +
|
| 174 | + [Download Visual Diff Artifacts](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) |
| 175 | +
|
| 176 | + The artifacts contain: |
| 177 | + - \`pr-home.png\` - Your version of the home page |
| 178 | + - \`main-home.png\` - Current main branch home page |
| 179 | + - \`pr-about.png\` - Your version of the about page |
| 180 | + - \`main-about.png\` - Current main branch about page |
| 181 | +
|
| 182 | + Compare these images to see the visual impact of your changes! |
| 183 | +
|
| 184 | + --- |
| 185 | + <sub>Visual diff generated by GitHub Actions</sub>`; |
| 186 | +
|
| 187 | + github.rest.issues.createComment({ |
| 188 | + issue_number: context.issue.number, |
| 189 | + owner: context.repo.owner, |
| 190 | + repo: context.repo.repo, |
| 191 | + body: comment |
| 192 | + }); |
0 commit comments