How to create your own custom action for using in GitHub Actions Workflows

Raphael Moraes
Webera
Published in
4 min readAug 19, 2022

--

Hey guys, good to have you back here reading one more article of mine! =)
Today I will talk shortly about an existing option working with GitHub Actions Workflow for creating your action from scratch. Yes, that is possible!

We know that sometimes we need to do some custom things, and in this case, we can encounter challenges that can frustrate us due to existing limitations. This article is precisely about this scenario, where I will use as an example a task that I worked on a long time ago to deploy new app releases on ECS clusters.

At that time, my first problem in that challenge was how to generate a custom task-definition.json file in a way that would be possible to reuse that same JSON file for multiple environments. My second problem was that I should avoid adding sensitive data hardcoded in the file besides needing to change the values used in that JSON specification file according to the environment I was going to deploy.

And before you ask yourself why don’t use the existing Amazon ECS “Render Task Definition” Action for GitHub Actions, knows that even if that action is excellent, it would not fit all my needs. So, I decided to wrote my own action from scratch.

Before to start following the step-by-step, let me just introduce what are actions for those who are not familiar with GitHub Actions. Actions are individual tasks that you can combine to create jobs and customize your workflow. You can create your own actions, or use and customize actions shared by the GitHub community. You can write your own actions to use in your workflow or share the actions you build with the GitHub community. To share actions you’ve built with everyone, your repository must be public. I will not cover in this article about publishing an action, but you can read more about it here. Actions can run directly on a machine or in a Docker container. You can define an action’s inputs, outputs, and environment variables. There are the following Types of Actions:

Now that you learned a little bit more about the actions and which types exist, knows that I’m going to use the Composite Action Type in this article. It allow us to combine multiple workflow steps within one action. For example, you can use this feature to bundle together multiple run commands into an action, and then have a workflow that executes the bundled commands as a single step using that action. Let’s jump now to the step-by-step.

Step-by-Step

Step 1. As recommended in the official documentation, we need to choose a location for the action. In my case, I created the following folder structure in the root directory of my project:

.github/
├── actions
│ └── generate-task-definition-json
│ └── action.yml
└── workflows
├── ecs-workflow.yml
└── README.md

And inside the subfolder .github/actions/generate-task-definition-json I created the metadata file named action.yml (The metadata filename must be either action.yml or action.yaml) with the following content:

NOTE: Action metadata files use YAML syntax. If you’re new to YAML, you can read “Learn YAML in five minutes.”

You can see above that we have some sections in the file action.yml:

Step 2. Now, we can proceed to create the workflow, where we will call that action. I created a file named ecs-workflow.yml inside the subfolder .github/workflows/ with the following content:

You can see that I used secret values for the fields app_datasource_password, app_datasource_url, and app_datasource_username. In this way, I don’t need anymore to hardcode sensitive data in the JSON file, because now it is being created dynamically and getting the sensitive data from GitHub Secrets (which keeps your environment variables encrypted in your Organization or repository)

Knows best practices and recommendations to ensure compatibility with GitHub Enterprise Server and release management is essential. So, if you are planning to start writing new actions for publishing to the GitHub MarketPlace, please, read the documentation shared in the references of this article.

That’s it. =)

Thank you for reading the article!

References:
https://docs.github.com/en/actions/creating-actions
https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
https://www.codeproject.com/Articles/1214409/Learn-YAML-in-five-minutes
https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
https://docs.github.com/en/actions/creating-actions/publishing-actions-in-github-marketplace

--

--