diff --git a/.github/scripts/README.md b/.github/scripts/README.md new file mode 100644 index 00000000..e4ff746d --- /dev/null +++ b/.github/scripts/README.md @@ -0,0 +1,55 @@ +# GitHub Scripts and Automation + +This directory contains scripts and documentation for GitHub Actions automation in the Orionrobots repository. + +## Features + +### Automated Docker Image PR Comments + +When a Pull Request modifies files that affect the base Docker image (such as `Dockerfile`, `package.json`, `package-lock.json`, or workflow files), the CI system automatically: + +1. **Detects changes** to base image related files +2. **Builds and pushes** a Docker image tagged with the PR number to `ghcr.io/orionrobots/orionrobots-site.base:${PR_NUMBER}` +3. **Comments on the PR** with a direct link to the newly built Docker image + +#### How it works + +The automation is implemented in `.github/workflows/on_pr_test.yaml`: + +- **Detection**: The `detect_base_image_changes` job uses `tj-actions/changed-files` to detect changes to base image files +- **Build**: If changes are detected, the `build_site` job builds and pushes the image with the PR number as tag +- **Comment**: The `comment_docker_image` job creates or updates a comment on the PR with the image details + +#### Benefits + +- **Easy access**: Reviewers and team members can quickly find and use the Docker image built for a specific PR +- **No searching**: No need to dig through workflow logs or GitHub Package registry +- **Idempotent**: Comments are updated rather than duplicated when the image is rebuilt +- **Clear instructions**: The comment includes copy-paste commands for using the image + +#### Comment format + +The automated comment includes: +- Direct link to the Docker image +- Instructions for pulling and running the image +- Usage examples for local development +- Clear indication that the comment is automatically managed + +#### Permissions and fork compatibility + +- **Internal PRs**: Full functionality with automatic image building and commenting +- **Forks**: May have limited access to push images depending on repository settings +- **Security**: Uses minimal required permissions (`pull-requests: write` for commenting) + +### Scripts + +- `new_post.sh`: Script for creating new blog posts with proper folder structure +- `staging/`: Configuration files for staging environment setup + +## Maintenance + +The Docker image commenting system is self-maintaining and requires no manual intervention. If issues arise: + +1. Check the workflow logs in GitHub Actions +2. Verify that the GitHub token has appropriate permissions +3. Ensure the base image build completed successfully before the comment job runs \ No newline at end of file diff --git a/.github/workflows/on_pr_test.yaml b/.github/workflows/on_pr_test.yaml index d548fe70..8cbff163 100644 --- a/.github/workflows/on_pr_test.yaml +++ b/.github/workflows/on_pr_test.yaml @@ -19,6 +19,7 @@ on: - '.github/workflows/on_push_to_master_test_and_deploy.yaml' - '.github/workflows/on_call_build_site.yaml' - '.github/workflows/on_call_staging_test.yaml' + - '.github/workflows/on_pr_test.yaml' - 'package.json' - 'package-lock.json' - 'webpack.config.js' @@ -58,6 +59,76 @@ jobs: ${{ needs.detect_base_image_changes.outputs.changed == 'true' && github.event.number || '' }} + comment_docker_image: + needs: [detect_base_image_changes, build_site] + runs-on: ubuntu-latest + # Only run if base image changes were detected and this is a PR event + if: needs.detect_base_image_changes.outputs.changed == 'true' && github.event_name == 'pull_request' + permissions: + pull-requests: write # Allow commenting on PR + steps: + - name: Comment on PR with Docker image link + uses: actions/github-script@v7 + with: + script: | + const prNumber = context.issue.number; + const imageTag = prNumber; + const imageUrl = `ghcr.io/orionrobots/orionrobots-site.base:${imageTag}`; + + // Comment body with Docker image information + const commentBody = `## 🐳 Docker Base Image Available + + A new base Docker image has been built and pushed for this PR: + + **Image:** \`${imageUrl}\` + + ### How to use this image: + + \`\`\`bash + # Pull the image + docker pull ${imageUrl} + + # Run with the image + docker run -it ${imageUrl} bash + \`\`\` + + ### For local development: + You can use this image as a base for testing changes without rebuilding dependencies. + + _This comment is automatically updated when the base image is rebuilt._`; + + // Look for existing comment from this bot + const comments = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + }); + + const botComment = comments.data.find(comment => + comment.user.type === 'Bot' && + comment.body.includes('🐳 Docker Base Image Available') + ); + + if (botComment) { + // Update existing comment + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: botComment.id, + body: commentBody + }); + console.log('Updated existing Docker image comment'); + } else { + // Create new comment + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + body: commentBody + }); + console.log('Created new Docker image comment'); + } + staging_test: uses: ./.github/workflows/on_call_staging_test.yaml needs: build_site