Skip to content

Commit

Permalink
Merge pull request #7 from DNXLabs/add-script-clone-repo
Browse files Browse the repository at this point in the history
馃殌 Add script clone-repo
  • Loading branch information
wvxavier committed May 19, 2022
2 parents 7c14baa + 0e2889c commit 6036638
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
38 changes: 38 additions & 0 deletions scripts/clone-repositories-list/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# clone-repositories

This script clones a list of GIT repositories.

## Use case:

Used extensively to clone private repositories locally using local or CICD pipeline credentials, eliminating the need to export or store GIT credentials.

_Examples:_

- Terraform modules stored in private repositories
- Applications that uses private repositories as dependencies.

## Prerequisites

- Bash
- GIT


## How to run

- Export the required environment variables:
- `ORG_NAME` = Organization name, this variable is used to compose the repository URL e.g.: `export ORG_NAME=DNXLabs`
- `VCS_URL` = Version control system, this variable is used to compose the repository URL e.g.: `export VCS_URL=github.com`
- From the command line run: `bash clone-repositories.sh "repo-1@1.0.1" repo-2@1.2.1" "repo-3@2.0.3"`

## Versioning

1.0.0


## Author

* **Woltter Xavier** - *Initial work* - [wvxavier](https://github.com/wvxavier)

## License

Apache 2 Licensed. See [LICENSE](https://github.com/DNXLabs/tools-box/blob/master/LICENSE) for full details.
32 changes: 32 additions & 0 deletions scripts/clone-repositories-list/clone-repositories.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#/bin/bash

#Checking if necessary environment variables are set
if [[ -z "${ORG_NAME}" ]] ; then
echo "ORG_NAME environment variable is not set, this variable is used to compose the repository URL"
exit
fi

if [[ -z "${VCS_URL}" ]] ; then
echo "VCS_URL environment variable is not set, this variable is used to compose the repository URL"
exit
fi

#List of private repositories and tags from arguments
declare -a repos=("$@")

# Disabe GIT advice
git config --global advice.detachedHead false

for PACKAGE in "${repos[@]}"; do
repoName="$(cut -d'@' -f1 <<< $PACKAGE)"
repoVersion="$(cut -d'@' -f2 <<< $PACKAGE)"
if [ -d "./local-repositories/$repoName" ]
then
echo "Repository $repoName already exists locally."
else
echo "Cloning git@$VCS_URL:$ORG_NAME/$repoName.git..."
git clone -b $repoVersion git@$VCS_URL:$ORG_NAME/$repoName.git ./local-repositories/$repoName
fi
done


0 comments on commit 6036638

Please sign in to comment.