|
| 1 | +--- |
| 2 | +name: Version bump ⬆ |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + workflow_dispatch: |
| 9 | + workflow_call: |
| 10 | + secrets: |
| 11 | + REPO_GITHUB_TOKEN: |
| 12 | + description: Github token with write access to the repo |
| 13 | + required: false |
| 14 | + inputs: |
| 15 | + disable-precommit-autoupdate: |
| 16 | + description: Disable precommit autoupdate |
| 17 | + required: false |
| 18 | + default: false |
| 19 | + type: boolean |
| 20 | + vbump-after-release: |
| 21 | + description: Whether the vbump workflow is running after a release has been published |
| 22 | + required: false |
| 23 | + default: false |
| 24 | + type: boolean |
| 25 | + package-subdirectory: |
| 26 | + description: Subdirectory in the repository, where the R package is located. |
| 27 | + required: false |
| 28 | + type: string |
| 29 | + default: "." |
| 30 | + |
| 31 | +concurrency: |
| 32 | + group: vbump-${{ github.event.pull_request.number || github.ref }} |
| 33 | + cancel-in-progress: true |
| 34 | + |
| 35 | +jobs: |
| 36 | + vbump: |
| 37 | + name: Bump version ⤴ |
| 38 | + runs-on: ubuntu-latest |
| 39 | + if: | |
| 40 | + !(contains(github.event.commits[0].message, '[skip vbump]') || |
| 41 | + contains(github.event.head_commit.message, '[skip vbump]') |
| 42 | + ) |
| 43 | + container: |
| 44 | + image: docker.io/rocker/tidyverse:latest |
| 45 | + |
| 46 | + steps: |
| 47 | + - name: Setup token 🔑 |
| 48 | + id: github-token |
| 49 | + run: | |
| 50 | + if [ "${{ secrets.REPO_GITHUB_TOKEN }}" == "" ]; then |
| 51 | + echo "REPO_GITHUB_TOKEN is empty. Substituting it with GITHUB_TOKEN." |
| 52 | + echo "token=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_OUTPUT |
| 53 | + else |
| 54 | + echo "Using REPO_GITHUB_TOKEN." |
| 55 | + echo "token=${{ secrets.REPO_GITHUB_TOKEN }}" >> $GITHUB_OUTPUT |
| 56 | + fi |
| 57 | + shell: bash |
| 58 | + |
| 59 | + - name: Get branch names 🌿 |
| 60 | + id: branch-name |
| 61 | + uses: tj-actions/branch-names@v7 |
| 62 | + |
| 63 | + - name: Checkout repo 🛎 |
| 64 | + uses: actions/checkout@v4.1.1 |
| 65 | + with: |
| 66 | + ref: ${{ steps.branch-name.outputs.head_ref_branch }} |
| 67 | + token: ${{ steps.github-token.outputs.token }} |
| 68 | + |
| 69 | + - name: Bump version in DESCRIPTION 📜 |
| 70 | + if: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/main') || inputs.vbump-after-release == true }} |
| 71 | + run: desc::desc_bump_version("dev", normalize = TRUE) |
| 72 | + shell: Rscript {0} |
| 73 | + working-directory: ${{ inputs.package-subdirectory }} |
| 74 | + |
| 75 | + - name: Update Date field 📅 |
| 76 | + run: if (desc::desc_has_fields("Date")) desc::desc_set("Date", Sys.Date()) |
| 77 | + shell: Rscript {0} |
| 78 | + working-directory: ${{ inputs.package-subdirectory }} |
| 79 | + |
| 80 | + - name: Bump version in NEWS.md 📰 |
| 81 | + run: | |
| 82 | + if [ -f NEWS.md ] |
| 83 | + then { |
| 84 | + git config --global --add safe.directory $(pwd) |
| 85 | + DESC_VERSION=$(R --slave -e 'cat(paste(desc::desc_get_version()))' | tr -d '\n' | xargs) |
| 86 | + NEWS_VERSION=$(awk '/^#+ /{print $3,$4; exit}' NEWS.md | tr -d '\n' | xargs) |
| 87 | + FIRST_NEWS_LINE=$(head -1 NEWS.md) |
| 88 | + if [ "${{ inputs.vbump-after-release }}" == "true" ]; then |
| 89 | + # Add a new section with the released version that will be vbumped below. |
| 90 | + printf "$FIRST_NEWS_LINE\n\n" | cat - NEWS.md > temp-news.md |
| 91 | + mv temp-news.md NEWS.md |
| 92 | + fi |
| 93 | + # Replace only the first occurence of $NEWS_VERSION, |
| 94 | + # but only if it's not already set to (development version) |
| 95 | + if [ $NEWS_VERSION != "(development version)" ] |
| 96 | + then { |
| 97 | + sed -i "0,/$NEWS_VERSION/s/$NEWS_VERSION/$DESC_VERSION/" NEWS.md |
| 98 | + } |
| 99 | + fi |
| 100 | + echo "NEW_PKG_VERSION=${DESC_VERSION}" >> $GITHUB_ENV |
| 101 | + } |
| 102 | + fi |
| 103 | + shell: bash |
| 104 | + working-directory: ${{ inputs.package-subdirectory }} |
| 105 | + |
| 106 | + - name: Check if a pre-commit config exists |
| 107 | + id: precommit-config-exists |
| 108 | + uses: andstor/file-existence-action@v3 |
| 109 | + with: |
| 110 | + files: ".pre-commit-config.yaml" |
| 111 | + |
| 112 | + - name: Setup Python 🐍 |
| 113 | + if: | |
| 114 | + inputs.disable-precommit-autoupdate != 'true' && |
| 115 | + steps.precommit-config-exists.outputs.files_exists == 'true' |
| 116 | + uses: actions/setup-python@v5 |
| 117 | + with: |
| 118 | + python-version: '3.12' |
| 119 | + |
| 120 | + - name: Precommit autoupdate 🅿️ |
| 121 | + if: | |
| 122 | + inputs.disable-precommit-autoupdate != 'true' && |
| 123 | + steps.precommit-config-exists.outputs.files_exists == 'true' |
| 124 | + run: | |
| 125 | + git config --global --add safe.directory $(pwd) |
| 126 | + python -m pip -q install pre-commit |
| 127 | + pre-commit autoupdate |
| 128 | +
|
| 129 | + - name: Checkout to main 🛎 |
| 130 | + if: ${{ inputs.vbump-after-release == true }} |
| 131 | + run: | |
| 132 | + git fetch origin main |
| 133 | + git checkout main |
| 134 | + git pull origin main |
| 135 | +
|
| 136 | + - name: Set file pattern to commit ⚙️ |
| 137 | + id: file-pattern |
| 138 | + run: | |
| 139 | + if [[ "${{ inputs.package-subdirectory }}" == "." || "${{ inputs.package-subdirectory }}" == "" ]]; then |
| 140 | + FILE_PATTERN="NEWS.md DESCRIPTION" |
| 141 | + else |
| 142 | + FILE_PATTERN="${{ inputs.package-subdirectory }}/NEWS.md ${{ inputs.package-subdirectory }}/DESCRIPTION" |
| 143 | + fi |
| 144 | + if [[ '${{ steps.precommit-config-exists.outputs.files_exists }}' == 'true' ]]; then |
| 145 | + FILE_PATTERN="$FILE_PATTERN .pre-commit-config.yaml" |
| 146 | + fi |
| 147 | + echo "file-pattern=$FILE_PATTERN" >> $GITHUB_OUTPUT |
| 148 | + shell: bash |
| 149 | + |
| 150 | + - name: Commit and push changes 📌 |
| 151 | + if: ${{ env.NEW_PKG_VERSION }} |
| 152 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 153 | + with: |
| 154 | + commit_message: "[skip actions] Bump version to ${{ env.NEW_PKG_VERSION }}" |
| 155 | + file_pattern: "${{ steps.file-pattern.outputs.file-pattern }}" |
| 156 | + commit_user_name: github-actions |
| 157 | + commit_user_email: >- |
| 158 | + 41898282+github-actions[bot]@users.noreply.github.com |
| 159 | + continue-on-error: true |
0 commit comments