Skip to content

Commit 60200f7

Browse files
Merge pull request #5695 from aleksandrychev/ENT-8630
Added support of deployment from subdirectory to the masterfiles stage script
2 parents 84974ae + ae4ab01 commit 60200f7

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

contrib/masterfiles-stage/README.org

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ when developing new staging methods.
188188
- =my_tag=
189189
- =88335d36b48c8808b12b48667a463182dc8d0338=
190190
- =cb375d0f=
191+
192+
- =PROJECT_SUBDIRECTORY= :: The subdirectory inside GIT repository where the project is located (optional).
193+
- Example values:
194+
- =path/to/policy_set=
195+
- =path/to/build_project=
191196

192197
- =PKEY= :: Fully qualified path to passphraseless private SSH Key used for authorization.
193198
This file should be owned by root and have only user read/write permissions.

contrib/masterfiles-stage/common.sh

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,56 @@ git_setup_local_mirrored_repo() {
6969
error_exit "Failed: git clone --mirror '${GIT_URL}' '${local_mirrored_repo}'"
7070
}
7171

72+
git_checkout_into_temp_dir() {
73+
# Depends on $local_mirrored_repo
74+
# Accepts two args:
75+
# $1 - dir to deploy to
76+
# $2 - refspec to deploy - a git tagname, branch, or commit hash.
77+
# $3 - project subdirectory inside the repository to deploy.
78+
79+
# The '^0' at the end of the refspec
80+
# populates HEAD with the SHA of the commit
81+
# rather than potentially using a git branch name.
82+
# Also see http://stackoverflow.com/a/13293010/5419599
83+
# and https://github.com/cfengine/core/pull/2465#issuecomment-173656475
84+
85+
# Checkout with optional subdirectory if not empty
86+
if [ -n "$3" ]; then
87+
# checkout subdirectory only
88+
git --git-dir="${local_mirrored_repo}" --work-tree="${temp_stage}" checkout -q -f "${2}^0" -- "$3" ||
89+
error_exit "Failed to checkout subdirectory '$3' from '$2'"
90+
# move contents of subdirectory to the temp_stage root
91+
# include dotfile matching in wildcard expansions to move them as well
92+
shopt -s dotglob
93+
mv "${temp_stage}/${3}"/* "${temp_stage}/"
94+
# disable dotfile matching
95+
shopt -u dotglob
96+
# remove empty subdirectory from the temp_stage
97+
rmdir "${temp_stage}/${3}"
98+
else
99+
# checkout the whole repository
100+
git --git-dir="${local_mirrored_repo}" --work-tree="${temp_stage}" checkout -q -f "${2}^0" ||
101+
error_exit "Failed to checkout '$2' from '${local_mirrored_repo}'"
102+
fi
103+
}
104+
72105
git_deploy_refspec() {
73106
# Contributed by Mike Weilgart
74107

75108
# Depends on $local_mirrored_repo
76109
# Accepts two args:
77110
# $1 - dir to deploy to
78111
# $2 - refspec to deploy - a git tagname, branch, or commit hash.
112+
# $3 - project subdirectory inside the repository to deploy.
79113

80114
# This function
81115
# 1. creates an empty temp dir,
82116
# 2. checks out the refspec into the empty temp dir,
83117
# (including populating .git/HEAD in the temp dir),
84-
# 3. sets appropriate permissions on the policy set,
85-
# 4. validates the policy set using cf-promises,
86-
# 5. moves the temp dir policy set into the given deploy dir,
118+
# 3. if the project subdirectory is set, it checks it out and moves files from it to the root of the temp dir.
119+
# 4. sets appropriate permissions on the policy set,
120+
# 5. validates the policy set using cf-promises,
121+
# 6. moves the temp dir policy set into the given deploy dir,
87122
# avoiding triggering policy updates unnecessarily
88123
# by comparing the cf_promises_validated flag file.
89124
# (See long comment at end of function def.)
@@ -105,13 +140,8 @@ git_deploy_refspec() {
105140
trap 'rm -rf "$temp_stage"' EXIT
106141

107142
########################## 2. CHECKOUT INTO TEMP DIR
108-
# The '^0' at the end of the refspec
109-
# populates HEAD with the SHA of the commit
110-
# rather than potentially using a git branch name.
111-
# Also see http://stackoverflow.com/a/13293010/5419599
112-
# and https://github.com/cfengine/core/pull/2465#issuecomment-173656475
113-
git --git-dir="${local_mirrored_repo}" --work-tree="${temp_stage}" checkout -q -f "${2}^0" ||
114-
error_exit "Failed to checkout '$2' from '${local_mirrored_repo}'"
143+
git_checkout_into_temp_dir "$1" "$2" "$3"
144+
115145
# Grab HEAD so it can be used to populate cf_promises_release_id
116146
mkdir -p "${temp_stage}/.git"
117147
cp "${local_mirrored_repo}/HEAD" "${temp_stage}/.git/"
@@ -180,15 +210,17 @@ git_cfbs_deploy_refspec() {
180210
# Accepts two args:
181211
# $1 - dir to deploy to
182212
# $2 - refspec to deploy - a git tagname, branch, or commit hash.
213+
# $3 - project subdirectory inside the repository to deploy.
183214

184215
# This function
185216
# 1. creates an empty temp dir,
186217
# 2. checks out the refspec into the empty temp dir,
187218
# (including populating .git/HEAD in the temp dir),
188-
# 3. builds policy with cfbs
189-
# 4. sets appropriate permissions on the policy set,
190-
# 5. validates the policy set using cf-promises,
191-
# 6. moves the temp dir policy set into the given deploy dir,
219+
# 3. if the project subdirectory is set, it checks it out and moves files from it to the root of the temp dir.
220+
# 4. builds policy with cfbs
221+
# 5. sets appropriate permissions on the policy set,
222+
# 6. validates the policy set using cf-promises,
223+
# 7. moves the temp dir policy set into the given deploy dir,
192224
# avoiding triggering policy updates unnecessarily
193225
# by comparing the cf_promises_validated flag file.
194226
# (See long comment at end of function def.)
@@ -222,13 +254,7 @@ git_cfbs_deploy_refspec() {
222254
trap 'rm -rf "$temp_stage"' EXIT
223255

224256
########################## 2. CHECKOUT INTO TEMP DIR
225-
# The '^0' at the end of the refspec
226-
# populates HEAD with the SHA of the commit
227-
# rather than potentially using a git branch name.
228-
# Also see http://stackoverflow.com/a/13293010/5419599
229-
# and https://github.com/cfengine/core/pull/2465#issuecomment-173656475
230-
git --git-dir="${local_mirrored_repo}" --work-tree="${temp_stage}" checkout -q -f "${2}^0" ||
231-
error_exit "Failed to checkout '$2' from '${local_mirrored_repo}'"
257+
git_checkout_into_temp_dir "$1" "$2" "$3"
232258

233259
########################## 3. cfbs build
234260
# Remember what directory we were in when we started.
@@ -382,7 +408,7 @@ git_stage_policy_channels() {
382408

383409
git_masterstage() {
384410
# $1 -- whether to check only [true/false; optional]
385-
# Depends on $GIT_URL, $ROOT, $MASTERDIR, $GIT_REFSPEC
411+
# Depends on $GIT_URL, $ROOT, $MASTERDIR, $GIT_REFSPEC, $PROJECT_SUBDIRECTORY
386412
check_git_installed
387413
git_setup_local_mirrored_repo "$( dirname "$ROOT" )"
388414
if [ "x$1" = "xtrue" ]; then
@@ -392,8 +418,8 @@ git_masterstage() {
392418
return 0 # not in sync => update available
393419
fi
394420
fi
395-
git_deploy_refspec "$MASTERDIR" "${GIT_REFSPEC}"
396-
echo "Successfully deployed '${GIT_REFSPEC}' from '${GIT_URL}' to '${MASTERDIR}' on $(date)"
421+
git_deploy_refspec "$MASTERDIR" "${GIT_REFSPEC}" "${PROJECT_SUBDIRECTORY}"
422+
echo "Successfully deployed '${GIT_REFSPEC}' from '${GIT_URL}'$( [[ -n "${PROJECT_SUBDIRECTORY}" ]] && echo " subdirectory: '${PROJECT_SUBDIRECTORY}'") to '${MASTERDIR}' on $(date)"
397423
}
398424

399425
git_cfbs_masterstage() {
@@ -409,8 +435,8 @@ git_cfbs_masterstage() {
409435
return 0 # not in sync => update available
410436
fi
411437
fi
412-
git_cfbs_deploy_refspec "$MASTERDIR" "${GIT_REFSPEC}"
413-
echo "Successfully built and deployed '${GIT_REFSPEC}' from '${GIT_URL}' to '${MASTERDIR}' on $(date) with cfbs"
438+
git_cfbs_deploy_refspec "$MASTERDIR" "${GIT_REFSPEC}" "${PROJECT_SUBDIRECTORY}"
439+
echo "Successfully built and deployed '${GIT_REFSPEC}' from '${GIT_URL}'$( [[ -n "${PROJECT_SUBDIRECTORY}" ]] && echo " subdirectory: '${PROJECT_SUBDIRECTORY}'") to '${MASTERDIR}' on $(date) with cfbs"
414440
}
415441

416442
svn_branch() {

contrib/masterfiles-stage/example_params/PARAMS_example_git_branch.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
ROOT="/opt/cfengine/masterfiles_staging"
33
GIT_URL="https://gitlab.com/nickanderson/masterfiles-vcs"
44
GIT_REFSPEC="3.15.x"
5+
PROJECT_SUBDIRECTORY=""
56
GIT_USERNAME=""
67
GIT_PASSWORD=""
78
GIT_WORKING_BRANCH="CF_WORKING_BRANCH"

0 commit comments

Comments
 (0)