[CICD-583] Split FLAGS variable into an array #32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
JIRA Ticket
CICD-583
What Are We Doing Here
1. Converting the FLAGS variable to an array
Rsync flags sometimes include whitespace in the flag's value. For example,
--filter=':- .gitignore'
includes whitespace between the filter rule and the file name. The single quotes surrounding the flag's value are meant to prevent splitting. Unfortunately, when bash expands the$FLAGS
variable as part of the main rsync command it escapes the single quotes ('\''
)and then splits on the whitespace between:-
and.gitignore
. This was causing rsync to see the filter flag as two separate arguments,--filter=:-
and.gitignore
. For example:If the example above hadn't encountered the error with
--filter
, it still would have misinterpreted the--exclude
rule and not excluded dotfiles from the deploy.Simply double-quoting
$FLAGS
string doesn't help because it prevents splitting between flags when multiple are provided. For example:To fix this, we needed to split the
FLAGS
variable into an array while respecting single quotes. This pre-split array can be passed to rsync with double-quotes to prevent further splitting. So we end up with:2. Enabling debug mode for the main rsync command
Bash debug mode was especially useful for determining how the
FLAGS
input was being transformed in all these different cases. I believe it would be just as useful for GitHub Actions and Bitbucket Pipelines users to spot issues with escaping in theirFLAGS
input. For that reason, I've left debug mode on for the main rsync call. Users will now see output similar to:3. Starting a new functions.sh file
Testing these changes has been pretty difficult given that we don't have anything more granular than an end-to-end smoke test configured for this project. Fixing that is beyond the scope of this PR, but I did take the opportunity to set us up for unit testing in the future by putting the new functions I added into a separate file. This allowed me to test the function independently of a full deploy using several different test cases. In the future, we can formalize this a bit more, add a framework like (bats)[https://bats-core.readthedocs.io/en/stable/], and extract other functions from
entrypoint.sh
.Testing
I've added tests for the
parse_flags
function. You can run those using make.Additionally, I've included a demo script that illustrates the need to parse FLAGS into an array using the examples described above. You can run that one as a stand-alone script.