Posts: Automatic WordPress Theme Version with GitHub Actions
Automatically replace a string in style.css with the latest tag name during git archive
One of the things I do on a regular basis involves releasing new versions of custom WordPress themes for the various websites my company maintains. Since we recently migrated everything from Subversion to Git with GitHub, I added a GitHub Action, .github/workflows/release-action.yml, to each theme that automatically packages the themes in a way that WordPress likes:
| |
WordPress wants the theme’s folder to be the first thing at the root of the .zip flie, which is accomplished by setting --prefix in git archive.
It took some searching, but Anton wrote a great article on setting up .gitattributes to automatically replace strings in files during the archival process:
| |
Then, in style.css, set the Version: value to $Format:%(describe:tags=true)$:
| |
Once this is all done, commit and go into GitHub to create a new release with a new, or existing, tag. After a minute or two, the new file will appear and the tag name you just created will be the new version.
With a little goofing around, I did find that doing a git archive with commits after the latest tag set the version to something like: 0.0.0-2-gce6902f where 0.0.0 is the tag, 2 is the number of commits after the tag, g might mean “git”, and ce6902f is the commit hash.