|
| 1 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | +name: mkosi-update-snapshot |
| 3 | +description: Update distribution snapshot and create a pull request |
| 4 | + |
| 5 | +inputs: |
| 6 | + token: |
| 7 | + description: GitHub token for authentication |
| 8 | + required: true |
| 9 | + distribution: |
| 10 | + description: Distribution to update the snapshot for |
| 11 | + required: false |
| 12 | + default: "" |
| 13 | + config: |
| 14 | + description: Path to the config file to update |
| 15 | + required: true |
| 16 | + release: |
| 17 | + description: Distribution release to update the snapshot for |
| 18 | + required: false |
| 19 | + default: "" |
| 20 | + |
| 21 | +runs: |
| 22 | + using: composite |
| 23 | + steps: |
| 24 | + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 |
| 25 | + with: |
| 26 | + token: ${{ inputs.token }} |
| 27 | + |
| 28 | + - uses: ./ |
| 29 | + |
| 30 | + - name: Free disk space |
| 31 | + shell: bash |
| 32 | + run: | |
| 33 | + sudo mv /usr/local /usr/local.trash |
| 34 | + sudo mv /opt/hostedtoolcache /opt/hostedtoolcache.trash |
| 35 | + sudo systemd-run rm -rf /usr/local.trash /opt/hostedtoolcache.trash |
| 36 | +
|
| 37 | + - name: Configure git |
| 38 | + shell: bash |
| 39 | + run: | |
| 40 | + git config user.name "github-actions[bot]" |
| 41 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 42 | +
|
| 43 | + - name: Update snapshot |
| 44 | + id: update |
| 45 | + shell: bash |
| 46 | + env: |
| 47 | + DISTRIBUTION: ${{ inputs.distribution }} |
| 48 | + RELEASE: ${{ inputs.release }} |
| 49 | + run: | |
| 50 | + branch="update-snapshot/${DISTRIBUTION:-default}" |
| 51 | + git checkout -B "$branch" |
| 52 | +
|
| 53 | + mkosi ${DISTRIBUTION:+-d "$DISTRIBUTION"} ${RELEASE:+-r "$RELEASE"} latest-snapshot -- --update ${{ inputs.config }} --commit |
| 54 | +
|
| 55 | + if git diff --quiet HEAD~1 HEAD 2>/dev/null; then |
| 56 | + echo "updated=false" >>"$GITHUB_OUTPUT" |
| 57 | + else |
| 58 | + echo "updated=true" >>"$GITHUB_OUTPUT" |
| 59 | + echo "branch=$branch" >>"$GITHUB_OUTPUT" |
| 60 | + echo "title=$(git log -1 --format=%s)" >>"$GITHUB_OUTPUT" |
| 61 | + fi |
| 62 | +
|
| 63 | + - name: Build image and generate manifest diff |
| 64 | + if: steps.update.outputs.updated == 'true' |
| 65 | + shell: bash |
| 66 | + env: |
| 67 | + DISTRIBUTION: ${{ inputs.distribution }} |
| 68 | + RELEASE: ${{ inputs.release }} |
| 69 | + run: | |
| 70 | + tee mkosi.local.conf <<EOF |
| 71 | + [Distribution] |
| 72 | + ${DISTRIBUTION:+Distribution=$DISTRIBUTION} |
| 73 | + ${RELEASE:+Release=$RELEASE} |
| 74 | +
|
| 75 | + [Output] |
| 76 | + Format=directory |
| 77 | + ManifestFormat=json |
| 78 | + EOF |
| 79 | +
|
| 80 | + # Build the image to generate a manifest. If the build fails, we still want to |
| 81 | + # create the PR with just the snapshot update but include the build output so |
| 82 | + # that it's easy to see what went wrong. |
| 83 | + if ! mkosi -f 2>&1 | tee /tmp/mkosi-build.log; then |
| 84 | + echo "::warning::Image build failed, skipping manifest diff" |
| 85 | + cat >/tmp/pr-body.md <<BODY |
| 86 | + :x: **Image build failed** |
| 87 | +
|
| 88 | + <details> |
| 89 | + <summary>Build log</summary> |
| 90 | +
|
| 91 | + \`\`\` |
| 92 | + $(cat /tmp/mkosi-build.log) |
| 93 | + \`\`\` |
| 94 | +
|
| 95 | + </details> |
| 96 | + BODY |
| 97 | + mkosi -f clean |
| 98 | + git push -f origin "${{ steps.update.outputs.branch }}" |
| 99 | + exit 0 |
| 100 | + fi |
| 101 | +
|
| 102 | + manifest="mkosi.output/image.manifest" |
| 103 | +
|
| 104 | + if [[ -e mkosi/mkosi.conf ]] || [[ -e mkosi/mkosi.tools.conf ]]; then |
| 105 | + manifests_dir="mkosi/manifests" |
| 106 | + else |
| 107 | + manifests_dir="manifests" |
| 108 | + fi |
| 109 | +
|
| 110 | + old_manifest="$manifests_dir/${DISTRIBUTION:-default}.manifest.json" |
| 111 | +
|
| 112 | + if [[ -f "$old_manifest" && -f "$manifest" ]]; then |
| 113 | + python3 tools/mkosi-manifest-diff.py "$old_manifest" "$manifest" >/tmp/pr-body.md |
| 114 | + elif [[ -f "$manifest" ]]; then |
| 115 | + echo "First manifest for this distribution, no previous manifest to diff against." >/tmp/pr-body.md |
| 116 | + fi |
| 117 | +
|
| 118 | + if [[ -f "$manifest" ]]; then |
| 119 | + mkdir -p "$manifests_dir" |
| 120 | + cp "$manifest" "$old_manifest" |
| 121 | + git add "$old_manifest" |
| 122 | + git commit --amend --no-edit |
| 123 | + fi |
| 124 | +
|
| 125 | + mkosi -f clean |
| 126 | + rm mkosi.local.conf |
| 127 | + git push -f origin "${{ steps.update.outputs.branch }}" |
| 128 | +
|
| 129 | + - name: Create or update pull request |
| 130 | + if: steps.update.outputs.updated == 'true' |
| 131 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea |
| 132 | + with: |
| 133 | + github-token: ${{ inputs.token }} |
| 134 | + script: | |
| 135 | + const fs = require('fs'); |
| 136 | + const branch = '${{ steps.update.outputs.branch }}'; |
| 137 | + const title = '${{ steps.update.outputs.title }}'; |
| 138 | +
|
| 139 | + let body = ''; |
| 140 | + try { |
| 141 | + body = fs.readFileSync('/tmp/pr-body.md', 'utf8').trim(); |
| 142 | + } catch (e) { |
| 143 | + console.log('No PR body file found, continuing without body'); |
| 144 | + } |
| 145 | +
|
| 146 | + const { data: pulls } = await github.rest.pulls.list({ |
| 147 | + owner: context.repo.owner, |
| 148 | + repo: context.repo.repo, |
| 149 | + state: 'open', |
| 150 | + head: `${context.repo.owner}:${branch}`, |
| 151 | + }); |
| 152 | +
|
| 153 | + let number; |
| 154 | +
|
| 155 | + if (pulls.length > 0) { |
| 156 | + const pull = pulls[0]; |
| 157 | + await github.rest.pulls.update({ |
| 158 | + owner: context.repo.owner, |
| 159 | + repo: context.repo.repo, |
| 160 | + pull_number: pull.number, |
| 161 | + title: title, |
| 162 | + body: body, |
| 163 | + }); |
| 164 | + console.log(`Updated existing pull request #${pull.number}`); |
| 165 | + number = pull.number; |
| 166 | + } else { |
| 167 | + const { data: pr } = await github.rest.pulls.create({ |
| 168 | + owner: context.repo.owner, |
| 169 | + repo: context.repo.repo, |
| 170 | + title: title, |
| 171 | + head: branch, |
| 172 | + base: 'main', |
| 173 | + body: body, |
| 174 | + }); |
| 175 | + console.log(`Created pull request #${pr.number}`); |
| 176 | + number = pr.number; |
| 177 | + } |
| 178 | +
|
| 179 | + // Enable auto-merge so the PR merges automatically once CI is green. |
| 180 | + try { |
| 181 | + const { data: pullData } = await github.rest.pulls.get({ |
| 182 | + owner: context.repo.owner, |
| 183 | + repo: context.repo.repo, |
| 184 | + pull_number: number, |
| 185 | + }); |
| 186 | +
|
| 187 | + await github.graphql(` |
| 188 | + mutation($pullRequestId: ID!) { |
| 189 | + enablePullRequestAutoMerge(input: { pullRequestId: $pullRequestId, mergeMethod: MERGE }) { |
| 190 | + clientMutationId |
| 191 | + } |
| 192 | + } |
| 193 | + `, { pullRequestId: pullData.node_id }); |
| 194 | +
|
| 195 | + console.log(`Enabled auto-merge for pull request #${number}`); |
| 196 | + } catch (e) { |
| 197 | + console.log(`Could not enable auto-merge for pull request #${number}: ${e.message}`); |
| 198 | + } |
0 commit comments