Section 2 - The test reusable workflow
Last updated
Last updated
The test reusable workflow will function like the test workflow we wrote in Chapter 2, Section 3, with the difference that this workflow will be reusable and can be called from another repository.
We will reference the logic we used in Chapter2, Section 3 - The test workflow and refactor it to make the workflow reusable.
We'll start by creating a reusable-test.yaml file under the .github/workflows/ directory in the repo.
Next, we'll copy the test workflow job from one of our service repositories. In this example, we take the from the user-management-service
's test-build-and-deploy
workflow and refactor it to make it reusable.
To refactor the test job into a reusable workflow, we make the following changes:
Update the trigger to workflow_call
.
Define the inputs that the calling workflow can pass to the reusable workflow.
The goal is to make the reusable workflow as flexible as possible, though there is a trade-off. Since this reusable workflow may only be used for a specific type of service, we can limit its flexibility accordingly. For instance, we're assuming all our services are written in Go, so by default, we include Go installation steps in the test workflow job.
We'll add the following inputs and provided default values, allowing the caller to override these defaults only if needed:
docker-compose-command: the Docker Compose command to run
defaults to: docker compose up -d
docker-compose-command-opts: the Docker Compose command options
defaults to: -inMemory
test-script: the test script to run
defaults to:
aws-region: the AWS region:
defaults to: us-west-2
dynamodb-local-endpoint: the DynamoDB local endpoint:
defaults to:
test-aws-access-key-id: the AWS access key ID
defaults to: test
test-aws-secret-access-key: the AWS secret access key
defaults to: test
We'll utilize all the inputs defined in the workflow. For example, instead of hardcoding commands like docker compose up -d
directly in the workflow, we use the input value ${{ inputs.docker-compose-command }}
, which allows the calling workflow to override the default command if needed.
Here is the refactored test job in the reusable workflow:
Finally, we commit and push the changes to the reusable workflows repository. In our case, we’ll push the updates to the repository.