|
| 1 | +on: |
| 2 | + workflow_dispatch: |
| 3 | + push: |
| 4 | + tags: |
| 5 | + - "*/v*" |
| 6 | + |
| 7 | +name: Release policy |
| 8 | + |
| 9 | +jobs: |
| 10 | + calculate-policy-from-tag: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + outputs: |
| 13 | + policy-working-dir: ${{ steps.calculate-policy.outputs.policy_working_dir }} |
| 14 | + policy-version: ${{ steps.policy-info.outputs.policy-version }} |
| 15 | + policy-id: ${{ steps.policy-info.outputs.policy-id }} |
| 16 | + policy-name: ${{ steps.policy-info.outputs.policy-basename }} |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 19 | + - name: Calculate Policy |
| 20 | + id: calculate-policy |
| 21 | + shell: bash |
| 22 | + run: | |
| 23 | + policy_name=$( echo ${{ github.ref_name }} | sed 's/\(.*\)\/\(.*\)$/\1/' ) |
| 24 | + policy_working_dir=$( find policies -type d -name "$policy_name") |
| 25 | + echo "policy_working_dir=$policy_working_dir" >> $GITHUB_OUTPUT |
| 26 | + - name: Get policy metadata |
| 27 | + id: policy-info |
| 28 | + uses: ./.github/actions/get-policy-metadata |
| 29 | + with: |
| 30 | + policy-working-dir: "${{ steps.calculate-policy.outputs.policy_working_dir }}" |
| 31 | + |
| 32 | + ci: |
| 33 | + uses: ./.github/workflows/reusable-ci.yaml |
| 34 | + needs: calculate-policy-from-tag |
| 35 | + with: |
| 36 | + policy-working-dir: ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }} |
| 37 | + |
| 38 | + pre-release-checks: |
| 39 | + name: "Pre release checks" |
| 40 | + runs-on: ubuntu-latest |
| 41 | + needs: [calculate-policy-from-tag] |
| 42 | + steps: |
| 43 | + - name: Checkout code |
| 44 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 45 | + |
| 46 | + - name: Check that `io.kubewarden.policy.version` annotation is up-to-date |
| 47 | + shell: bash |
| 48 | + working-directory: ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }} |
| 49 | + run: | |
| 50 | + VERSION=$(grep 'io.kubewarden.policy.version' metadata.yml | awk '{print $2}' | tr -d '"') |
| 51 | + if [ "$VERSION" != "${{ needs.calculate-policy-from-tag.outputs.policy-version }}" ]; then |
| 52 | + echo "The value of io.kubewarden.policy.version annotation is not in sync with the expected version: '${VERSION}' != '${{ needs.calculate-policy-from-tag.outputs.policy-version }}'" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | +
|
| 56 | + release: |
| 57 | + runs-on: ubuntu-latest |
| 58 | + needs: [calculate-policy-from-tag, pre-release-checks, ci] |
| 59 | + permissions: |
| 60 | + # Required to create GH releases |
| 61 | + contents: write |
| 62 | + # Required to push to GHCR |
| 63 | + packages: write |
| 64 | + # Required by cosign keyless signing |
| 65 | + id-token: write |
| 66 | + steps: |
| 67 | + - name: Checkout code |
| 68 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 69 | + |
| 70 | + - name: Install dependencies |
| 71 | + uses: ./.github/actions/install-dependencies |
| 72 | + |
| 73 | + - name: Build Wasm module |
| 74 | + shell: bash |
| 75 | + run: make -C ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }} policy.wasm |
| 76 | + |
| 77 | + - name: Annotate Wasm module |
| 78 | + shell: bash |
| 79 | + run: | |
| 80 | + make -C ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }} annotated-policy.wasm |
| 81 | +
|
| 82 | + - name: Generate the SBOM files |
| 83 | + shell: bash |
| 84 | + working-directory: ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }} |
| 85 | + run: | |
| 86 | + # SBOM files should have "sbom" in the name due the CLO monitor |
| 87 | + # https://clomonitor.io/docs/topics/checks/#software-bill-of-materials-sbom |
| 88 | + syft scan --output spdx-json=policy-sbom.spdx.json \ |
| 89 | + --source-name $(yq '.annotations["io.kubewarden.policy.title"] + "-" + .annotations["io.kubewarden.policy.version"]' metadata.yml) \ |
| 90 | + --source-version ${{ github.sha }} \ |
| 91 | + -vv dir:. |
| 92 | +
|
| 93 | + - name: Sign BOM file |
| 94 | + shell: bash |
| 95 | + working-directory: ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }} |
| 96 | + run: | |
| 97 | + cosign sign-blob --yes \ |
| 98 | + --bundle policy-sbom.spdx.jsonl.bundle.sigstore \ |
| 99 | + policy-sbom.spdx.json |
| 100 | +
|
| 101 | + - name: Upload policy SBOM files |
| 102 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 103 | + with: |
| 104 | + name: policy-sbom |
| 105 | + path: | |
| 106 | + ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }}/policy-sbom.spdx.json |
| 107 | + ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }}/policy-sbom.spdx.jsonl.bundle.sigstore |
| 108 | +
|
| 109 | + - name: Login to GitHub Container Registry |
| 110 | + uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0 |
| 111 | + with: |
| 112 | + registry: ghcr.io |
| 113 | + username: ${{ github.repository_owner }} |
| 114 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 115 | + |
| 116 | + - name: Publish Wasm policy artifact to OCI registry with the 'latest' tag |
| 117 | + shell: bash |
| 118 | + if: ${{ startsWith(github.ref, 'refs/heads/') }} |
| 119 | + run: | |
| 120 | + set -ex |
| 121 | + echo Pushing :latest policy to OCI container registry |
| 122 | + IMMUTABLE_REF=$(kwctl push -o json annotated-policy.wasm ghcr.io/${{ github.repository_owner }}/openplatform-kubewarden-policies/${{ needs.calculate-policy-from-tag.outputs.policy-id }}:latest | jq -r .immutable_ref) |
| 123 | +
|
| 124 | + echo Keyless signing of policy using cosign |
| 125 | + cosign sign --yes ${IMMUTABLE_REF} |
| 126 | +
|
| 127 | + - name: Publish Wasm policy artifact to OCI registry with the version tag and 'latest' |
| 128 | + shell: bash |
| 129 | + if: ${{ ! startsWith(github.ref, 'refs/heads/') }} |
| 130 | + working-directory: ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }} |
| 131 | + run: | |
| 132 | + set -ex |
| 133 | + export OCI_TAG="v${{ needs.calculate-policy-from-tag.outputs.policy-version }}" |
| 134 | +
|
| 135 | + echo Pushing tagged policy to OCI container registry |
| 136 | + IMMUTABLE_REF=$(kwctl push -o json annotated-policy.wasm ghcr.io/${{ github.repository_owner }}/openplatform-kubewarden-policies/${{ needs.calculate-policy-from-tag.outputs.policy-id }}:${OCI_TAG} | jq -r .immutable_ref) |
| 137 | +
|
| 138 | + echo Keyless signing of policy using cosign |
| 139 | + cosign sign --yes ${IMMUTABLE_REF} |
| 140 | +
|
| 141 | + - name: Create release |
| 142 | + id: create-release |
| 143 | + uses: release-drafter/release-drafter@b1476f6e6eb133afa41ed8589daba6dc69b4d3f5 # v6.1.0 |
| 144 | + with: |
| 145 | + config-name: "release-drafter-${{ needs.calculate-policy-from-tag.outputs.policy-name }}.yml" |
| 146 | + disable-autolabeler: true |
| 147 | + prerelease: ${{ contains(needs.calculate-policy-from-tag.outputs.policy-version, '-alpha') || contains(needs.calculate-policy-from-tag.outputs.policy-version, '-beta') || contains(needs.calculate-policy-from-tag.outputs.policy-version, '-rc') }} |
| 148 | + env: |
| 149 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 150 | + |
| 151 | + - name: Upload release assets |
| 152 | + if: ${{ ! startsWith(github.ref, 'refs/heads/') }} |
| 153 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 154 | + env: |
| 155 | + POLICY_WORKING_DIR: ${{ needs.calculate-policy-from-tag.outputs.policy-working-dir }} |
| 156 | + RELEASE_ID: ${{ steps.create-release.outputs.id }} |
| 157 | + with: |
| 158 | + script: | |
| 159 | + let fs = require('fs'); |
| 160 | + let path = require('path'); |
| 161 | + let workingDir = process.env.POLICY_WORKING_DIR || '.'; |
| 162 | +
|
| 163 | + let files = [ |
| 164 | + `${workingDir}/policy.wasm`, |
| 165 | + `${workingDir}/policy-sbom.spdx.json`, |
| 166 | + `${workingDir}/policy-sbom.spdx.jsonl.bundle.sigstore`, |
| 167 | + ] |
| 168 | + const {RELEASE_ID} = process.env |
| 169 | +
|
| 170 | + for (const file of files) { |
| 171 | + let file_data = fs.readFileSync(file); |
| 172 | +
|
| 173 | + let response = await github.rest.repos.uploadReleaseAsset({ |
| 174 | + owner: context.repo.owner, |
| 175 | + repo: context.repo.repo, |
| 176 | + release_id: `${RELEASE_ID}`, |
| 177 | + name: path.basename(file), |
| 178 | + data: file_data, |
| 179 | + }); |
| 180 | + } |
| 181 | + - name: Publish release |
| 182 | + # if we are on a single repo, we obtain the last draft release as it contains the changelog |
| 183 | + # and edit it by adding the artifacts. Then we mark the release as latest |
| 184 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 185 | + env: |
| 186 | + RELEASE_ID: ${{ steps.create-release.outputs.id }} |
| 187 | + with: |
| 188 | + script: | |
| 189 | + const {RELEASE_ID} = process.env |
| 190 | + const TAG_NAME = "${{ github.ref_name }}"; |
| 191 | + isPreRelease = ${{ contains(github.ref_name, '-alpha') || contains(github.ref_name, '-beta') || contains(github.ref_name, '-rc') }} |
| 192 | + github.rest.repos.updateRelease({ |
| 193 | + owner: context.repo.owner, |
| 194 | + repo: context.repo.repo, |
| 195 | + release_id: `${RELEASE_ID}`, |
| 196 | + draft: false, |
| 197 | + tag_name: TAG_NAME, |
| 198 | + name: TAG_NAME, |
| 199 | + prerelease: isPreRelease, |
| 200 | + make_latest: !isPreRelease |
| 201 | + }); |
0 commit comments