|
| 1 | +name: "Automatically tag new version" |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + secrets: |
| 6 | + github-token: |
| 7 | + required: true |
| 8 | + |
| 9 | +jobs: |
| 10 | + auto-tag-new-version: |
| 11 | + name: "Automatically tag new version" |
| 12 | + runs-on: "ubuntu-latest" |
| 13 | + steps: |
| 14 | + - name: "Checkout" |
| 15 | + uses: "actions/checkout@v4" |
| 16 | + with: |
| 17 | + fetch-depth: 0 # see https://github.com/actions/checkout/issues/1471 |
| 18 | + fetch-tags: true |
| 19 | + persist-credentials: false |
| 20 | + - name: "Extract version" |
| 21 | + run: | |
| 22 | + sudo apt-get update |
| 23 | + sudo apt-get install --assume-yes --no-install-recommends --quiet pcregrep |
| 24 | + VERSION=$(pcregrep -o1 "define\s*\(\s*['\"]PLUGIN_[A-Z]+_VERSION['\"]\s*,\s*['\"]([^'\"]+)['\"]\s*\)" setup.php) |
| 25 | + echo "VERSION=${VERSION}" >> $GITHUB_ENV |
| 26 | + - name: "Check if tag exists" |
| 27 | + run: | |
| 28 | + CREATE_TAG=no |
| 29 | + if [[ ! -z "${{ env.VERSION }}" && -z "$(git tag -l ${{ env.VERSION }})" ]]; then |
| 30 | + CREATE_TAG=yes |
| 31 | + fi |
| 32 | + echo "CREATE_TAG=${CREATE_TAG}" >> $GITHUB_ENV |
| 33 | + - name: "Create release" |
| 34 | + if: ${{ env.CREATE_TAG == 'yes' }} |
| 35 | + uses: "actions/github-script@v7" |
| 36 | + with: |
| 37 | + # A PAT is required. |
| 38 | + # From https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow: |
| 39 | + # > When you use the repository's GITHUB_TOKEN to perform tasks, events triggered by the GITHUB_TOKEN, |
| 40 | + # > with the exception of workflow_dispatch and repository_dispatch, will not create a new workflow run. |
| 41 | + # > This prevents you from accidentally creating recursive workflow runs. For example, if a workflow run pushes |
| 42 | + # > code using the repository's GITHUB_TOKEN, a new workflow will not run even when the repository |
| 43 | + # > contains a workflow configured to run when push events occur. |
| 44 | + github-token: ${{ secrets.github-token }} |
| 45 | + script: | |
| 46 | + try { |
| 47 | + github.rest.git.createRef( |
| 48 | + { |
| 49 | + ref: 'refs/tags/${{ env.VERSION }}', |
| 50 | + sha: context.sha, |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + } |
| 54 | + ); |
| 55 | + } catch (error) { |
| 56 | + core.setFailed(error.message); |
| 57 | + } |
0 commit comments