|
| 1 | +--- |
| 2 | +name: Create and publish a kube-ovn image |
| 3 | + |
| 4 | +permissions: |
| 5 | + actions: read |
| 6 | + contents: read |
| 7 | + id-token: write |
| 8 | + packages: write |
| 9 | + pull-requests: write |
| 10 | + security-events: write |
| 11 | + |
| 12 | +on: |
| 13 | + pull_request: |
| 14 | + paths: |
| 15 | + - .github/workflows/container-build-kube-ovn.yaml |
| 16 | + - ContainerFiles/kube-ovn |
| 17 | + schedule: |
| 18 | + - cron: '0 0 * * 0' # Run Weekly at midnight UTC |
| 19 | + workflow_dispatch: |
| 20 | + inputs: |
| 21 | + kube-ovn-version: |
| 22 | + description: 'Version of Kube-OVN to use' |
| 23 | + required: true |
| 24 | + default: "v1.14.4" |
| 25 | + type: choice |
| 26 | + options: |
| 27 | + - "v1.13.14" |
| 28 | + - "v1.14.4" |
| 29 | + |
| 30 | +env: |
| 31 | + REGISTRY: ghcr.io |
| 32 | + IMAGE_NAME: ${{ github.repository }}/kube-ovn |
| 33 | + # NOTE(cloudnull): This is used to parse the workflow_dispatch inputs, sadly the inputs are not available in the |
| 34 | + # workflow_dispatch event, so they're being stored in the environment variables. This is a |
| 35 | + # workaround until there's a better way to handle this. |
| 36 | + kube_ovn: > |
| 37 | + ["v1.14.4", "v1.13.14"] |
| 38 | +jobs: |
| 39 | + init: |
| 40 | + runs-on: ubuntu-latest |
| 41 | + outputs: |
| 42 | + kube-ovn-version: ${{ steps.generate-matrix.outputs.kube_ovn }} |
| 43 | + steps: |
| 44 | + - name: generate-matrix |
| 45 | + id: generate-matrix |
| 46 | + run: | |
| 47 | + if [ "${{ github.event_name == 'workflow_dispatch' }}" = "true" ]; then |
| 48 | + kube_ovn="$(echo '${{ github.event.inputs.kube-ovn-version }}' | jq -R '[select(length>0)]' | jq -c '.')" |
| 49 | + fi |
| 50 | + echo "kube_ovn=${kube_ovn:-${{ env.kube_ovn }}}" >> $GITHUB_OUTPUT |
| 51 | + build-and-push-image: |
| 52 | + needs: |
| 53 | + - init |
| 54 | + strategy: |
| 55 | + matrix: |
| 56 | + kube-ovn-version: ${{ fromJSON(needs.init.outputs.kube-ovn-version) }} |
| 57 | + runs-on: ubuntu-latest |
| 58 | + steps: |
| 59 | + - name: Checkout |
| 60 | + uses: actions/checkout@v4 |
| 61 | + - name: Set up QEMU |
| 62 | + uses: docker/setup-qemu-action@v3 |
| 63 | + - name: Set up Docker Buildx |
| 64 | + uses: docker/setup-buildx-action@v3 |
| 65 | + - name: Dynamically set MY_DATE environment variable |
| 66 | + run: echo "MY_DATE=$(date +%s)" >> $GITHUB_ENV |
| 67 | + - name: Dynamically set environment variables |
| 68 | + run: | |
| 69 | + VERSION=$(echo -n "${{ matrix.kube-ovn-version }}" | awk -F'/' '{($2=="" ? x=$1 : x=$2); print x}') |
| 70 | + echo "OS_VERSION_PARSE=${VERSION}" >> $GITHUB_ENV |
| 71 | + NAME=$(echo -n "${{ env.IMAGE_NAME }}" | awk -F'/' '{print $NF}') |
| 72 | + echo "CATEGORY_NAME=${VERSION}-${NAME}" >> $GITHUB_ENV |
| 73 | + - name: Log in to the Container registry |
| 74 | + uses: docker/login-action@v3 |
| 75 | + with: |
| 76 | + registry: ${{ env.REGISTRY }} |
| 77 | + username: ${{ github.actor }} |
| 78 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + - name: Extract metadata (tags, labels) for Docker |
| 80 | + id: meta |
| 81 | + uses: docker/metadata-action@v5 |
| 82 | + with: |
| 83 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 84 | + - name: Build and push Docker image |
| 85 | + uses: docker/build-push-action@v6 |
| 86 | + with: |
| 87 | + context: . |
| 88 | + file: ContainerFiles/kube-ovn |
| 89 | + push: false |
| 90 | + load: true |
| 91 | + cache-from: type=gha |
| 92 | + cache-to: type=gha,mode=max |
| 93 | + tags: | |
| 94 | + ${{ env.IMAGE_NAME }}:local |
| 95 | + labels: ${{ steps.meta.outputs.labels }} |
| 96 | + build-args: | |
| 97 | + KUBE_OVN_VERSION=${{ matrix.kube-ovn-version }} |
| 98 | + CACHEBUST=${{ github.sha }} |
| 99 | + - name: Run Trivy vulnerability scanner |
| 100 | + uses: aquasecurity/[email protected] |
| 101 | + if: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' }} |
| 102 | + with: |
| 103 | + image-ref: '${{ env.IMAGE_NAME }}:local' |
| 104 | + format: 'sarif' |
| 105 | + output: 'trivy-results.sarif' |
| 106 | + ignore-unfixed: true |
| 107 | + severity: 'CRITICAL,HIGH,MEDIUM' |
| 108 | + - name: Upload Trivy scan results to GitHub Security tab |
| 109 | + continue-on-error: true |
| 110 | + if: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' }} |
| 111 | + uses: github/codeql-action/upload-sarif@v3 |
| 112 | + with: |
| 113 | + sarif_file: 'trivy-results.sarif' |
| 114 | + category: "${{ env.CATEGORY_NAME }}" |
| 115 | + - name: Run Trivy scanner |
| 116 | + uses: aquasecurity/[email protected] |
| 117 | + if: ${{ github.event_name == 'pull_request' }} |
| 118 | + with: |
| 119 | + image-ref: '${{ env.IMAGE_NAME }}:local' |
| 120 | + output: trivy.txt |
| 121 | + ignore-unfixed: true |
| 122 | + severity: 'CRITICAL,HIGH,MEDIUM' |
| 123 | + - name: Create trivy output file in markdown format |
| 124 | + if: ${{ github.event_name == 'pull_request' }} |
| 125 | + run: | |
| 126 | + if [[ -s trivy.txt ]]; then |
| 127 | + echo "### Security Output" > trivy-output.txt |
| 128 | + echo '```terraform' >> trivy-output.txt |
| 129 | + cat trivy.txt >> trivy-output.txt |
| 130 | + echo '```' >> trivy-output.txt |
| 131 | + fi |
| 132 | + - name: Publish Trivy Output to Summary |
| 133 | + if: ${{ github.event_name == 'pull_request' }} |
| 134 | + run: | |
| 135 | + if [[ -s trivy-output.txt ]]; then |
| 136 | + { |
| 137 | + cat trivy-output.txt |
| 138 | + } >> $GITHUB_STEP_SUMMARY |
| 139 | + fi |
| 140 | + - name: Build and push Docker image |
| 141 | + uses: docker/build-push-action@v6 |
| 142 | + with: |
| 143 | + context: . |
| 144 | + file: ContainerFiles/kube-ovn |
| 145 | + push: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' }} |
| 146 | + cache-from: type=gha |
| 147 | + cache-to: type=gha,mode=max |
| 148 | + tags: | |
| 149 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.kube-ovn-version }} |
| 150 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.kube-ovn-version }}-latest |
| 151 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.kube-ovn-version }}-${{ env.MY_DATE }} |
| 152 | + labels: ${{ steps.meta.outputs.labels }} |
| 153 | + build-args: | |
| 154 | + KUBE_OVN_VERSION=${{ matrix.kube-ovn-version }} |
| 155 | + CACHEBUST=${{ github.sha }} |
0 commit comments