In the software development stages, deployment is one of the most problem causing steps, even if your development pipeline is perfectly set, this is the moment when all the bugs of the software that did not was caught during the building or by the Quality assurance process will be shipped to the end-users, of course, luckily WordPress development with GitHub actions can make this step much smoother and today’s article will look at Github actions and discuss what it is and how it can be used in a real WordPress project.
GitHub Actions
By the acquisition of Github from Microsoft, it was clear that tech giant had big plans for it, and innovation in the Github system was only a matter of time; after some time, they introduced their innovation in Continuous Integration and Continuous Development Pipelines game, it was a Github Actions.
GitHub Actions makes it “easy to automate all your software workflows”, basically its a one configuration file YAML that stores all the predefined workflows, jobs and steps, that fire as a response to predefined events, an example of these events can be a pull request being merged, push to a particular branch, release being tagged or any other events.
For the best presentation of what GitHub action is and how it works can be found here, take a look at this video:
Now, you may be wondering how this can be used for WordPress development, well, there are a lot of use cases, take a look at the Github actions marketplace dedicated to WordPress actions:
As you can see there are many useful actions for WordPress developers, starting from an automatic plugin or theme deployment from your GitHub page to WordPress.org and ending to .Pot file generation it can really save tons of time.
In the next example, we will cover how to set up such an action to catch any missing text domains for the translated strings in our theme or plugin
Creating Action
Setting up GitHub
As we said before there are many use cases for Github actions in WordPress but In this example, we will create an action for a WordPress project that will automatically find missing text-domain from our theme files and will fill it directly in the files for us, this feature is very useful when you miss for some reason forgot adding text-domain for your translation strings into the files, it will fully automate this process for you.
The action we are using as a template from the GitHub marketplace is this: https://github.com/marketplace/actions/text-domain-updater-for-wordpress
The easiest way and position to start using Github action are to start it from directly your repository, this page offers ready-made templates for different kinds of actions you can use straightforward way, however, if you are resting something unique you need to click on the Actions tab and use Setup Workflow Yourself button.
You will be redirected to the Action configuration page where you need to setup .yaml file, this file contains all the configuration steps of your actions such as a Name, branch name where you want to run this action, what actions and works need to be done and in which order, it also contains Secret code of the Action that is necessary to run this particular action.
What configuration it does is adding a new folder Workflow into your project and .yaml file with all the configuration rules inside.
Now let’s move to the next step and add some workflow into our file
Adding some Workflow
The code of our workflow will look like this
name: On Push on: push: branches: - main jobs: wp_textdomain_updater: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: WordPress Textdomain uses: varunsridharan/action-wp-textdomain@2.0 with: domain: '{your-text-domain}' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Let’s break the pieces of this workflow file step by step.
name: On Push on: push: branches: - main
First-line in this command contains the name of the workflow, anything is normal if you have different actions for different branches mentioning them in the name will simplify after management. The second line defines when this action needs to be a trigger, in this case on every change in the main branch
jobs: wp_textdomain_updater: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: WordPress Textdomain uses: varunsridharan/action-wp-textdomain@2.0 with: domain: '{your-text-domain}' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
In this section of the command, we add information about the environment we want it to run, by default we are using Ubuntu-Latest, this environment works well for most workflows and can be changed in very specific situations.
actions/checkout@v2 in the code is a pre-built Action and means we’re going to use it as a default action.
The second Uses from the code represents a source of this action, since we are using prebuilt GitHub action it’s pointed to the marketplace item, that’s where is the action is coming from, you can check the action link here: https://github.com/marketplace/actions/text-domain-updater-for-wordpress.
Next in the code comes the default name of your text domain, you need to fill in the name of your text-domain here, once this action catches a missed function from your code without a text-domain it will be used to add it in the function.
Env – is an access token that needs to be created for the action, we will talk about this in the next chapter:
Since we are using prebuilt action basically we are just copying workflow file content, we just need to modify it to our project and once you fill in all the information it will look something like this:
Please consider because this is YAML spacing and any character matters. The last step in this chapter is to commit your changes, simply click on the green button on the right-left and your configuration file will be created in the Workflow folder.
Secrets and credentials
in order to create a secret click on the Settings tap on the same page and from the left side navigation choose secrets and create a new Secret and make sure you have the same name as in the .yaml file, in this case, it’s: GITHUB_TOKEN
In the Value field, you need to provide Personal Access Token which can be generated from your Github profile page > Developer Settings, more details about this step can be found
Once you Personal Access Token save it and you’re ready
Testing our action
If you did all the steps correctly and you follow all the instructions then you are ready to make your first test, all you need to do is to push new changes to your main branch, or for testing purposes just add the new file and from the translation function remove the text domain, once you push changes your action will fire up and after several seconds you will see all the changes made into the file.
A good thing to consider is to make such actions first on the branch, once you make sure the action did it work you may always merge it to the main branch after
Conclusion
I hope you enjoyed this little DevOps adventure, GitHub action saves your time and it really worths investing, there are a lot of ways to use it in practical development, just review your development steps and think yourself how and where GitHub action can be used.
Happy coding and let me know if you have any questions
Leave A Comment