Skip to content

Commit

Permalink
feat(github-actions/k8s-prepare): scaffold bash script
Browse files Browse the repository at this point in the history
  • Loading branch information
themightychris committed Nov 22, 2023
1 parent 8dbab9a commit 10ef6e0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
4 changes: 4 additions & 0 deletions github-actions/k8s-prepare/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GITHUB_TOKEN=PASTE_YOUR_TOKEN
GITHUB_REPOSITORY=exampleorg/exampleproject
BRANCH_DEPLOY=deploys/k8s-manifests
BRANCH_RELEASE=releases/k8s-manifests
31 changes: 31 additions & 0 deletions github-actions/k8s-prepare/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# GitHub Action: k8s-prepare

## Development

This action encapsulates most of its complexity in a standalone Bash script that accepts environment variables, making it easy to test locally with the help of a `.env` file you can point at a test repository.

1. Create `.env` from the template:

```bash
cp .env.example .env
```

2. Paste a GitHub token and tailor repository/branches to your test target.

3. Save this directory's complete path in a shell variable:
```bash
export GITHUB_ACTION_PATH="$(pwd)"
```
4. Change into your test target's local git clone:

```bash
cd ~/Repositories/test-repo
```

5. Run bash script with `.env` applied to emulate GitHub Actions context:

```bash
eval $(< "${GITHUB_ACTION_PATH}/.env") "${GITHUB_ACTION_PATH}/pull-request.sh"
```
64 changes: 64 additions & 0 deletions github-actions/k8s-prepare/pull-request.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash


## build PR description body
echo
echo "Builing PR title+body content..."
diff_size=$(du -k '/tmp/kube.diff' | cut -f1)
pr_head_describe="$(git describe --always --tag)"

pr_title="Deploy ${BRANCH_RELEASE} ${pr_head_describe}"
pr_body="$(cat <<EOF
\`kubectl diff\` reports that applying ${pr_head_describe} will change:
\`\`\`diff
$(if (( diff_size > 50000)); then echo 'diff too big; review locally'; else cat /tmp/kube.diff; fi)
\`\`\`
EOF
)"


## generate initial commit for base if needed
if ! git ls-remote --exit-code --heads origin "${BRANCH_DEPLOY}"; then
echo
echo "Existing branch ${BRANCH_DEPLOY} not found, generating initial commit..."
git fetch origin --unshallow
_first_projected_commit=$(git rev-list --max-parents=0 --first-parent HEAD)
git push origin "${_first_projected_commit}:refs/heads/${BRANCH_DEPLOY}"
fi


## check for existing PR
echo
echo "Looking for existing open PR for branch ${BRANCH_RELEASE}..."
_existing_pr_number=$(
gh pr view "${BRANCH_RELEASE}" \
--json number,state \
--template '{{.number}}{{"\t"}}{{.state}}' \
| grep OPEN \
| awk '{print $1}'
)

if [ -n "${_existing_pr_number}" ]; then
echo
echo "Found existing PR #${_existing_pr_number}, updating description..."
pr_url=$(
gh api "/repos/${GITHUB_REPOSITORY}/pulls/${_existing_pr_number}" \
--field title="${pr_title}" \
--field body="${pr_body}" \
--jq '.url'
)
echo "Updated PR: ${pr_url}"
else
echo
echo "Opening PR..."
pr_url=$(
gh pr create \
--base "${BRANCH_DEPLOY}" \
--head "${BRANCH_RELEASE}" \
--title "${pr_title}" \
--body "${pr_body}"
)
pr_number="${pr_url##*/}"
echo "Opened PR #${pr_number}"
fi

0 comments on commit 10ef6e0

Please sign in to comment.