Introductoryblogpost:https://www.conjur.org/blog/stop-bashing-bash/
______________________________.'.'.|.'.'.'|.'_______________.'______________.'||___________||___________||||_=_|__=__|_=_||||_=_|__=__|_=_|||______||_____===_____||||_____===_____|||__________.'||_____===_____||||_____===_____||.'.'|.'||_____===_____||||_____===_____||.'.'|.'___________|_______________||_______________|.'__________.'||.----------.|.-----___-----.||.-----___-----.|||_____.----------.|]|||_____________||||_____________|||.'[|||||.-----___-----.||.-----___-----.||.'|||||||_____________||||_____________|||==========||||||.-----___-----.||.-----___-----.|||_____|||]o|||_____________||||_____________|||.'['o|||||.-----___-----.||.-----___-----.||.'|||||||||||_____________|||==========||||||||||.-----___-----.|||_____|||]||||||||||.'[|||__________|||_____________||||_____________|||.'________|__________|''----------'''------------------------------'''----------''(o)LGB(o)Theplacetostorefunctionsthatareusedinpipelinesformultiplerepos.
Pleaseaddwhateverisusefultoyou,butkeepittidysoitsstillusefultoeveryoneelse:)
ReleaseStatus:AlphaTL;DR:Readyforuse,butneedsexpansion.
Thefunctionsinthisrepoaretestedandreadyforuse,butcertainlibsareprettymuchplaceholders(eglogging).Thoseneedfurthercontributionsbeforetheyprovideacomprehensivesolution.
License:Apache2.0Seethelicensefile
UsageAddbash-libintoyourprojectinthewaythatbestfitsyourworkflow.Theonlyrequirementisthatyoupintheversionofbash-libthatyouuse.Thisisimportantsothatchangestobash-libdonothavethepowertobreakallprojectsthatusebash-lib.Yourprojectcanthentestupdatestobash-libandrollforwardperiodically.
Options:
Addasubmodule:theyareaneasywaytointegratebash-libandautomaticallyuseasingleSHAuntilmanuallyupdated.Submodulesaddapointerfromamountpointinyourrepototheexternalrepo(bash-lib),andrequireworkflowchangestoensurethatpointerisderferencedduringclone,checkoutandsomeotheropertaions.Addasubtree:Thisrepousessubtreestopullintestdependencies.Subtreescopyanexternalrepointoasubdirectoryofthehostrepo,noworkflowchangesarerequired.Subtreesnaturallykeepasingleversionofbash-libuntilexplicitlyupdated.Notethatsubtreemergecommitsdonotrebasewell⚠️,sobesttokeepsubtreeupdatesinseparatePRsfromnormalcommits.Clonebash-libinyourdeploymentprocess,bash-libdoesn'thavetobewithinyourrepo,justneedstobesomewherewhereyourscriptscansourceinit.Thisiswhereit'smostimportantthatyouimplementamechanismtoalwaysusethesameSHA,asaclonewilltrackmasterbydefault,whichisnotanalloweduseofbash-lib.Onceyouhavebash-libclonedinyourproject,yousourcetwothings:
Sourcebash-lib/init.ThisensuressubmodulesareinitalisedandsetstheBASH_LIB_DIRenvvartotheabsolutepathtothebash-libdir.Thismakesiteasytosourcelibrariesfromotherscripts.Source${BASH_LIB_DIR}/lib-name/libforanylibrariesyouareinterestedin.Youarenowreadytousebash-libfunctions:)
StructureThe/initscriptsetsupeverythingrequiredtousethelibrary,mostimportantlytheBASH_LIB_DIRvariablewhichgivestheabsolutepathtotherootofthelibraryandshouldbeusedforsourcingthemodules.
Therepoisorganizedintolibraries,eachlibraryisadirectorythathasalibfile.Sourcingthelibforalibraryshouldexposeallthefunctionsthatlibraryoffers.Thelibfilemaysourceorreferenceothersupportingfileswithinit'sdirectory.
.├──libname│├──lib│└──supporting-file├──init#initscript,sourcethisfirst├──run-tests#topleveltestscript,executesalltests├──secrets.yml#secretsrequiredforexecutingtests├──test-utils│├──bats#gitsubtree│├──bats-assert-1#gitsubtree│├──bats-support#gitsubtree│├──lib│└──tap2junit└──tests-for-this-repo├──filehandling.bats├──fixtures#│└──libname#Dircontainingtestfixturesforalibrary├──tap2junit├──libname.bats#containstestsforlibname/lib├──python-lint#supportingfilesforpythonlint├──run-bats-tests#scripttorunbatstests├──run-gitleaks#scripttocheckforleakedsecrets└──run-python-lint#scripttorunpythonlintContentsLibraryDescriptionFunctionsfilehandlingFunctionsrelatingtofileandpathhandlingbl_abs_path:EnsureapathisabsolutegitGithelpersbl_git_available:Trueifgitbinaryorfunctionisavailablebl_in_git_repo:Trueifcurrentdirectoryisagitworkingdirectorybl_github_owner_repo:returns$owner/$repoextractedfromtheurloftheoriginremotebl_repo_root:Findtherootofthecurrentgitrepo.bl_all_files_in_repo:Listfilestrackedbygit,excludessubmodules.bl_remote_latest_tag:Returnsthesymbolicnameofthelatesttagfromaremote.bl_remote_latest_tagged_commit:ReturnstheSHAofthemostrecentlytaggedcommitinaremoterepo(tag^{}).bl_remote_sha_for_ref:ReturnstheSHAforagivenreffromanamedremote.bl_remote_tag_for_sha:ReturnsthetagcorrespondingtoaSHAfromanamedremote-ifthereisone.bl_tracked_files_excluding_subtrees:Listfilestrackedbygit,butexcludinganyfilesthatareinpathslistedin.gittrees.bl_gittrees_present:Succeedsif.gittreesispresentintherootoftherepo,otherwisefails.bl_cat_gittrees:Returnsthecontentsof.gittreesfromthetopleveloftherepo,excludinganycomments.Failsif.gittreesisnotpresent.githubGithubRelatedFunctionsbl_hub_available:Trueifhubbinaryorfunctionisavailablebl_hub_creds_available:Trueifhubcredsareavailable(fileorenvvars)bl_hub_check:Preflightcheckforhub,trueifgitinstalled,ingitrepo,hubinstalledandhubcredsareavailablebl_hub_download_latest:Downloadlatesthubbinaryfromgithubandinstallto~/binorspecifiedpathbl_hub_issue_number_for_title:Findtheissuenumberforanissuefromitstitle,searchesopenissuesinthecurrentrepo.(currentrepo=workdingdirectory,repoisfoundbyoriginremote).Ifmultipleissuesmatchthesuppliedtitlestring,onlythefirstisreturned.bl_hub_add_issue_comment:Addacommenttoanissuebl_hub_comment_or_create_issue:Createissueifanissuematchingthetitledoesn'texist.Ifamatchisfound,addacommenttoithelpersBashscriptinghelpersbl_die:printmessageandexit1bl_fail:printmessageandreturn1bl_spushd/bl_spopd:Safeverisonsofpushd&popdthatcalldieifthepush/popfails,theyalsodropstdout.bl_is_num:Checkifavalueisanumberviaregexbl_retry:Retryacommanduntilitsucceedsuptoauserspecifiedmaximumnumberofattempts.Escalatingdelaybetweenattempts.bl_retry_constant:Retryacommanduntilitsucceedswithaconstantdelaybetweenattemptsk8sUtilsforconnectingtoK8sbl_build_gke_image:BuilddockerimageforrunningkubectlcommandsagainstGKE.bl_delete_gke_image:DeleteimagefromGKE.bl_run_docker_gke_command:Runcommandingke-utilscontainer,alreadyauthenticatedtok8scluster.loggingHelpersrelatedtologging.bl_announce:Echomessageinasciibannertodistinguishitfromotherlogmessages.bl_log:Logamessageatthespecifiedlevel.Defaultloglevelisinfo,changelevelbysettingenvironmentvariableBASH_LIB_LOG_LEVELbl_check_log_level:Checkifavalueisavalidbashlibloglevelbl_debug:Logamessageatdebuglevelbl_info:Logamessageatinfolevelbl_warning:Logamessageatwarninglevelbl_error:Logamessageaterrorlevelbl_fatal:LogamessageatfatallevelRubyHelpersrelatedtorubyinfrastructurebl_gem_latest_version:Returnthelatestversionofagemfromrubygems.orgbl_jq_available:Checkjqbinaryisavailablebl_curl_available:Checkcurlbinaryisavailabletest-utilsHelpersforexecutingtestsbl_shellcheck_script:Executeshellcheckagainstascript,usesdocker.bl_find_scripts:Findgittrackedfileswithextension.bl_tap2junit:ConvertasubsetofTAPtoJUnitXML.Retainslogsforerrors.bl_validate_changelog:CheckCHANGELOG.md(oraspecifiedfile)complieswithkeepachangelog.comformat.ContributingForfurtherinformationoncontributing,style&testing,pleaseseeCONTRIBUTING.md
MaintainersHughSaunders
评论