Created
April 23, 2019 13:29
-
-
Save medwig/de8f0fbd071e88feaa38e75e8cf87a5b to your computer and use it in GitHub Desktop.
Repair symlinks in AWS codepipeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 0.2 | |
phases: | |
pre_build: | |
commands: | |
# Hack to fix the symlinks that Codepipeline breaks | |
# Applies to .tf files that contain only 1 line | |
- for file in *.tf ; do awk 'NR==2{exit}END{exit NR!=1}' "$file" && sh -c "cat $file | xargs -I R2 ln -sf R2 $file"; done | |
years later, this is still a super helpful gist!
In my case I had many yml files in folders with various levels of nesting that all needed to be symlinked. I ran into issues using glob patterns so i switched it out for find
:
pre_build:
commands:
# Hack to fix the symlinks that Codepipeline breaks when it clones the repo
# Applies to .yml files that contain only 1 line (a broken symlink)
- >
symlink_files=$(find ./my-folder -name "*.yml" -type f);
for file in $symlink_files; do
if awk "NR==2{exit}END{exit NR!=1}" "$file"; then
content=$(cat "$file")
ln -sf "$content" "$file"
echo "Created symlink for $file -> $content"
fi
done;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brilliant, I was spending hours to realize that codebuild bricks all symlinks (in our case package.json files referencing a package.json in the project root). Given our repo size we cannot just switch to the proposed solution of cloning the repo as cheap CodeBuild does not support shallow clones on branches. This is what I ended up doing:
In this script:
find . -name 'package.json' -type f
: Finds files namedpackage.json
that are regular files.-exec bash -c '...' \;
: Executes the given bash script for each file found.if [ ! -L "{}" ]; then ... fi
: Checks if the file is not a symlink (-L
checks if the file is a symlink).target=$(head -n 1 "{}")
: Reads the first line of the file to get the presumed target path.[[ $target == /* || $target == .* || $target == */* ]]
: Checks if the target path looks like a valid path.{ rm -f "{}"; ln -sf "$target" "{}"; }
: Removes the regular file and creates a symlink with the same name pointing to the target path.This command will only convert
package.json
files to symlinks if they are not already symlinks and if their content appears to be a valid path. As always, test this script in a controlled environment before using it in production to ensure it works as expected with your specific file setup.