|
| 1 | +--- # yamllint disable rule:line-length |
| 2 | +name: "Preview Deployment" |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + types: [opened, synchronize, reopened] |
| 7 | + push: |
| 8 | + branches: [main] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + packages: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +env: |
| 16 | + REGISTRY: ghcr.io |
| 17 | + IMAGE_PREFIX: ghcr.io/${{ github.repository_owner }} |
| 18 | + |
| 19 | +jobs: |
| 20 | + set-tag: |
| 21 | + name: "Determine Tag" |
| 22 | + runs-on: ubuntu-latest |
| 23 | + outputs: |
| 24 | + tag: ${{ steps.set-tag.outputs.tag }} |
| 25 | + steps: |
| 26 | + - name: Set output tag |
| 27 | + id: set-tag |
| 28 | + run: | |
| 29 | + if [ "${{ github.event_name }}" == "pull_request" ]; then |
| 30 | + echo "tag=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT |
| 31 | + else |
| 32 | + echo "tag=${{ github.ref_name }}" >> $GITHUB_OUTPUT |
| 33 | + fi |
| 34 | +
|
| 35 | + build-and-publish: |
| 36 | + name: "Build and Publish Preview Images" |
| 37 | + runs-on: ubuntu-latest |
| 38 | + needs: set-tag |
| 39 | + strategy: |
| 40 | + matrix: |
| 41 | + component: |
| 42 | + - wrongsecrets-balancer |
| 43 | + - cleaner |
| 44 | + steps: |
| 45 | + - name: Checkout |
| 46 | + uses: actions/checkout@v5 |
| 47 | + |
| 48 | + - name: Set up Docker Buildx |
| 49 | + uses: docker/setup-buildx-action@v3 |
| 50 | + |
| 51 | + - name: Log in to Container Registry |
| 52 | + uses: docker/login-action@v3 |
| 53 | + with: |
| 54 | + registry: ${{ env.REGISTRY }} |
| 55 | + username: ${{ github.actor }} |
| 56 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 57 | + |
| 58 | + - name: Generate metadata |
| 59 | + id: meta |
| 60 | + uses: docker/metadata-action@v5 |
| 61 | + with: |
| 62 | + images: ${{ env.IMAGE_PREFIX }}/${{ matrix.component }} |
| 63 | + tags: | |
| 64 | + type=raw,value=${{ needs.set-tag.outputs.tag }} |
| 65 | +
|
| 66 | + - name: Build and push |
| 67 | + uses: docker/build-push-action@v6 |
| 68 | + with: |
| 69 | + context: ./${{ matrix.component }} |
| 70 | + file: ./${{ matrix.component }}/Dockerfile |
| 71 | + push: true |
| 72 | + tags: ${{ steps.meta.outputs.tags }} |
| 73 | + labels: ${{ steps.meta.outputs.labels }} |
| 74 | + platforms: linux/amd64,linux/arm64 |
| 75 | + |
| 76 | + generate-preview-instructions: |
| 77 | + name: "Generate Preview Instructions" |
| 78 | + runs-on: ubuntu-latest |
| 79 | + needs: [set-tag, build-and-publish] |
| 80 | + if: github.event_name == 'pull_request' |
| 81 | + steps: |
| 82 | + - name: Checkout |
| 83 | + uses: actions/checkout@v5 |
| 84 | + |
| 85 | + - name: Install yq |
| 86 | + run: | |
| 87 | + sudo snap install yq |
| 88 | +
|
| 89 | + - name: Generate preview values |
| 90 | + id: values |
| 91 | + run: | |
| 92 | + # Create a preview values file |
| 93 | + cat > preview-values.yaml << EOF |
| 94 | + balancer: |
| 95 | + repository: ${{ env.IMAGE_PREFIX }}/wrongsecrets-balancer |
| 96 | + tag: ${{ needs.set-tag.outputs.tag }} |
| 97 | +
|
| 98 | + wrongsecretsCleanup: |
| 99 | + repository: ${{ env.IMAGE_PREFIX }}/cleaner |
| 100 | + tag: ${{ needs.set-tag.outputs.tag }} |
| 101 | +
|
| 102 | + # Preview configuration |
| 103 | + ingress: |
| 104 | + enabled: true |
| 105 | + hosts: |
| 106 | + - host: >- |
| 107 | + preview-${{ needs.set-tag.outputs.tag }}.wrongsecrets.local |
| 108 | + paths: |
| 109 | + - "/" |
| 110 | + EOF |
| 111 | +
|
| 112 | + # Output the content for use in the comment |
| 113 | + echo "values<<EOF" >> $GITHUB_OUTPUT |
| 114 | + cat preview-values.yaml >> $GITHUB_OUTPUT |
| 115 | + echo "EOF" >> $GITHUB_OUTPUT |
| 116 | +
|
| 117 | + - name: Create deployment instructions |
| 118 | + id: instructions |
| 119 | + run: | |
| 120 | + # yamllint disable rule:line-length |
| 121 | + cat > instructions.md << 'EOF' |
| 122 | + ## 🚀 Preview Deployment Ready! |
| 123 | +
|
| 124 | + Your pull request has been built and is ready for preview deployment. |
| 125 | + Here's how to test your changes: |
| 126 | +
|
| 127 | + ### Container Images Built |
| 128 | +
|
| 129 | + - **Balancer**: `${{ env.IMAGE_PREFIX }}/wrongsecrets-balancer:${{ needs.set-tag.outputs.tag }}` |
| 130 | + - **Cleaner**: `${{ env.IMAGE_PREFIX }}/cleaner:${{ needs.set-tag.outputs.tag }}` |
| 131 | +
|
| 132 | + ### Quick Deploy with Helm |
| 133 | +
|
| 134 | + ```bash |
| 135 | + # Add the wrongsecrets helm repository |
| 136 | + helm repo add wrongsecrets https://owasp.org/wrongsecrets-ctf-party |
| 137 | + helm repo update |
| 138 | +
|
| 139 | + # Deploy with preview images |
| 140 | + helm install my-preview wrongsecrets/wrongsecrets-ctf-party \ |
| 141 | + --set balancer.repository=${{ env.IMAGE_PREFIX }}/wrongsecrets-balancer \ |
| 142 | + --set balancer.tag=${{ needs.set-tag.outputs.tag }} \ |
| 143 | + --set wrongsecretsCleanup.repository=${{ env.IMAGE_PREFIX }}/cleaner \ |
| 144 | + --set wrongsecretsCleanup.tag=${{ needs.set-tag.outputs.tag }} \ |
| 145 | + --set imagePullPolicy=Always |
| 146 | +
|
| 147 | + # Port forward to access locally |
| 148 | + kubectl port-forward service/wrongsecrets-balancer 3000:3000 |
| 149 | + ``` |
| 150 | +
|
| 151 | + ### Deploy with Custom Values |
| 152 | +
|
| 153 | + <details> |
| 154 | + <summary>Click to see preview-values.yaml</summary> |
| 155 | +
|
| 156 | + ```yaml |
| 157 | + ${{ steps.values.outputs.values }} |
| 158 | + ``` |
| 159 | +
|
| 160 | + </details> |
| 161 | +
|
| 162 | + ```bash |
| 163 | + # Save the above values to preview-values.yaml, then: |
| 164 | + helm install my-preview wrongsecrets/wrongsecrets-ctf-party \ |
| 165 | + -f preview-values.yaml |
| 166 | + ``` |
| 167 | +
|
| 168 | + ### Deploy with Local Build Scripts |
| 169 | +
|
| 170 | + ```bash |
| 171 | + # Clone this PR |
| 172 | + git fetch origin pull/${{ github.event.number }}/head:pr-${{ github.event.number }} |
| 173 | + git checkout pr-${{ github.event.number }} |
| 174 | +
|
| 175 | + # Use the existing deployment script with custom images |
| 176 | + ./build-and-deploy.sh |
| 177 | + ``` |
| 178 | +
|
| 179 | + ### Test the Changes |
| 180 | +
|
| 181 | + 1. Access the application at http://localhost:3000 |
| 182 | + 2. Create a team and verify functionality |
| 183 | + 3. Test any new features or bug fixes |
| 184 | +
|
| 185 | + ### Container Registry |
| 186 | +
|
| 187 | + The preview images are available at: |
| 188 | + - https://github.com/${{ github.repository_owner }}/wrongsecrets-ctf-party/pkgs/container/wrongsecrets-balancer |
| 189 | + - https://github.com/${{ github.repository_owner }}/wrongsecrets-ctf-party/pkgs/container/cleaner |
| 190 | +
|
| 191 | + --- |
| 192 | +
|
| 193 | + *This preview was automatically generated for PR #${{ github.event.number }}* |
| 194 | + EOF |
| 195 | + # yamllint enable rule:line-length |
| 196 | +
|
| 197 | + echo "content<<EOF" >> $GITHUB_OUTPUT |
| 198 | + cat instructions.md >> $GITHUB_OUTPUT |
| 199 | + echo "EOF" >> $GITHUB_OUTPUT |
| 200 | +
|
| 201 | + - name: Comment on PR |
| 202 | + uses: actions/github-script@v7 |
| 203 | + env: |
| 204 | + INSTRUCTIONS_CONTENT: ${{ steps.instructions.outputs.content }} |
| 205 | + with: |
| 206 | + script: | |
| 207 | + const { owner, repo } = context.repo; |
| 208 | + const issue_number = context.issue.number; |
| 209 | +
|
| 210 | + // Find existing preview comment |
| 211 | + const comments = await github.rest.issues.listComments({ |
| 212 | + owner, |
| 213 | + repo, |
| 214 | + issue_number, |
| 215 | + }); |
| 216 | +
|
| 217 | + const existingComment = comments.data.find(comment => |
| 218 | + comment.user.login === 'github-actions[bot]' && |
| 219 | + comment.body.includes('🚀 Preview Deployment Ready!') |
| 220 | + ); |
| 221 | +
|
| 222 | + const body = process.env.INSTRUCTIONS_CONTENT; |
| 223 | +
|
| 224 | + if (existingComment) { |
| 225 | + // Update existing comment |
| 226 | + await github.rest.issues.updateComment({ |
| 227 | + owner, |
| 228 | + repo, |
| 229 | + comment_id: existingComment.id, |
| 230 | + body |
| 231 | + }); |
| 232 | + } else { |
| 233 | + // Create new comment |
| 234 | + await github.rest.issues.createComment({ |
| 235 | + owner, |
| 236 | + repo, |
| 237 | + issue_number, |
| 238 | + body |
| 239 | + }); |
| 240 | + } |
| 241 | +
|
| 242 | + notify-main-branch: |
| 243 | + name: "Notify Main Branch Build" |
| 244 | + runs-on: ubuntu-latest |
| 245 | + needs: [set-tag, build-and-publish] |
| 246 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 247 | + steps: |
| 248 | + - name: Create main branch notification |
| 249 | + run: | |
| 250 | + # yamllint disable rule:line-length |
| 251 | + echo "## 🚀 Main Branch Preview Images Updated!" |
| 252 | + echo "" |
| 253 | + echo "New preview images have been built for the main branch:" |
| 254 | + echo "" |
| 255 | + echo "- **Balancer**: \`${{ env.IMAGE_PREFIX }}/wrongsecrets-balancer:${{ needs.set-tag.outputs.tag }}\`" |
| 256 | + echo "- **Cleaner**: \`${{ env.IMAGE_PREFIX }}/cleaner:${{ needs.set-tag.outputs.tag }}\`" |
| 257 | + echo "" |
| 258 | + echo "These can be used for testing the latest main branch changes." |
| 259 | + # yamllint enable rule:line-length |
0 commit comments