Skip to content

Commit

Permalink
Feat/rm need temlateversionrc (#264)
Browse files Browse the repository at this point in the history
* refact(): move stout and stderr into separate file 🥷

* refact(): move stout and stderr into separate file 🥷

* refact(): #257 no need of .templateversionrc file anymore 🚀

* docs(): #257 update documentation 📝

* fix(): #257 invert logic 🐛
  • Loading branch information
AndreasAugustin committed Dec 27, 2022
1 parent 8aa35f1 commit efdeda5
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 81 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ indent_size = 2
[Makefile]
indent_style = tab
indent_size = 4

[*.sh]
indent_size = 2
indent_style = tab
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: markdownlint
run: make markdownlint
- name: prune
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
name: Shellcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Run shellcheck in container
run: make shellcheck
3 changes: 0 additions & 3 deletions .markdownlint.json

This file was deleted.

1 change: 1 addition & 0 deletions .markdownlintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 0 additions & 1 deletion .templateversionrc

This file was deleted.

5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ help: ## help target to show available commands with information

.PHONY: markdownlint
markdownlint: ## Validate markdown files
docker-compose run docs markdownlint .github/ --ignore node_modules
docker-compose run docs markdownlint . --ignore node_modules
docker-compose run docs markdownlint .

.PHONY: zsh
zsh: ## open dev container with build environment
Expand All @@ -32,4 +31,4 @@ prune: ## delete the whole environment

.Phony: shellcheck
shellcheck: ## run shellcheck
docker-compose run shellcheck -x src/entrypoint.sh
docker-compose run shellcheck -x src/*.sh
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ This GitHub action will help you to keep track of the template changes.

## Usage

### Update

starting with version v0.5.2-draft the `templateversionrc` file is not needed anymore. You can delete that file from the target repositories.

### GitHub Actions

Add this configuration to your github action
Expand Down Expand Up @@ -59,7 +63,8 @@ You will receive a pull request within your repository if there are some changes
| github_token | Token for the repo. Can be passed in using `$\{{ secrets.GITHUB_TOKEN }}` | `true` | |
| source_repo_path | Repository path of the template | `true` | |
| upstream_branch | The target branch | `true` | `main` |
| source_repo_ssh_private_key | `[optional]` private ssh key for the source repository. E.q. useful if using a private template repository. [see](#private-template-repository)| `false` | |
| source_repo_ssh_private_key | `[optional]` private ssh key for the source repository. E.q. useful if using a private template repository.
[see](#private-template-repository)| `false` | |
| pr_branch_name_prefix | `[optional]` the prefix of branches created by this action | `false` | `chore/template_sync` |
| pr_title | `[optional]` the title of PRs opened by this action. Must be already created. | `false` | `upstream merge template repository` |
| pr_labels | `[optional]` comma separated list. [pull request labels][pr-labels]. Must be already created. | `false` | |
Expand All @@ -85,8 +90,9 @@ If you have a private template repository.
#### SSH

You have various options to use ssh keys with GitHub.
An example are [deployment keys][deployment-keys]. For our use case write permissions are not needed.
Within the repository where the GitHub action is enabled add a secret (e.q. `SOURCE_REPO_SSH_PRIVATE_KEY`) with the content of your private SSH key. Make sure that the read permissions of that secret fulfil your use case.
An example are [deployment keys][deployment-keys]. For our use case write permissions are not needed.i
Within the repository where the GitHub action is enabled add a secret (e.q. `SOURCE_REPO_SSH_PRIVATE_KEY`) with the content of your private SSH key.
Make sure that the read permissions of that secret fulfil your use case.
Set the optional `source_repo_ssh_private_key` input parameter.

```yaml
Expand All @@ -110,15 +116,17 @@ jobs:

## Ignore Files

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.
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

You must create a secret named `ACTIONS_STEP_DEBUG` with the value `true` to see the debug messages set by this command in the log. For more information, see "[Enabling debug logging.][enabling-debug-logging]"
You must create a secret named `ACTIONS_STEP_DEBUG` with the value `true` to see the debug messages set by this command in the log.
For more information, see "[Enabling debug logging.][enabling-debug-logging]"

## DEV

Expand Down
15 changes: 5 additions & 10 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ SshConfigureEntry[Configure ssh related variables]
GitConfigureEntry[Configure git global settings]
EnvCheckSync{Required environment variables exists}
SshConfigureSync[Configure SSH variables]
SshConfigureSync[Eventually configure SSH variables]
SetVariablesSync[Set the needed variables, e.q. with reading remote 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"}
CheckCommitLocalExistent{"Check if source commit hash is present in target repo"}
GitCheckoutSync["Create git branch <branch_prefix_git_hash>"]
GitPullSync["Pull from remote repository"]
CheckIgnoreFileExistsSync{"Check if .templatesyncignore file exists\n(First inside .github folder, then in root)"}
Expand Down Expand Up @@ -67,15 +65,12 @@ EnvCheckSync -->|do exist| SshConfigureSync
SshConfigureSync --> SetVariablesSync
subgraph compareVersion["Compare the sync version"]
SetVariablesSync --> CheckTemplateFileExists
CheckTemplateFileExists -->|exists| WriteTemplateVersionSync
CheckTemplateFileExists -->|does not exist| CompareTemplateVersionSync
WriteTemplateVersionSync --> CompareTemplateVersionSync
CompareTemplateVersionSync -->|equal versions| Exit
SetVariablesSync --> CheckCommitLocalExistent
CheckCommitLocalExistent -->|commit hash already in target history| Exit
end
subgraph git["Git Actions"]
CompareTemplateVersionSync -->|versions not equal| GitCheckoutSync
CheckCommitLocalExistent -->|commit hash not in target history| GitCheckoutSync
GitCheckoutSync --> GitPullSync
GitPullSync --> CheckIgnoreFileExistsSync
CheckIgnoreFileExistsSync -->|does not exist| GitCommitSync
Expand Down
4 changes: 0 additions & 4 deletions docs/_config.yml

This file was deleted.

3 changes: 2 additions & 1 deletion src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ RUN tar --strip-components=1 -xf ghcli.tar.gz

ADD *.sh /bin/
RUN chmod +x /bin/entrypoint.sh \
&& chmod +x /bin/sync_template.sh
&& chmod +x /bin/sync_template.sh \
&& chmod +x /bin/sync_common.sh

RUN mkdir -p /root/.ssh \
&& ssh-keyscan -t rsa github.com >> /root/.ssh/known_hosts
Expand Down
42 changes: 30 additions & 12 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ set -e
# set -u
set -x

# shellcheck source=src/sync_common.sh
source sync_common.sh

[ -z "${GITHUB_TOKEN}" ] && {
echo "::error::Missing input 'github_token: \${{ secrets.GITHUB_TOKEN }}'.";
err "Missing input 'github_token: \${{ secrets.GITHUB_TOKEN }}'.";
exit 1;
};

if [[ -z "${SOURCE_REPO_PATH}" ]]; then
echo "::error::Missing input 'source_repo_path: \${{ input.source_repo_path }}'.;"
err "Missing input 'source_repo_path: \${{ input.source_repo_path }}'.";
exit 1
fi

Expand All @@ -18,28 +21,43 @@ SOURCE_REPO_HOSTNAME="${HOSTNAME:-github.com}"
# In case of private template repository this will be overwritten
SOURCE_REPO_PREFIX="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${SOURCE_REPO_HOSTNAME}/"

# Forward to /dev/null to swallow the output of the private key
if [[ -n "${SSH_PRIVATE_KEY_SRC}" ]] &>/dev/null; then
function ssh_setup() {
echo "::group::ssh setup"

info "prepare ssh"
SRC_SSH_FILE_DIR="/tmp/.ssh"
SRC_SSH_PRIVATEKEY_FILE_NAME="id_rsa_actions_template_sync"
export SRC_SSH_PRIVATEKEY_ABS_PATH="${SRC_SSH_FILE_DIR}/${SRC_SSH_PRIVATEKEY_FILE_NAME}"
echo "::debug::We are using SSH within a private source repo"
debug "We are using SSH within a private source repo"
mkdir -p "${SRC_SSH_FILE_DIR}"
# use cat <<< instead of echo to swallow output of the private key
cat <<< "${SSH_PRIVATE_KEY_SRC}" | sed 's/\\n/\n/g' > "${SRC_SSH_PRIVATEKEY_ABS_PATH}"
chmod 600 "${SRC_SSH_PRIVATEKEY_ABS_PATH}"
SOURCE_REPO_PREFIX="git@${SOURCE_REPO_HOSTNAME}:"

echo "::endgroup::"
}

# Forward to /dev/null to swallow the output of the private key
if [[ -n "${SSH_PRIVATE_KEY_SRC}" ]] &>/dev/null; then
ssh_setup
fi

export SOURCE_REPO="${SOURCE_REPO_PREFIX}${SOURCE_REPO_PATH}"

echo "::group::git init"
echo "set git global configuration"
git config --global user.email "github-action@actions-template-sync.noreply.${SOURCE_REPO_HOSTNAME}"
git config --global user.name "${GITHUB_ACTOR}"
git config --global pull.rebase false
git config --global --add safe.directory /github/workspace
echo "::endgroup::"
function git_init() {
echo "::group::git init"
info "set git global configuration"

git config --global user.email "github-action@actions-template-sync.noreply.${SOURCE_REPO_HOSTNAME}"
git config --global user.name "${GITHUB_ACTOR}"
git config --global pull.rebase false
git config --global --add safe.directory /github/workspace

echo "::endgroup::"
}

git_init

# shellcheck source=src/sync_template.sh
source sync_template.sh
41 changes: 41 additions & 0 deletions src/sync_common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#! /usr/bin/env bash

set -e
# set -u
set -x

#######################################
# write a message to STDERR.
# Arguments:
# message to print.
#######################################
err() {
echo "::error::[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2;
}

#######################################
# write a debug message.
# Arguments:
# message to print.
#######################################
debug() {
echo "::debug::$*";
}

#######################################
# write a warn message.
# Arguments:
# message to print.
#######################################
warn() {
echo "::warn::$*";
}

#######################################
# write a info message.
# Arguments:
# message to print.
#######################################
info() {
echo "::info::$*";
}
Loading

0 comments on commit efdeda5

Please sign in to comment.