This is the CI/CD workflow for checking if there are API changes. For now will be triggered manually, we will setup a schedule in phase 2.
How it works:
Checkout code
Install OpenAPI Generator via Homebrew
Then we run our code generation script. The script fetches latest JSON input files, places them to the input folder (Config/input) and runs the generator. The generator then will re-generate all the APIs and models based on the input files and place the result to the output folder (Generated)
Check for changes in these folders.
Here we use git status -s, which provides lines containing a prefix (A, M or D) and a file name.
We also remove double quotes (sed 's/"//g') because a deleted file has them in its line, in contrast with M and A. This can cause issues later.
We set variables changes_in_generated and changes_in_input
We are checking if these variables are not empty by using [ -n "$changes_in_generated" ]. We also set changes_detected variable here.
Then we send these variables to the output of this step by using >> $GITHUB_OUTPUT. Multiline string notation is used for CHANGES_IN_GENERATED and CHANGES_IN_INPUT output variables
If changes not found - we say good bye (if: ${{ steps.check_for_changes.outputs.CHANGES_DETECTED == 'false' }})
All next steps have this condition if: ${{ steps.check_for_changes.outputs.CHANGES_DETECTED == 'true' }}, therefore they will be executed only if there are changes.
Prepare git and define a branch name
Configure git with our bot info
Create a branch name including current date, to avoid collisions. Set BRANCH_NAME variable in the output of this step.
Create new branch and commit
Push
Create pull request:
Use $GITHUB_TOKEN provided by GitHub (because our Actions are allowed to create pull requests)
Create pr_body, include changes from input and output folders there
Prepare JSON data for the pull request
Execute curl to create the pull request. ${{ github.repository }} - is a current repository 🙂
In this commit I prepared the workflow for production. It is not triggered on push anymore and uses main branch (to compare and to create pull request).
You can see the result of its work against my test branch for example, in this PR.
If you want to test it yourself, set HEAD to a previous commit, push something and then observe execution of this workflow in Actions.
To check a manual execution and a real work against main, we need to merge this PR at first (and then wait for API to be changed 😅) Let's just hope it will work! 🙂
This is the CI/CD workflow for checking if there are API changes. For now will be triggered manually, we will setup a schedule in phase 2.
How it works:
Config/input
) and runs the generator. The generator then will re-generate all the APIs and models based on the input files and place the result to the output folder (Generated
)git status -s
, which provides lines containing a prefix (A
,M
orD
) and a file name.sed 's/"//g'
) because a deleted file has them in its line, in contrast withM
andA
. This can cause issues later.changes_in_generated
andchanges_in_input
[ -n "$changes_in_generated" ]
. We also setchanges_detected
variable here.>> $GITHUB_OUTPUT
. Multiline string notation is used forCHANGES_IN_GENERATED
andCHANGES_IN_INPUT
output variablesif: ${{ steps.check_for_changes.outputs.CHANGES_DETECTED == 'false' }}
)if: ${{ steps.check_for_changes.outputs.CHANGES_DETECTED == 'true' }}
, therefore they will be executed only if there are changes.BRANCH_NAME
variable in the output of this step.$GITHUB_TOKEN
provided by GitHub (because our Actions are allowed to create pull requests)pr_body
, include changes from input and output folders therecurl
to create the pull request.${{ github.repository }}
- is a current repository 🙂In this commit I prepared the workflow for production. It is not triggered on
push
anymore and usesmain
branch (to compare and to create pull request).You can see the result of its work against my test branch for example, in this PR.
If you want to test it yourself, set HEAD to a previous commit, push something and then observe execution of this workflow in Actions.
To check a manual execution and a real work against
main
, we need to merge this PR at first (and then wait for API to be changed 😅) Let's just hope it will work! 🙂