Skip to content

Instantly share code, notes, and snippets.

View atemate's full-sized avatar
🐕

Artem Yushkovskiy atemate

🐕
View GitHub Profile
@atemate
atemate / workflow.yml
Created January 5, 2023 17:34
GitHub Actions workflow to use git to calculate last change on a directory/file
...
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }} # checkout pull request HEAD commit instead of merge commit
fetch-depth: 0 # fetch all history for all branches and tags
- name: Set env variables
id: setup-vars
run: |
@atemate
atemate / log_release_history.sh
Last active January 5, 2023 15:30
List history of the "tag: " line changes in a charts repo
FILE_PATH=path/to/config/file.yaml
LINE_MARK="tag: "
line_number=$(awk "/$LINE_MARK/ {print FNR}" $FILE_PATH)
test $line_number # TODO: better error reporting
git log --oneline --decorate --pretty=format:"%cs [%h] by %aN%n" -u -L ${line_number},${line_number}:$FILE_PATH \
| grep -v "diff --git\|---\|+++\|@@\|- \|^$"
@atemate
atemate / .flake8
Last active November 10, 2022 17:22
Pre-commit config for typical python projects
[flake8]
max-line-length = 88
extend-ignore = \
E203, \ # whitespace before ':'
F841, \ # local variable name is assigned to but never used
E266, \ # too many leading '#' for block comment
per-file-ignores =
*/__init__.py: F401
@atemate
atemate / action.yml
Created October 10, 2022 10:25
GH Action to Transform input json, similar to jq
name: Transform input json, similar to jq
inputs:
input_json: {type: string, required: true}
transform: {type: string, required: true}
outputs:
result:
value: ${{ steps.parse.outputs.result }}
@atemate
atemate / export-vars__action.yaml
Created October 7, 2022 13:13
GH Action to accept arguments as json dict with some value to overload
name: Setup env variables
inputs:
vars_json: {type: string, default: "{}"}
# TODO: add argument "by_workflow_call: bool" to skip some steps
# env vars to override:
PAYLOAD: {type: string, default: ""}
TRAIN_IMAGE_TAG: {type: string, default: ""}
outputs:
@atemate
atemate / bash-tricks-array-string.sh
Last active October 3, 2022 10:18
Bash array and key-value string split
arr=()
arr+=( "FOO=foo" )
arr+=( "BAR=bar" )
for kv in ${arr[@]}; do
k=${kv%%=*}; v=${kv##*=}
echo "Debug: name=$k value=$v"
echo "::set-output name=$k::$v"
echo "$k=$v" >> $GITHUB_ENV
done
@atemate
atemate / delete-gh-actions-workflow-runs.sh
Created September 20, 2022 08:18
Delete multiple workflow runs for GH Actions
# source: https://stackoverflow.com/a/64473987
# Deletes ALL runs except those named "Dev", "Test" and "Prod".
user=GH_USERNAME repo=REPO_NAME
out=$(gh api repos/$user/$repo/actions/runs --paginate -q '.workflow_runs[] | select(.head_branch != "main")' )
ids=$(echo $out | jq 'select(.name != "Dev" and .name != "Test" and .name != "Prod") | .id')
for id in $ids; do echo $id; gh api repos/$user/$repo/actions/runs/$id -X DELETE & sleep 0.1; done
@atemate
atemate / workflow.yml
Created September 20, 2022 07:28
GH Actions to trigger by label terraform apply
...
on:
pull_request:
types: [synchronize, labeled]
push:
branches: [main]
...
@atemate
atemate / camel_to_snake.py
Last active August 1, 2022 11:29
Convert CamelCase to snake_case
def camel_to_snake(s):
# source: https://gist.github.com/jaytaylor/3660565
import re
_underscorer1 = re.compile(r'(.)([A-Z][a-z]+)')
_underscorer2 = re.compile('([a-z0-9])([A-Z])')
subbed = _underscorer1.sub(r'\1_\2', s)
return _underscorer2.sub(r'\1_\2', subbed).lower()
def test_camel_to_snake():
assert camel_to_snake('snakesOnAPlane') == 'snakes_on_a_plane'
@atemate
atemate / query.sh
Created July 20, 2022 15:54
Script to make BQ requests
#!/bin/env bash
set -euo pipefail
USAGE="[ERROR] Usage: '$0 QUERY_FILE_NAME key1=value2 key2=value2 ...'"
if [ "$#" -lt 1 ]; then
echo "[ERROR] Expect at least 1 argument, received: $#" >&2
echo $USAGE >&2
exit 1
fi