Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(github-actions/k8s-prepare): add action #37

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
```
66 changes: 66 additions & 0 deletions github-actions/k8s-prepare/pull-request.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/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 list \
--head "${BRANCH_RELEASE}" \
--base "${BRANCH_DEPLOY}" \
--state open \
--limit 1 \
--json number \
--jq '.[0].number'
)

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