|
| 1 | +name: Changelog submit / apply |
| 2 | +description: > |
| 3 | + Write action that downloads the changelog artifact produced by the evaluate |
| 4 | + action and either commits it to the PR branch or posts it as a PR comment. |
| 5 | + Requires contents:write and pull-requests:write permissions. |
| 6 | +
|
| 7 | +inputs: |
| 8 | + github-token: |
| 9 | + description: 'GitHub token with contents:write and pull-requests:write' |
| 10 | + default: '${{ github.token }}' |
| 11 | + config: |
| 12 | + description: 'Path to changelog.yml configuration file (used in failure comment)' |
| 13 | + default: 'docs/changelog.yml' |
| 14 | + |
| 15 | +outputs: |
| 16 | + committed: |
| 17 | + description: 'Whether a changelog was committed (true/false)' |
| 18 | + value: ${{ steps.commit.outputs.committed || 'false' }} |
| 19 | + |
| 20 | +runs: |
| 21 | + using: composite |
| 22 | + steps: |
| 23 | + - name: Download staging artifact |
| 24 | + uses: actions/download-artifact@v8 |
| 25 | + with: |
| 26 | + name: changelog-staging |
| 27 | + path: /tmp/changelog-staging |
| 28 | + |
| 29 | + - name: Setup docs-builder |
| 30 | + uses: elastic/docs-actions/docs-builder/setup@v1 |
| 31 | + with: |
| 32 | + version: edge |
| 33 | + github-token: ${{ inputs.github-token }} |
| 34 | + |
| 35 | + - name: Evaluate artifact |
| 36 | + id: meta |
| 37 | + shell: bash |
| 38 | + env: |
| 39 | + GITHUB_TOKEN: ${{ inputs.github-token }} |
| 40 | + REPO_OWNER: ${{ github.repository_owner }} |
| 41 | + REPO_NAME: ${{ github.event.repository.name }} |
| 42 | + run: | |
| 43 | + docs-builder changelog evaluate-artifact \ |
| 44 | + --metadata /tmp/changelog-staging/metadata.json \ |
| 45 | + --owner "$REPO_OWNER" \ |
| 46 | + --repo "$REPO_NAME" |
| 47 | +
|
| 48 | + - name: Checkout PR branch |
| 49 | + if: steps.meta.outputs.should-commit == 'true' |
| 50 | + uses: actions/checkout@v6 |
| 51 | + with: |
| 52 | + repository: ${{ steps.meta.outputs.head-repo }} |
| 53 | + ref: ${{ steps.meta.outputs.head-sha }} |
| 54 | + token: ${{ inputs.github-token }} |
| 55 | + persist-credentials: false |
| 56 | + |
| 57 | + - name: Validate ref names |
| 58 | + if: steps.meta.outputs.should-commit == 'true' |
| 59 | + shell: bash |
| 60 | + env: |
| 61 | + HEAD_REF: ${{ steps.meta.outputs.head-ref }} |
| 62 | + run: | |
| 63 | + if [[ ! "$HEAD_REF" =~ ^[a-zA-Z0-9._/+-]+$ ]]; then |
| 64 | + echo "::error::Ref name contains disallowed characters: ${HEAD_REF}" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + - name: Commit changelog |
| 69 | + if: steps.meta.outputs.should-commit == 'true' |
| 70 | + id: commit |
| 71 | + shell: bash |
| 72 | + env: |
| 73 | + PR_NUMBER: ${{ steps.meta.outputs.pr-number }} |
| 74 | + CHANGELOG_DIR: ${{ steps.meta.outputs.changelog-dir }} |
| 75 | + CHANGELOG_FILENAME: ${{ steps.meta.outputs.changelog-filename }} |
| 76 | + GH_TOKEN: ${{ inputs.github-token }} |
| 77 | + GIT_REPOSITORY: ${{ github.repository }} |
| 78 | + IS_FORK: ${{ steps.meta.outputs.is-fork }} |
| 79 | + HEAD_REPO: ${{ steps.meta.outputs.head-repo }} |
| 80 | + HEAD_REF: ${{ steps.meta.outputs.head-ref }} |
| 81 | + run: | |
| 82 | + GENERATED=$(ls /tmp/changelog-staging/*.yaml 2>/dev/null | head -1) |
| 83 | + if [ -z "$GENERATED" ]; then |
| 84 | + echo "::error::No changelog YAML found in staging directory" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | +
|
| 88 | + if [ -n "$CHANGELOG_FILENAME" ]; then |
| 89 | + TARGET_FILENAME="$CHANGELOG_FILENAME" |
| 90 | + else |
| 91 | + TARGET_FILENAME=$(basename "$GENERATED") |
| 92 | + fi |
| 93 | + CHANGELOG_FILE="$CHANGELOG_DIR/$TARGET_FILENAME" |
| 94 | +
|
| 95 | + if [ -f "$CHANGELOG_FILE" ]; then |
| 96 | + VERB="Update" |
| 97 | + else |
| 98 | + VERB="Add" |
| 99 | + fi |
| 100 | +
|
| 101 | + mkdir -p "$CHANGELOG_DIR" |
| 102 | + cp "$GENERATED" "$CHANGELOG_FILE" |
| 103 | +
|
| 104 | + git config user.name "github-actions[bot]" |
| 105 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 106 | + git add "$CHANGELOG_FILE" |
| 107 | +
|
| 108 | + if git diff --cached --quiet; then |
| 109 | + echo "No changes to commit" |
| 110 | + echo "committed=false" >> "$GITHUB_OUTPUT" |
| 111 | + else |
| 112 | + git commit -m "${VERB} changelog for PR #${PR_NUMBER}" |
| 113 | + push_failed=false |
| 114 | + if [[ "$IS_FORK" == "true" ]]; then |
| 115 | + git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${HEAD_REPO}.git" |
| 116 | + if ! git push origin "HEAD:refs/heads/${HEAD_REF}" 2>&1; then |
| 117 | + echo "::warning::Push to fork failed — maintainer_can_modify may have been revoked after evaluation" |
| 118 | + push_failed=true |
| 119 | + fi |
| 120 | + else |
| 121 | + git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${GIT_REPOSITORY}.git" |
| 122 | + git push |
| 123 | + fi |
| 124 | + git remote set-url origin "https://github.com/${GIT_REPOSITORY}.git" |
| 125 | +
|
| 126 | + if [[ "$push_failed" == "true" ]]; then |
| 127 | + echo "committed=false" >> "$GITHUB_OUTPUT" |
| 128 | + echo "push-failed=true" >> "$GITHUB_OUTPUT" |
| 129 | + else |
| 130 | + echo "committed=true" >> "$GITHUB_OUTPUT" |
| 131 | + fi |
| 132 | + fi |
| 133 | +
|
| 134 | + echo "changelog-file=$CHANGELOG_FILE" >> "$GITHUB_OUTPUT" |
| 135 | +
|
| 136 | + - name: Post success comment |
| 137 | + if: steps.commit.outputs.committed == 'true' |
| 138 | + uses: actions/github-script@v9 |
| 139 | + env: |
| 140 | + PR_NUMBER: ${{ steps.meta.outputs.pr-number }} |
| 141 | + HEAD_REF: ${{ steps.meta.outputs.head-ref }} |
| 142 | + CHANGELOG_FILE: ${{ steps.commit.outputs.changelog-file }} |
| 143 | + with: |
| 144 | + github-token: ${{ inputs.github-token }} |
| 145 | + script: | |
| 146 | + const script = require('${{ github.action_path }}/scripts/post-success-comment.js'); |
| 147 | + await script({ github, context, core }); |
| 148 | +
|
| 149 | + - name: Post push-fallback comment |
| 150 | + if: steps.commit.outputs.push-failed == 'true' |
| 151 | + uses: actions/github-script@v9 |
| 152 | + env: |
| 153 | + PR_NUMBER: ${{ steps.meta.outputs.pr-number }} |
| 154 | + CHANGELOG_DIR: ${{ steps.meta.outputs.changelog-dir }} |
| 155 | + STAGING_DIR: /tmp/changelog-staging |
| 156 | + with: |
| 157 | + github-token: ${{ inputs.github-token }} |
| 158 | + script: | |
| 159 | + const script = require('${{ github.action_path }}/scripts/post-push-fallback.js'); |
| 160 | + await script({ github, context, core }); |
| 161 | +
|
| 162 | + - name: Post comment-only changelog |
| 163 | + if: steps.meta.outputs.should-comment-success == 'true' |
| 164 | + uses: actions/github-script@v9 |
| 165 | + env: |
| 166 | + PR_NUMBER: ${{ steps.meta.outputs.pr-number }} |
| 167 | + CHANGELOG_DIR: ${{ steps.meta.outputs.changelog-dir }} |
| 168 | + STAGING_DIR: /tmp/changelog-staging |
| 169 | + with: |
| 170 | + github-token: ${{ inputs.github-token }} |
| 171 | + script: | |
| 172 | + const script = require('${{ github.action_path }}/scripts/post-comment-only.js'); |
| 173 | + await script({ github, context, core }); |
| 174 | +
|
| 175 | + - name: Post failure comment |
| 176 | + if: steps.meta.outputs.should-comment-failure == 'true' |
| 177 | + uses: actions/github-script@v9 |
| 178 | + env: |
| 179 | + PR_NUMBER: ${{ steps.meta.outputs.pr-number }} |
| 180 | + LABEL_TABLE: ${{ steps.meta.outputs.label-table }} |
| 181 | + PRODUCT_LABEL_TABLE: ${{ steps.meta.outputs.product-label-table }} |
| 182 | + SKIP_LABELS: ${{ steps.meta.outputs.skip-labels }} |
| 183 | + CONFIG_FILE: ${{ inputs.config }} |
| 184 | + with: |
| 185 | + github-token: ${{ inputs.github-token }} |
| 186 | + script: | |
| 187 | + const script = require('${{ github.action_path }}/scripts/post-failure-comment.js'); |
| 188 | + await script({ github, context, core }); |
0 commit comments