|
| 1 | +name: Build a zip, Comment on PR & Delete a zip on closed |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: [opened, synchronize, ready_for_review, closed, labeled] |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - dev |
| 8 | + - next-release |
| 9 | + - milestone |
| 10 | + paths: |
| 11 | + - "src/**" |
| 12 | + - "includes/**" |
| 13 | + - "ultimate-addons-for-gutenberg.php" |
| 14 | + - "package.json" |
| 15 | + - "package-lock.json" |
| 16 | + - "webpack.config.js" |
| 17 | + - ".github/workflows/build-a-zip.yml" |
| 18 | + |
| 19 | +# Cancels all previous workflow runs for pull requests that have not completed. |
| 20 | +concurrency: |
| 21 | + # The concurrency group contains the workflow name and the branch name for pull requests |
| 22 | + # or the commit hash for any other events. |
| 23 | + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} |
| 24 | + cancel-in-progress: true |
| 25 | + |
| 26 | +env: |
| 27 | + ZIP_URL: https://dev.ultimategutenberg.com/wp-content/uploads/uag-gh/ |
| 28 | + PLUGIN_SLUG: ${{ github.event.repository.name }} |
| 29 | + ZIP_NAME: "${{ github.event.repository.name }}.zip" |
| 30 | + |
| 31 | +jobs: |
| 32 | + build-zip: |
| 33 | + name: "Build a zip and upload to server" |
| 34 | + if: ${{ github.event.action != 'closed' && github.event.label.name == 'create zip' }} |
| 35 | + runs-on: ubuntu-latest |
| 36 | + outputs: |
| 37 | + branch_name: ${{ steps.get_current_branch.outputs.branch_name }} |
| 38 | + steps: |
| 39 | + - name: "Checkout" |
| 40 | + uses: actions/checkout@v4 |
| 41 | + |
| 42 | + - name: Use desired version of NodeJS |
| 43 | + uses: actions/setup-node@v4 |
| 44 | + with: |
| 45 | + node-version: '20' |
| 46 | + cache: 'npm' |
| 47 | + |
| 48 | + - name: Extract current branch name |
| 49 | + id: get_current_branch |
| 50 | + run: echo "branch_name=$(REF=${GITHUB_HEAD_REF:-$GITHUB_REF} && echo ${REF#refs/heads/} | sed 's/\//-/g')" >> $GITHUB_OUTPUT |
| 51 | + |
| 52 | + - name: Install dependencies |
| 53 | + run: npm ci |
| 54 | + |
| 55 | + - name: Create a build |
| 56 | + run: npm run build |
| 57 | + |
| 58 | + - name: Build a package |
| 59 | + run: bash ./bin/build-zip.sh |
| 60 | + |
| 61 | + - name: Create branch directory |
| 62 | + run: | |
| 63 | + cd artifact |
| 64 | + mkdir -p ${{ steps.get_current_branch.outputs.branch_name }} |
| 65 | + mv ${{ env.ZIP_NAME }} ${{ steps.get_current_branch.outputs.branch_name }}/ |
| 66 | +
|
| 67 | + - name: Upload a zip to server |
| 68 | + uses: burnett01/rsync-deployments@4.1 |
| 69 | + with: |
| 70 | + # switches: -avzr --delete --delete-excluded --exclude-from=".distignore" --include="" --filter="" |
| 71 | + switches: -avzr |
| 72 | + path: ./artifact/ |
| 73 | + remote_path: ${{ secrets.SSH_ZIP_PATH }} |
| 74 | + remote_host: ${{ secrets.SSH_HOST }} |
| 75 | + remote_port: ${{ secrets.SSH_PORT }} |
| 76 | + remote_user: ${{ secrets.SSH_USER }} |
| 77 | + remote_key: ${{ secrets.SSH_KEY }} |
| 78 | + |
| 79 | + comment-on-pr: |
| 80 | + name: Comment on PR with links to plugin ZIPs |
| 81 | + if: ${{ github.event.action != 'closed' && github.head_ref && github.head_ref != null && github.event.label.name == 'create zip' }} |
| 82 | + runs-on: ubuntu-latest |
| 83 | + needs: build-zip |
| 84 | + env: |
| 85 | + CI: true |
| 86 | + GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }} |
| 87 | + outputs: |
| 88 | + pr_number: ${{ steps.get_pr_number.outputs.pr_number }} |
| 89 | + comment_body: ${{ steps.get_comment_body.outputs.body }} |
| 90 | + steps: |
| 91 | + - name: Get PR number |
| 92 | + id: get_pr_number |
| 93 | + run: echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT |
| 94 | + |
| 95 | + - name: Get comment body |
| 96 | + id: get_comment_body |
| 97 | + run: | |
| 98 | + body="Plugin zip for ${{ github.event.pull_request.head.sha }} is ready :bellhop_bell:! |
| 99 | + - Download the plugin [zip](${{ env.ZIP_URL }}${{ needs.build-zip.outputs.branch_name }}/${{ env.ZIP_NAME }})" |
| 100 | + body="${body//$'\n'/'%0A'}" |
| 101 | + echo "body=$body" >> $GITHUB_OUTPUT |
| 102 | +
|
| 103 | + - name: Find an old comment if any |
| 104 | + uses: sandeshjangam/comment-actions@v1 |
| 105 | + id: find_comment |
| 106 | + with: |
| 107 | + type: find |
| 108 | + number: ${{ steps.get_pr_number.outputs.pr_number }} |
| 109 | + search_term: Download the plugin [zip] |
| 110 | + |
| 111 | + - name: Delete an old comment |
| 112 | + if: ${{ steps.find_comment.outputs.comment_id != '' }} |
| 113 | + uses: sandeshjangam/comment-actions@v1 |
| 114 | + with: |
| 115 | + type: delete |
| 116 | + comment_id: ${{ steps.find_comment.outputs.comment_id }} |
| 117 | + |
| 118 | + - name: Create a new comment with a download link of plugin zip |
| 119 | + uses: sandeshjangam/comment-actions@v1 |
| 120 | + with: |
| 121 | + type: create |
| 122 | + number: ${{ steps.get_pr_number.outputs.pr_number }} |
| 123 | + body: ${{ steps.get_comment_body.outputs.body }} |
| 124 | + reactions: hooray, heart, rocket |
| 125 | + |
| 126 | + clean-zip: |
| 127 | + name: "Clean a zip on server" |
| 128 | + if: ${{ github.event.action == 'closed' }} |
| 129 | + runs-on: ubuntu-latest |
| 130 | + steps: |
| 131 | + - name: "Checkout" |
| 132 | + uses: actions/checkout@v4 |
| 133 | + |
| 134 | + - name: Extract current branch name |
| 135 | + id: get_current_branch |
| 136 | + run: | |
| 137 | + echo "branch_name=$(REF=${GITHUB_HEAD_REF:-$GITHUB_REF} && echo ${REF#refs/heads/} | sed 's/\//-/g')" >> $GITHUB_OUTPUT |
| 138 | + echo ${{ steps.get_current_branch.outputs.branch_name }} |
| 139 | +
|
| 140 | + - name: Execute a delete command using password |
| 141 | + if: ${{ steps.get_current_branch.outputs.branch_name != '' }} |
| 142 | + uses: appleboy/ssh-action@v1.0.3 |
| 143 | + with: |
| 144 | + host: ${{ secrets.SSH_HOST }} |
| 145 | + port: ${{ secrets.SSH_PORT }} |
| 146 | + username: ${{ secrets.SSH_USER }} |
| 147 | + key: ${{ secrets.SSH_KEY }} |
| 148 | + script: | |
| 149 | + cd ${{ secrets.SSH_ZIP_PATH }} |
| 150 | + rm -rf ${{ steps.get_current_branch.outputs.branch_name }} |
0 commit comments