Skip to content

DA GitHub Release

DA GitHub Release #53

Workflow file for this run

# trigger manually or on CRON
# validate inputs
# checkout repository at master
# bump version
# generate and update changelog
# update master
# merges new master to release
# create a GitHub release
# this workflow is expected to run on master branch when triggered manually, but should run fine in all cases
# note that you are expected to enter major, minor, or patch whenever you plan on creating a release and deploying to SVN
# the conditionals should preent creating a release unless the version was incremented, otherwise you may face conflicts with duplicate tags
name: DA GitHub Release
env:
OTTO_TOKEN: ${{ secrets.OTTO_GITHUB_PAT }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
on:
schedule:
# release every Tuesday at 5pm UTC, LA time 9am
- cron: '0 17 * * 2'
workflow_dispatch:
inputs:
version_type:
description: "Version type (major, minor, patch or leave empty to release current master branch)"
required: false
default: ""
deploy:
description: "Deploy to WordPress.org (true or false)"
required: false
default: "false"
jobs:
github-release:
name: "Create a GitHub Release"
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: "Dump GitHub Context"
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: |
echo "$GITHUB_CONTEXT"
- name: "Checkout repository"
uses: actions/checkout@v3.3.0
with:
ssh-key: ${{ secrets.GH_ACTIONS_SSH_KEY }}
ref: master
- name: "Check defined version type"
id: version
run: |
if [ "${{ github.event_name }}" == "schedule" ]; then
echo "version=patch" >> $GITHUB_OUTPUT
elif [ "${{ github.event.inputs.version_type }}" == "major" ]; then
echo "version=major" >> $GITHUB_OUTPUT
elif [ "${{ github.event.inputs.version_type }}" == "minor" ]; then
echo "version=minor" >> $GITHUB_OUTPUT
elif [ "${{ github.event.inputs.version_type }}" == "patch" ]; then
echo "version=patch" >> $GITHUB_OUTPUT
else
echo "version=current" >> $GITHUB_OUTPUT
fi
# =====================================
# Edit Release Version ================
# =====================================
- name: "Git Configuration"
run: |
git config --global user.email "nathan@tylerdigital.com"
git config --global user.name "tylerdigital"
- name: "Bump package.json version number"
if: ${{ steps.version.outputs.version != 'current' }}
run: |
npm version ${{ steps.version.outputs.version }} --no-git-tag-version
- name: "Get Package Version"
id: package-version
uses: martinbeentjes/npm-get-version-action@master
- name: "Update version on php and readme files"
if: ${{ steps.version.outputs.version != 'current' }}
run: |
npm install
grunt increment-version
# =====================================
# Build Dev Changelog =================
# =====================================
- name: "Build Dev Changelog"
uses: mikepenz/release-changelog-builder-action@v4.0.0-b02
id: build_dev_changelog
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
toTag: ${{ github.ref }}
configuration: "./.github/config/dev_changelog_configuration.json"
# if scheduled and no pull requests caught in changelog, exit
- name: "Exit if no pull requests caught in changelog"
if: github.event_name == 'schedule' && steps.build_dev_changelog.outputs.pull_requests == ''
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "No pull requests caught in changelog. Exiting."
gh run cancel ${{ github.run_id }}
gh run watch ${{ github.run_id }}
# =====================================
# Build Changelog =====================
# =====================================
- name: "Build Changelog"
uses: mikepenz/release-changelog-builder-action@v4.0.0-b02
id: build_changelog
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
toTag: ${{ github.ref }}
configuration: "./.github/config/changelog_configuration.json"
- name: "Write changelog step outputs to console"
env:
CHANGELOG: ${{ toJson(steps.build_changelog.outputs) }}
run: |
echo "$CHANGELOG"
- name: "Update Changelog"
if: steps.build_changelog.outputs.changelog != '' && steps.build_changelog.outputs.pull_requests != ''
uses: stefanzweifel/changelog-updater-action@v1
with:
latest-version: "${{ steps.package-version.outputs.current-version }}"
release-notes: ${{steps.build_changelog.outputs.changelog}}
- name: "Update readme.txt from CHANGELOG.md"
if: steps.build_changelog.outputs.changelog != '' && steps.build_changelog.outputs.pull_requests != ''
run: |
# delete current changelog
sed -i '/# Changelog/,${d}' readme.txt
# get top N lines from CHANGELOG.md and append in readme.txt
echo "$( head -70 CHANGELOG.md )" >> readme.txt
# append some empty lines
echo "" >> readme.txt
echo "" >> readme.txt
# append link to the complete changelog
echo "For older changelog entries, please see the [additional changelog.txt file](https://plugins.svn.wordpress.org/draw-attention/trunk/CHANGELOG.md) delivered with the plugin." >> readme.txt
# =====================================
# Update branches =====================
# =====================================
- name: 'Update branch master'
if: steps.version.outputs.version != 'current' || ( steps.build_changelog.outputs.changelog != '' && steps.build_changelog.outputs.pull_requests != '' )
run: |
git add -A .
git commit -m "Update to ${{ steps.package-version.outputs.current-version }} [version-bump]"
git push origin master
- name: 'Merge master -> release'
id: merge-master-release
run: |
git fetch origin --unshallow
git checkout -b release origin/release
git merge master
git push origin release -f
# =====================================
# Prepare Pre-release Asset ===========
# =====================================
- name: "Package the App"
run: |
npm install
grunt wporg
- name: "List Files Included In Release"
run: |
echo "-Listing included files-"
ls release/svn
echo "-Finished listing included files-"
- name: "Package Included Files to Upload to GitHub Release"
id: zip_build
env:
ZIP_FILENAME: "${{ github.event.repository.name }}-${{ steps.package-version.outputs.current-version }}-prerelease.zip"
run: |
mv release/svn draw-attention
zip -r $ZIP_FILENAME draw-attention
echo "-Listing files in working directory-"
ls
# =====================================
# Cleanup if Needed ===================
# =====================================
- name: "If tag exists - delete tag to avoid conflict"
uses: dev-drprasad/delete-tag-and-release@v1.0
with:
tag_name: "${{ steps.package-version.outputs.current-version }}"
github_token: ${{ secrets.GITHUB_TOKEN }}
delete_release: false
# =====================================
# Create GitHub release ===============
# =====================================
- name: "GH Release"
uses: softprops/action-gh-release@v0.1.15
env:
ZIP_FILENAME: "${{ github.event.repository.name }}-${{ steps.package-version.outputs.current-version }}-prerelease.zip"
with:
prerelease: true
token: ${{ env.OTTO_TOKEN }}
body: ${{steps.build_changelog.outputs.changelog}}
tag_name: "${{ steps.package-version.outputs.current-version }}"
files: |
${{ env.ZIP_FILENAME }}
- name: "Get release ID"
id: release-data
run: |
response=$(curl -s https://api.github.com/graphql -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -d '{ "query": "query { repository(owner: \"nsquared-team\", name: \"draw-attention\") { release(tagName: \"${{ steps.package-version.outputs.current-version }}\") { databaseId createdAt } } }" }')
databaseId=$(echo "$response" | jq -r '.data.repository.release.databaseId')
if [[ -z "$databaseId" ]]; then
echo "Error: databaseId is empty"
exit 1
fi
echo "release-id=$databaseId" >> $GITHUB_OUTPUT
- name: "Update release to be latest"
run: |
curl -L \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$GITHUB_REPOSITORY/releases/${{ steps.release-data.outputs.release-id }} \
-d '{"make_latest":true}'
# =====================================
# Send Failure Warning to Slack =======
# =====================================
- name: "Send Failure Warning to Slack"
if: failure()
uses: slackapi/slack-github-action@v1.23.0
env:
JSON: "{\"text\":\"\\n:red_circle: Draw Attention ${{ steps.package-version.outputs.current-version }}\\nFailed to Prepare GitHub release.\\n\\nKindly check what went wrong, or try triggering the release workflow again.\\n\",\"blocks\":[{\"type\":\"divider\"},{\"type\":\"header\",\"text\":{\"type\":\"plain_text\",\"text\":\"\\n:red_circle: Draw Attention ${{ steps.package-version.outputs.current-version }}\\n\"}},{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"Failed to Prepare GitHub release.\\n\\nKindly check what went wrong, or try triggering the release workflow again.\\n\"}}]}"
with:
payload: ${{ env.JSON }}
# =====================================
# Send Dev Changelog Notification =====
# =====================================
- name: "Format changelog for slack"
id: format-dev-changelog
env:
CHANGELOG_JSON: ${{ steps.build_dev_changelog.outputs.categorized }}
CHANGELOG_STRING: ${{ steps.build_dev_changelog.outputs.changelog }}
PROJECT: "Draw Attention"
run: |
php "external/actions-scripts/format-changelog.php" --version=${{ steps.package-version.outputs.current-version }} --repo=${{ github.repository }} --out="formatted-dev-changelog.json"
- name: "View JSON file"
run: |
cat "formatted-dev-changelog.json"
- name: "Send Dev Changelog Notification to Slack"
if: success()
run: |
json_content=$(cat "formatted-dev-changelog.json")
curl -X POST -H "Content-type: application/json" --data "$json_content" "${{ env.SLACK_WEBHOOK_URL }}"
# ===================================================================
# Trigger deploy workflow only if the workflow was triggered on cron
# ===================================================================
- name: "Trigger deploying to WordPress.org"
env:
GH_TOKEN: ${{ github.token }}
if: success() && ( github.event_name == 'schedule' || github.event.inputs.deploy == 'true' )
run: |
echo 'Sleeping for 5 minutes to allow GitHub to create the release'
sleep 300
gh workflow run deploy.yml -f tag_name="${{ steps.package-version.outputs.current-version }}"