diff --git a/.all-contributorsrc b/.all-contributorsrc index edd5028..6cf9c51 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -136,7 +136,7 @@ "login": "LuisHenri", "name": "Luís Henrique A. Schünemann", "avatar_url": "https://avatars.githubusercontent.com/u/44511825?v=4", - "profile": "https://www.instagram.com/LuisHenri_", + "profile": "https://github.com/LuisHenri", "contributions": [ "ideas" ] diff --git a/README.md b/README.md index 9688dd9..667a96e 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,8 @@ jobs: Create a `.templatesyncignore` file. Just like writing a `.gitignore` file, follow the [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) in defining the files and folders that should be excluded from syncing with the template repository. +It can also be stored inside `.github` folder. + _Note: It is not possible to sync also the `.templatesyncignore` itself. Any changes from the template repository will be restored automatically._ ## Debug diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 7db2f18..f25c1f2 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -6,7 +6,7 @@ within this section you will find some ## Code -The architecture and logic within the code +The architecture and logic within the code: ```mermaid flowchart TD @@ -17,26 +17,26 @@ GitHubActionEnv{Read GitHubAction env} style Start fill:#f9f,stroke:#333,stroke-width:4px style Exit fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5 -EnvCheckEntry{required environment variables exists} +EnvCheckEntry{Required environment variables exists} SshCheckEntry{SSH private key defined} SshConfigureEntry[Configure ssh related variables] GitConfigureEntry[Configure git global settings] -EnvCheckSync{required environment variables exists} +EnvCheckSync{Required environment variables exists} SshConfigureSync[Configure SSH variables] SetVariablesSync[Set the needed variables, e.q. with reading remote repository] -CheckTemplateFileExists{"Check if the .templatesyncrc file exists in repository"} +CheckTemplateFileExists{"Check if the .templatesyncrc file exists\n(First inside .github folder, then in root)"} WriteTemplateVersionSync["Read and write the template sync version into variable"] CompareTemplateVersionSync{"Compare the source repository version"} -GitCheckoutSync["create git branch "] -GitPullSync["pull from remote repository"] -CheckIgnoreFileExistsSync{"check if .templatesyncignore file exists"} +GitCheckoutSync["Create git branch "] +GitPullSync["Pull from remote repository"] +CheckIgnoreFileExistsSync{"Check if .templatesyncignore file exists\n(First inside .github folder, then in root)"} ResetChangesSync["Reset the changes listed within the ignore file"] -GitCommitSync["commit the changes"] +GitCommitSync["Commit the changes"] -CheckIsDryRun{"check if is_dry_run is set to true"} +CheckIsDryRun{"Check if is_dry_run is set to true"} GitPushSync["Push the changes to GitHub"] -GitPullRequestSync["create a pull request on GitHub"] +GitPullRequestSync["Create a pull request on GitHub"] subgraph githubactions["GitHubActions"] @@ -66,15 +66,15 @@ EnvCheckSync -->|do not exist| Exit EnvCheckSync -->|do exist| SshConfigureSync SshConfigureSync --> SetVariablesSync -subgraph compareVersion["compare the sync version"] +subgraph compareVersion["Compare the sync version"] SetVariablesSync --> CheckTemplateFileExists CheckTemplateFileExists -->|exists| WriteTemplateVersionSync -CheckTemplateFileExists -->|does not exist|CompareTemplateVersionSync +CheckTemplateFileExists -->|does not exist| CompareTemplateVersionSync WriteTemplateVersionSync --> CompareTemplateVersionSync CompareTemplateVersionSync -->|equal versions| Exit end -subgraph git["Git actions"] +subgraph git["Git Actions"] CompareTemplateVersionSync -->|versions not equal| GitCheckoutSync GitCheckoutSync --> GitPullSync GitPullSync --> CheckIgnoreFileExistsSync @@ -83,7 +83,7 @@ CheckIgnoreFileExistsSync -->|exists| ResetChangesSync ResetChangesSync --> GitCommitSync end -subgraph github["gitHub actions"] +subgraph github["GitHub Actions"] GitCommitSync --> CheckIsDryRun CheckIsDryRun -->|is true| Exit CheckIsDryRun -->|is not true| GitPushSync diff --git a/src/sync_template.sh b/src/sync_template.sh index b750d23..915966a 100755 --- a/src/sync_template.sh +++ b/src/sync_template.sh @@ -28,22 +28,26 @@ if [[ -n "${SRC_SSH_PRIVATEKEY_ABS_PATH}" ]]; then export GIT_SSH_COMMAND="ssh -i ${SRC_SSH_PRIVATEKEY_ABS_PATH}" fi -TEMPLATE_VERSION_FILE_NAME=".templateversionrc" -TEMPLATE_SYNC_IGNORE_FILE_NAME=".templatesyncignore" +TEMPLATE_VERSION_FILE_PATH=".templateversionrc" +TEMPLATE_SYNC_IGNORE_FILE_PATH=".templatesyncignore" TEMPLATE_REMOTE_GIT_HASH=$(git ls-remote "${SOURCE_REPO}" HEAD | awk '{print $1}') NEW_TEMPLATE_GIT_HASH=$(git rev-parse --short "${TEMPLATE_REMOTE_GIT_HASH}") NEW_BRANCH="${PR_BRANCH_NAME_PREFIX}_${NEW_TEMPLATE_GIT_HASH}" echo "::group::Check new changes" echo "::debug::new Git HASH ${NEW_TEMPLATE_GIT_HASH}" -if [ -r ${TEMPLATE_VERSION_FILE_NAME} ] -then - CURRENT_TEMPLATE_GIT_HASH=$(cat ${TEMPLATE_VERSION_FILE_NAME}) + +# Check if the Version File exists inside .github folder or if it doesn't exist at all +if [[ -f ".github/${TEMPLATE_VERSION_FILE_PATH}" || ! -f "${TEMPLATE_VERSION_FILE_PATH}" ]]; then + echo "::debug::using version file as in .github folder" + TEMPLATE_VERSION_FILE_PATH=".github/${TEMPLATE_VERSION_FILE_PATH}" +fi +if [ -r ${TEMPLATE_VERSION_FILE_PATH} ]; then + CURRENT_TEMPLATE_GIT_HASH=$(cat ${TEMPLATE_VERSION_FILE_PATH}) echo "::debug::Current git hash ${CURRENT_TEMPLATE_GIT_HASH}" fi -if [ "${NEW_TEMPLATE_GIT_HASH}" == "${CURRENT_TEMPLATE_GIT_HASH}" ] -then +if [ "${NEW_TEMPLATE_GIT_HASH}" == "${CURRENT_TEMPLATE_GIT_HASH}" ]; then echo "::warn::repository is up to date" exit 0 fi @@ -66,19 +70,23 @@ fi echo "::group::persist template version" echo "write new template version file" -echo "${NEW_TEMPLATE_GIT_HASH}" > ${TEMPLATE_VERSION_FILE_NAME} -echo "::debug::wrote new template version file with content $(cat ${TEMPLATE_VERSION_FILE_NAME})" +echo "${NEW_TEMPLATE_GIT_HASH}" > ${TEMPLATE_VERSION_FILE_PATH} +echo "::debug::wrote new template version file with content $(cat ${TEMPLATE_VERSION_FILE_PATH})" echo "::endgroup::" echo "::group::commit and push changes" git add . +# Check if the Ignore File exists inside .github folder or if it doesn't exist at all +if [[ -f ".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}" || ! -f "${TEMPLATE_SYNC_IGNORE_FILE_PATH}" ]]; then + echo "::debug::using ignore file as in .github folder" + TEMPLATE_SYNC_IGNORE_FILE_PATH=".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}" +fi # we are checking the ignore file if it exists or is empty # -s is true if the file contains whitespaces -if [ -s ${TEMPLATE_SYNC_IGNORE_FILE_NAME} ] -then +if [ -s ${TEMPLATE_SYNC_IGNORE_FILE_PATH} ]; then echo "::debug::unstage files from template sync ignore" - git reset --pathspec-from-file="${TEMPLATE_SYNC_IGNORE_FILE_NAME}" + git reset --pathspec-from-file="${TEMPLATE_SYNC_IGNORE_FILE_PATH}" echo "::debug::clean untracked files" git clean -df