GitHub Action

Learn how to integrate the AutomagicWP GitHub Action for automating your releases.

This guide will show you how to integrate the GitHub Action into your workflow. You can automate your build and release processes with ease. This method works for public and private repositories.

Prerequisites

  • Basic knowledge of GitHub and GitHub Actions
  • A GitHub repository (public or private)
  • A WordPress plugin to automate

Setting up GitHub Action

This action consists of two main steps:

  • Building your project and uploading the artifact
  • Creating a new version on AutomagicWP

The following example demonstrates how to set up a GitHub Actions workflow to automate the release process for your WordPress plugin. This workflow triggers on a new release event and uploads the build artifact to AutomagicWP.

Tip: In this example, the wplatest/action GitHub Action is pinned to the main branch. It's good practice to pin the action to a specific version to avoid breaking changes (e.g. wplatest/action@1.0.0) after testing the workflow.

Warning: Ensure that your GitHub token has the necessary permissions to access the repository and perform the required actions. This is crucial for the workflow to function correctly because we use the GitHub token to get the artifact download URL. An example on setting the right permissions is provided in the permissions section.

Workflow example

	# .github/workflows/release.yml
name: Release Build
 
on:
  release:
    types: [created]
 
jobs:
  build:
    runs-on: ubuntu-latest
 
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
 
      # Optional: Set up PHP
      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
 
      # Optional: Build your project
      - name: Install Composer dependencies
        run: composer install --no-dev --optimize-autoloader
 
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        id: upload-artifact-zip
        with:
          name: 'plugin-zip'
          path: |
            ./
            !./composer.json
            !./.vscode
            !./.github
            !./.git
            !./phpcs.xml
            !./composer.lock
            !./README.md
            !./.sftpignore
            !./.gitignore
 
      - name: Get artifact details
        id: get-artifact-details
        run: |
          ARTIFACT_ID=${{ steps.upload-artifact-zip.outputs.artifact-id }}
          echo "{"id": "$ARTIFACT_ID"}" > artifact_id.json
 
      - name: Upload artifact ID
        uses: actions/upload-artifact@v4
        with:
          name: artifact-id
          path: artifact_id.json
          overwrite: true
 
  wp-deploy:
    runs-on: ubuntu-latest
    needs: build
    permissions:
      contents: read
      actions: write
    steps:
      - name: Set up authorization
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: echo "TOKEN=${GITHUB_TOKEN}" >> $GITHUB_ENV
 
      - name: Download artifact ID
        uses: actions/download-artifact@v4
        with:
          name: artifact-id
          path: .
 
      - name: Get artifact download URL
        id: get-artifact-url
        run: |
          ARTIFACT_ID=$(cat artifact_id.json | jq -r '.id')
          API_URL="https://api.github.com/repos/${{ github.repository }}/actions/artifacts/${ARTIFACT_ID}/zip"
          LOCATION=$(curl -s -H "Authorization: token ${TOKEN}" -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" -D - "${API_URL}" | grep -i location: | awk '{print $2}' | tr -d '\r')
 
          if [ -z "${LOCATION}" ]; then
            echo "No location header found in response"
            exit 1
          fi
 
          echo "::set-output name=ARTIFACT_URL::${LOCATION}"
 
      - name: Create new version on AutomagicWP
        uses: wplatest/action@main
        id: new-version
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          wplatest-plugin-id: ${{ secrets.WPLATEST_PLUGIN_ID }}
          wplatest-action: create-new-version
          wplatest-artifact-zip-url: ${{ steps.get-artifact-url.outputs.ARTIFACT_URL }}
          wplatest-secret: ${{ secrets.WPLATEST_SECRET }}

Workflow breakdown

  1. Checkout Repository — Uses actions/checkout@v4 to checkout your repository and access the code.
  2. Set up dependencies — Optional steps to set up PHP and build your project. Customize based on your requirements.
  3. Upload Artifact — Uses actions/upload-artifact@v4 to upload the build artifact. The path parameter specifies which files to include or exclude.
  4. Get Artifact Details — Captures the artifact ID using a shell script and stores it in a JSON file.
  5. Upload Artifact ID — Uploads the artifact ID JSON file so it's accessible in later steps.
  6. Set up Authorization — Adds the GitHub token to environment variables for authenticating API requests.
  7. Download Artifact ID — Downloads the previously uploaded artifact ID JSON file.
  8. Get Artifact Download URL — Constructs the download URL for the artifact using the GitHub API.
  9. Create New Version — Uses wplatest/action@main to create a new version of your plugin on AutomagicWP.

Walkthrough

Ensure that you replace placeholder values such as:

  • WPLATEST_PLUGIN_ID with your AutomagicWP plugin ID (found on the "Settings" page of your plugin)
  • WPLATEST_SECRET with your AutomagicWP API secret

After setting up the workflow, create a new release in your repository. This triggers the workflow, which builds your project, uploads the artifact, and creates a new version on AutomagicWP.

Next Steps

After setting up your GitHub Actions workflow, you can further automate your release process:

Troubleshooting

Permission Issues

Ensure that your GitHub token has the correct permissions to access the repository and perform the required actions.

Artifact Download Issues

Double-check the artifact ID and ensure the download URL is correctly retrieved from the GitHub API.

Workflow Errors

Use the logs provided by GitHub Actions to debug any errors in your workflow steps.

Need help? Email support@automagicwp.com