用sh.yml编写shell
shyml的Python项目详细描述
有时候gnu make会让我想起我多大了,shyml让我感觉自己又像个婴儿了。
开始
安装
使用pip install shyml安装
注意
在~/.local/bin中使用pip install --user进行非根安装
可执行文件和shebang
在repo中创建一个可执行的yaml文件(按惯例命名为sh.yml) 在shebang中使用shyml:
#!/usr/bin/env shyml
然后,开始在其中添加YAML文档
示例
#!/usr/bin/env shymlname:foohelp:barrequires:-otherscript:-./superlongline-shyml_otherjob# call another job as a bash function---name:otherjobscript:|your code here
文档模型
每个YAML文档(用-分隔)都应该包含一个名称键
它可以定义的其他键:
- 帮助:描述工作的帮助文本
- 颜色:用于呈现作业名称的颜色
- 需要:在此作业之前要执行的其他作业的列表
- hook:设置为before以在任何其他操作之前自动执行
- env:env var的yaml散列
cli用法
./sh.yml # lists jobs ./sh.yml jobname # run a job in a local bash shell shell=xonsh ./sh.yml jobname # apparently you your sh.yml contains xonsh instead of bash ^^ ./sh.yml debug jobname # print a job script code ./sh.yml test jobname # print a job help
示例
替换tox.ini
所以,一开始,我想从毒素中得到太多的东西,所以我就生了shyml。 即,集中测试自动化并最终部署(用于集成 测试)在单个多脚本文件中,用于各种上下文:
- 在系统python环境中,即在构建的容器中
- 在用户python环境中,我在这里签出了所有 我想破解的开发源代码版本 (我努力坚持向上,并有向前兼容的代码)
- 在virtualenv中,对已发布的模块版本进行测试。
为了解决这个问题,我使用了这样一个shyml作业,它将通过 默认情况下,如果venv=none,则不设置任何venv,如果 VenV=用户。
---name:installhelp:|Setup and activate a venv for a python executableIf venv=none, it will not do any venv.If venv=user, it will use pip install --user.script:|if [ "${venv-}" = "user" ]; thenpip_install="pip install --user"elif [ "${venv-}" != "none" ]; thenexport python="${python-python3}"export path="${path-.venv.$python}"test -d $path || virtualenv --python=$python $pathset +eux; echo activating $path; source $path/bin/activate; set -euxfi${pip_install-pip install} -Ue .[test]---name:testhelp:Run test in a python3 venv by default.script:shyml_install && py.test -vv --cov src --strict -r fEsxXw ${@-src}
然后,我可以跑:
venv=user ./sh.yml test # in my home venv=none ./sh.yml test # in a built container ./sh.yml test # just run tests in the default venv tox-like
嵌入docker compose.yml
--- name: compose script: | docker-compose -p $(pwd) -f <(cat <<EOF version: '3.5' services: django: build: dockerfile: Dockerfile context: ./ shm_size: 512mb depends_on: - postgres volumes: - ./:/app postgres: image: postgres:10 EOF ) $@
将Dockerfile替换为buildah
--- name: buildah script: - buildcntr1=$(buildah from --quiet docker.io/node:10-alpine) - buildmnt1=$(buildah mount $buildcntr1) - buildah config --env DJANGO_SETTINGS_MODULE=mrs.settings --env UWSGI_MODULE=mrs.wsgi:application --env NODE_ENV=production --env PYTHONIOENCODING=UTF-8 --env PYTHONUNBUFFERED=1 --env STATIC_URL=/static/ --env STATIC_ROOT=/app/static --env UWSGI_SPOOLER_NAMES=mail,stat --env UWSGI_SPOOLER_MOUNT=/app/spooler --env VIRTUAL_PROTO=uwsgi --env LOG=/app/log --env VIRTUAL_PROTO=uwsgi --env GIT_COMMIT=$CI_COMMIT_SHA $buildcntr1 - mkdir -p .cache/{apk,pip,npm} - buildah run -v $(pwd)/.cache/apk:/root/.cache/apk $buildcntr1 apk add --update --cache-dir /root/.cache/apk ca-certificates gettext shadow python3 py3-pillow py3-psycopg2 dumb-init bash git curl uwsgi-python3 uwsgi-http uwsgi-spooler uwsgi-cache uwsgi-router_cache uwsgi-router_static - buildah -v $(pwd)/.cache/pip:/root/.cache/pip run $buildcntr1 pip3 install --upgrade pip - buildah run $buildcntr1 bash -c 'curl -sL https://sentry.io/get-cli/ | bash' - buildah run $buildcntr1 bash -c 'mkdir -p /app && usermod -d /app -l app node && groupmod -n app node && chown -R app:app /app' - buildah config --workingdir /app $buildcntr1 - buildah copy $buildcntr1 yarn.lock .babelrc package.json webpack.config.js /app/ - buildah run $buildcntr1 yarn install --frozen-lockfile - buildah copy $buildcntr1 src/mrs/static /app/src/mrs/static - buildah run $(pwd)/.cache/npm:/npm $buildcntr1 yarn --cache-folder /npm prepare - buildah copy $buildcntr1 requirements.txt /app/ - buildah run -v $(pwd)/.cache/pip:/root/.cache/pip $buildcntr1 pip3 install --upgrade -r /app/requirements.txt - buildah run $buildcntr1 bash - buildah copy $buildcntr1 setup.py src /app/ - buildah run $buildcntr1 bash - buildah run -v $(pwd)/.cache/pip:/root/.cache/pip $buildcntr1 pip3 install --editable /app - buildah run $buildcntr1 mkdir -p /app/{log,static} - buildah run $buildcntr1 mrs collectstatic --noinput --clear - buildah copy $buildcntr1 locale /app/locale - buildah run $buildcntr1 mrs compilemessages -l fr - buildah run $buildcntr1 'find $STATIC_ROOT -type f | xargs gzip -f -k -9'