|
| 1 | +name: Build and Push OPA Bundle to GHCR |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - develop |
| 8 | + paths: |
| 9 | + - 'opa/**' |
| 10 | + - '.github/workflows/build-opa-bundle.yaml' |
| 11 | + pull_request: |
| 12 | + branches: |
| 13 | + - main |
| 14 | + paths: |
| 15 | + - 'opa/**' |
| 16 | + |
| 17 | +env: |
| 18 | + REGISTRY: ghcr.io |
| 19 | + IMAGE_NAME: ${{ github.repository_owner }}/opa-bundle |
| 20 | + |
| 21 | +jobs: |
| 22 | + build-and-push: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + permissions: |
| 25 | + contents: read |
| 26 | + packages: write |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Setup OPA |
| 32 | + run: | |
| 33 | + mkdir -p .tools |
| 34 | + cd .tools |
| 35 | + curl -L -o opa https://openpolicyagent.org/downloads/latest/opa_linux_amd64 |
| 36 | + chmod +x opa |
| 37 | + echo "$PWD" >> $GITHUB_PATH |
| 38 | +
|
| 39 | + - name: Build OPA bundle |
| 40 | + run: | |
| 41 | + test -d opa/bundle |
| 42 | + opa build -b ./opa/bundle -o ./bundle.tar.gz |
| 43 | +
|
| 44 | + - name: Verify bundle contents |
| 45 | + run: | |
| 46 | + tar -tzf ./bundle.tar.gz | head -20 |
| 47 | + echo "Bundle size: $(du -h ./bundle.tar.gz | cut -f1)" |
| 48 | +
|
| 49 | + - name: Set up Docker Buildx |
| 50 | + uses: docker/setup-buildx-action@v3 |
| 51 | + |
| 52 | + - name: Log in to Container Registry |
| 53 | + uses: docker/login-action@v3 |
| 54 | + with: |
| 55 | + registry: ${{ env.REGISTRY }} |
| 56 | + username: ${{ github.actor }} |
| 57 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + - name: Extract metadata |
| 60 | + id: meta |
| 61 | + run: | |
| 62 | + TAGS="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" |
| 63 | + TAGS="$TAGS,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}" |
| 64 | + |
| 65 | + if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then |
| 66 | + TAGS="$TAGS,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main" |
| 67 | + fi |
| 68 | + if [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then |
| 69 | + TAGS="$TAGS,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop" |
| 70 | + fi |
| 71 | + |
| 72 | + echo "tags=$TAGS" >> $GITHUB_OUTPUT |
| 73 | +
|
| 74 | + - name: Build and push OPA bundle image |
| 75 | + uses: docker/build-push-action@v5 |
| 76 | + with: |
| 77 | + context: . |
| 78 | + file: ./opa/Dockerfile.bundle |
| 79 | + push: ${{ github.event_name != 'pull_request' }} |
| 80 | + tags: ${{ steps.meta.outputs.tags }} |
| 81 | + labels: | |
| 82 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 83 | + org.opencontainers.image.revision=${{ github.sha }} |
| 84 | +
|
| 85 | + - name: Summary |
| 86 | + run: | |
| 87 | + echo "## OPA Bundle Build Summary" >> $GITHUB_STEP_SUMMARY |
| 88 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 89 | + echo "**Registry**: ${{ env.REGISTRY }}" >> $GITHUB_STEP_SUMMARY |
| 90 | + echo "**Image**: ${{ env.IMAGE_NAME }}" >> $GITHUB_STEP_SUMMARY |
| 91 | + echo "**Tags**: ${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY |
| 92 | + echo "**Source**: ./opa/bundle (local repository)" >> $GITHUB_STEP_SUMMARY |
0 commit comments