|
| 1 | +name: Docker |
| 2 | + |
| 3 | +# This workflow uses actions that are not certified by GitHub. |
| 4 | +# They are provided by a third-party and are governed by |
| 5 | +# separate terms of service, privacy policy, and support |
| 6 | +# documentation. |
| 7 | + |
| 8 | +on: |
| 9 | + workflow_dispatch: |
| 10 | + push: |
| 11 | + branches: |
| 12 | + - 'develop' |
| 13 | + - 'release/**' |
| 14 | + - 'hotfix/**' |
| 15 | + # Publish semver tags as releases. |
| 16 | + tags: [ 'v**' ] |
| 17 | + pull_request: |
| 18 | + branches: |
| 19 | + - 'main' |
| 20 | + - 'develop' |
| 21 | + - 'release/**' |
| 22 | + - 'hotfix/**' |
| 23 | + |
| 24 | +permissions: |
| 25 | + contents: read |
| 26 | + |
| 27 | +env: |
| 28 | + # Use docker.io for Docker Hub if empty |
| 29 | + REGISTRY: ghcr.io |
| 30 | + IMAGE_NAME: ${{ github.repository_owner }}/plugindpp |
| 31 | + DOCKERFILE_PATH: source/AAS.TwinEngine.Plugin.RelationalDatabase/Dockerfile |
| 32 | + BUILD_CONTEXT: source |
| 33 | + |
| 34 | + |
| 35 | +jobs: |
| 36 | + build: |
| 37 | + |
| 38 | + runs-on: ubuntu-latest |
| 39 | + permissions: |
| 40 | + contents: read |
| 41 | + packages: write |
| 42 | + |
| 43 | + steps: |
| 44 | + - name: Checkout repository |
| 45 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 46 | + |
| 47 | + # Set up BuildKit Docker container builder to be able to build |
| 48 | + # multi-platform images and export cache |
| 49 | + # https://github.com/docker/setup-buildx-action |
| 50 | + - name: Set up Docker Buildx |
| 51 | + uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 |
| 52 | + |
| 53 | + # Login against a Docker registry except on PR |
| 54 | + # https://github.com/docker/login-action |
| 55 | + - name: Log into registry ${{ env.REGISTRY }} |
| 56 | + if: github.event_name != 'pull_request' |
| 57 | + uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0 |
| 58 | + with: |
| 59 | + registry: ${{ env.REGISTRY }} |
| 60 | + username: ${{ github.actor }} |
| 61 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 62 | + |
| 63 | + # Extract metadata (tags, labels) for Docker |
| 64 | + # https://github.com/docker/metadata-action |
| 65 | + - name: Extract Docker metadata |
| 66 | + id: meta |
| 67 | + uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0 |
| 68 | + with: |
| 69 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 70 | + flavor: | |
| 71 | + latest=false |
| 72 | + tags: | |
| 73 | + type=semver,pattern={{raw}} |
| 74 | + type=raw,value=develop-{{sha}},enable=${{startsWith(github.ref, 'refs/heads/develop')}} |
| 75 | + type=raw,value=rc-{{branch}}-{{sha}},enable=${{startsWith(github.ref, 'refs/heads/release/')}} |
| 76 | + type=raw,value={{branch}}-{{sha}},enable=${{startsWith(github.ref, 'refs/heads/hotfix/')}} |
| 77 | + type=ref,event=pr |
| 78 | + type=raw,value=manual-{{branch}}-{{sha}},enable=${{github.event_name == 'workflow_dispatch'}} |
| 79 | +
|
| 80 | + # Build and push Docker image with Buildx (don't push on PR) |
| 81 | + # https://github.com/docker/build-push-action |
| 82 | + - name: Build and push Docker image |
| 83 | + id: build-and-push |
| 84 | + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 |
| 85 | + with: |
| 86 | + sbom: true |
| 87 | + provenance: mode=max |
| 88 | + context: ${{ env.BUILD_CONTEXT }} |
| 89 | + file: ${{ env.DOCKERFILE_PATH }} |
| 90 | + push: ${{ github.event_name != 'pull_request' }} |
| 91 | + tags: ${{ steps.meta.outputs.tags }} |
| 92 | + labels: ${{ steps.meta.outputs.labels }} |
| 93 | + cache-from: type=gha |
| 94 | + cache-to: type=gha,mode=max |
| 95 | + |
| 96 | + # Extract the pure application SBOM from the artifact stage, we want to handle it separately from the container SBOM |
| 97 | + # This automaticaly re-uses the previously generated stage from cache, so we get the exact sbom from previous build step |
| 98 | + - name: Export Application SBOM from artifact stage |
| 99 | + if: ${{ github.event_name != 'pull_request' }} |
| 100 | + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 |
| 101 | + with: |
| 102 | + context: ${{ env.BUILD_CONTEXT }} |
| 103 | + file: ${{ env.DOCKERFILE_PATH }} |
| 104 | + target: app-sbom-artifact |
| 105 | + push: false |
| 106 | + outputs: type=local,dest=sbom-output |
| 107 | + |
| 108 | + # Generate container SBOM. |
| 109 | + - name: Run Trivy in GitHub SBOM mode to generate CycloneDX SBOM for container |
| 110 | + if: ${{ github.event_name != 'pull_request' }} |
| 111 | + uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 # 0.33.1 |
| 112 | + with: |
| 113 | + scan-type: 'image' |
| 114 | + format: 'cyclonedx' |
| 115 | + output: 'sbom-output/sbom_container.cyclonedx.json' |
| 116 | + image-ref: ${{ steps.meta.outputs.tags }} |
| 117 | + skip-dirs: '/App' # Skip the /app directory as we handle the content of the application in a seperate SBOM for easier vulnerability management and because trivy misses important fields |
| 118 | + |
| 119 | + - name: Upload trivy/container AND application SBOMs as a Github artifact |
| 120 | + if: ${{ github.event_name != 'pull_request' }} |
| 121 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 122 | + with: |
| 123 | + name: sbom |
| 124 | + path: '${{ github.workspace }}/sbom-output/' |
0 commit comments