-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
document typical manual operations envx and create-adhoc-backup.sh ba…
…sh script, prepare tagged release pipeline
- Loading branch information
Showing
4 changed files
with
356 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,8 +100,8 @@ jobs: | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} | ||
labels: ${{ steps.meta.outputs.labels }} | ||
|
||
release: | ||
if: startsWith(github.ref, 'refs/tags/v') || (github.event_name == 'push' && github.ref == 'refs/heads/main') | ||
release-chart: | ||
if: startsWith(github.ref, 'refs/tags/v') | ||
|
||
needs: | ||
- build-test | ||
|
@@ -124,3 +124,107 @@ jobs: | |
env: | ||
CR_TOKEN: "${{ secrets.GITHUB_TOKEN }}" | ||
CR_SKIP_EXISTING: "true" | ||
|
||
|
||
release-bins: | ||
if: startsWith(github.ref, 'refs/tags/v') | ||
|
||
needs: | ||
- build-test | ||
permissions: | ||
contents: write | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Configure Git | ||
run: | | ||
git config user.name "$GITHUB_ACTOR" | ||
git config user.email "[email protected]" | ||
- name: Build the Docker image with binaries | ||
run: | | ||
docker build --target builder --file Dockerfile --tag ${IMAGE_NAME}-builder:${GITHUB_SHA:8} . | ||
docker create --name builder ${IMAGE_NAME}-builder:${GITHUB_SHA:8} | ||
mkdir -p dist | ||
- name: Extract binaries from container | ||
run: | | ||
for binary in backup-ns-linux-amd64 backup-ns-linux-arm64 backup-ns-darwin-amd64 backup-ns-darwin-arm64; do | ||
docker cp builder:/app/bin/$binary dist/ || exit 1 | ||
done | ||
cp LICENSE dist/ | ||
- name: Create release archives | ||
run: | | ||
cd dist | ||
for file in backup-ns-*; do | ||
tar czf "${file}.tar.gz" "$file" LICENSE | ||
done | ||
- name: Prepare script archive | ||
run: | | ||
cp create-adhoc-backup.sh dist/ | ||
chmod +x dist/create-adhoc-backup.sh | ||
cd dist | ||
tar czf "create-adhoc-backup.sh.tar.gz" create-adhoc-backup.sh LICENSE | ||
- name: Upload release artifacts | ||
uses: softprops/action-gh-release@v1 | ||
with: | ||
files: | | ||
dist/backup-ns-linux-amd64.tar.gz | ||
dist/backup-ns-linux-arm64.tar.gz | ||
dist/backup-ns-darwin-amd64.tar.gz | ||
dist/backup-ns-darwin-arm64.tar.gz | ||
dist/create-adhoc-backup.sh.tar.gz | ||
name: Release ${{ github.ref_name }} | ||
body: | | ||
## backup-ns ${{ github.ref_name }} | ||
### Docker image | ||
```bash | ||
docker pull ghcr.io/allaboutapps/backup-ns:${{ github.ref_name }} | ||
``` | ||
### Local Installation (Linux/amd64) | ||
```bash | ||
curl -Lo backup-ns.tar.gz https://github.com/${{ github.repository }}/releases/download/${{ github.ref_name }}/backup-ns-linux-amd64.tar.gz | ||
tar xzf backup-ns.tar.gz | ||
chmod +x backup-ns-linux-amd64 | ||
sudo mv backup-ns-linux-amd64 /usr/local/bin/backup-ns | ||
rm backup-ns.tar.gz | ||
``` | ||
### Local Installation (macOS/arm64) | ||
```bash | ||
curl -Lo backup-ns.tar.gz https://github.com/${{ github.repository }}/releases/download/${{ github.ref_name }}/backup-ns-darwin-arm64.tar.gz | ||
tar xzf backup-ns.tar.gz | ||
chmod +x backup-ns-darwin-arm64 | ||
sudo mv backup-ns-darwin-arm64 /usr/local/bin/backup-ns | ||
rm backup-ns.tar.gz | ||
``` | ||
### Adhoc backup script | ||
```bash | ||
curl -Lo create-adhoc-backup.sh.tar.gz https://github.com/${{ github.repository }}/releases/download/${{ github.ref_name }}/create-adhoc-backup.sh.tar.gz | ||
tar xzf create-adhoc-backup.sh.tar.gz | ||
chmod +x create-adhoc-backup.sh | ||
sudo mv create-adhoc-backup.sh /usr/local/bin/ | ||
rm create-adhoc-backup.sh.tar.gz | ||
``` | ||
draft: false | ||
prerelease: false | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Stop container | ||
if: always() | ||
run: docker stop builder | ||
|
||
- name: Cleanup | ||
if: always() | ||
run: docker rm builder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/bin/bash | ||
set -Eeo pipefail | ||
|
||
# This script creates an adhoc backup job for the (current) namespace in kubectl context. | ||
# $ BAK_DRY_RUN=true ./create-adhoc-backup.sh | ||
|
||
# ------------- | ||
# env | ||
|
||
# You can provide the following env vars to this script: | ||
NAMESPACE="${NAMESPACE:=$( (kubectl config view --minify | grep namespace | cut -d" " -f6) || kubectl get sa -o=jsonpath='{.items[0]..metadata.namespace}' || echo "default")}" | ||
BACKUP_JOB_WAIT="${BACKUP_JOB_WAIT:="true"}" | ||
BACKUP_JOB_WAIT_TIMEOUT="${BACKUP_JOB_WAIT_TIMEOUT:="1h"}" | ||
|
||
# BAK_* variables are injected as ENV into the job container, the following are the default overrides | ||
# see https://git.allaboutapps.at/projects/AW/repos/backup-ns/browse/lib/bak_env.sh | ||
|
||
# BAK_LABEL_VS_TYPE: adhoc (flag for adhoc backup job) | ||
BAK_LABEL_VS_TYPE="${BAK_LABEL_VS_TYPE:="adhoc"}" | ||
# BAK_LABEL_VS_RETAIN: days (flag for retention policy, currently only days is supported) | ||
BAK_LABEL_VS_RETAIN="${BAK_LABEL_VS_RETAIN:="days"}" | ||
|
||
# ------------- | ||
# main | ||
|
||
echo "Creating adhoc backup in ns=${NAMESPACE}..." | ||
|
||
# Check deps | ||
command -v awk >/dev/null || fatal "awk is required but not found." | ||
command -v yq >/dev/null || fatal "yq is required but not found." | ||
|
||
# Collect BAK_* environment variables and construct yq commands to inject the explicit BAK_* env vars into the kubectl job definition | ||
backup_env_vars=$(( set -o posix ; set ) | grep "BAK_" | awk -F= '{print $1}') | ||
yq_cmd="" | ||
while IFS= read -r bak_key; do | ||
bak_value=$(eval "echo \$${bak_key}") | ||
yq_cmd+=" | yq eval '.spec.template.spec.containers[0].env += [{\"name\": \"${bak_key}\", \"value\": \"${bak_value}\"}]' -" | ||
done <<< "$backup_env_vars" | ||
# create the command so we can print it right before executing it | ||
timestamp=$(date +"%Y-%m-%d-%H%M%S") | ||
backup_cmd="kubectl create job --from=cronjob.batch/backup \"backup-adhoc-${timestamp}\" -o yaml --dry-run=client -n \"${NAMESPACE}\" \ | ||
${yq_cmd} \ | ||
| kubectl apply -f -" | ||
echo "Prepared backup command:" | ||
echo "$backup_cmd" | ||
echo "Ensuring there is no other backup job running within ns=${NAMESPACE}..." | ||
if [ "$BACKUP_JOB_WAIT" == "true" ]; then | ||
sleep 3 | ||
fi | ||
# kubectl get job -l app=backup | ||
# ensure there is currently no other backup job running, if that is the case, exit 1 | ||
if kubectl -n "${NAMESPACE}" get job -l app=backup -o jsonpath='{.items[*].status.active}' | grep -q "1"; then | ||
>&2 echo "Another backup job is currently running, exit 1!" | ||
exit 1 | ||
fi | ||
# Create the backup job with the overwritten env vars | ||
echo "Creating job/backup-adhoc-${timestamp} for ns=${NAMESPACE}..." | ||
eval "$backup_cmd" | ||
echo "Follow logs with:" | ||
echo " kubectl logs -n ${NAMESPACE} -f job/backup-adhoc-${timestamp}" | ||
if [ "$BACKUP_JOB_WAIT" == "true" ]; then | ||
sleep 2 | ||
echo "Waiting for backup job/backup-adhoc-${timestamp} to complete for ns=${NAMESPACE}..." | ||
kubectl -n "${NAMESPACE}" wait --for=condition=complete --timeout="$BACKUP_JOB_WAIT_TIMEOUT" "job/backup-adhoc-${timestamp}" | ||
fi | ||
echo "" | ||
echo "List all snapshots in this namespace via:" | ||
echo "kubectl -n ${NAMESPACE} get vs -lbackup-ns.sh/retain -Lbackup-ns.sh/type,backup-ns.sh/retain,backup-ns.sh/daily,backup-ns.sh/weekly,backup-ns.sh/monthly,backup-ns.sh/delete-after" | ||
echo "" | ||
echo "Adhoc backups are only kept for 30days by default, you can delete this auto-retention flag manually by running:" | ||
echo "kubectl -n ${NAMESPACE} label vs/<snapshot-name> backup-ns.sh/retain- backup-ns.sh/delete-after-" |
Oops, something went wrong.