Share
Embed
🤖 Automating key workflows in your daily developer life!
In this talk, I want to guide the audience through the tools I implemented and the scripts I built over time in our CI system to automate common tasks like:
- Daily release deliveries to QA (releases run automatically only if there were any new commits since the last one)
- Automated Git(Hub) tags creation for each release
- Automated release notes included in the tags (for QA)
- Automated code formatting with standard rules automatically shared across all the members of a team
I currently work with Bitrise, which means my example may include that tool (but the scripts are tool agnostic).
Alessandro Mautone
May 14, 2022
Transcript
42 slides
-
Overview Automated Git(Hub) tags creation for each release 🤖 Automated
release notes for each release 📃 Automated code formatting with standard rules automatically shared across all the members of a team 📏 -
• Daily work fl ow • Check if there are
any commits since the last available tag • If yes -> generate and push a new tag CI setup -
Tag format: Beta_X E.g. Beta_1, Beta_2… Beta_123, etc Script -
Check commits since last release -
function get_latest_tag() { latestTag=$(git for-each-ref "refs/**/*Beta_[0-9]*" —sort=-creatordate --format='%(refname:short)' --count=1) }
Script - Check commits since last release -
function get_latest_tag_number() { # Gets beta number from format like
"beta_100" latestTagNumber=${ADDR[1]} } Script - Check commits since last release -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release git pull --rebase git fetch --tags -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD) -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release # Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release get_latest_tag_number -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release generate_new_tag_name export_tag_name -
git pull --rebase git fetch --tags get_latest_tag commitsSinceLastTag=$(git log $latestTag..HEAD)
# Check if there are commits since latest tag if [ -n "$commitsSinceLastTag" ]; then echo "There are commits since last tag" # Splits passed var by speci fi ed separator IFS='_' read -ra ADDR <<<"$latestTag" get_latest_tag_number # Increases beta number latestTagNumber=$(($latestTagNumber + 1)) echo $latestTagNumber generate_new_tag_name export_tag_name else echo "There are NO commits since last tag" exit 1 f Script - Check commits since last release echo "There are NO commits since last tag" exit 1 -
branch=$(git rev-parse --abbrev-ref HEAD) git tag -a $GIT_BETA_TAG -m "Creating
new automated tag for qa release" $branch git push --tags Script - Tag creator -
Overview Automated Git(Hub) tags creation for each release 🤖 Automated
release notes for each release 📃 Automated code formatting with standard rules automatically shared across all the members of a team 📏 -
githubReleasesUrl="https://your-repo/releases" echo "Getting latest release draft id" releaseId=$(curl -u $GITHUB_USERNAME:$GITHUB_TOKEN
$githubReleasesUrl | jq '.[] | select(.draft == true) | "\(.id)"') releaseIdWithoutQuotes=$(sed -e 's/^"//' -e 's/"$//' <<<"$releaseId") echo "Publishing latest release draft" curl -u $GITHUB_USERNAME:$GITHUB_TOKEN --request PATCH "$githubReleasesUrl/ $releaseIdWithoutQuotes" --header "Content-Type: application/json" --header "Accept: application/json" --data "{\"tag_name\": \"$GIT_BETA_TAG\", \"target_commitish\": \"develop\", \"name\": \"$GIT_BETA_TAG\", \"draft\": false, \"prerelease\": false}" Script - Release publisher -
githubReleasesUrl="https://your-repo/releases" echo "Getting latest release draft id" releaseId=$(curl -u $GITHUB_USERNAME:$GITHUB_TOKEN
$githubReleasesUrl | jq '.[] | select(.draft == true) | "\(.id)"') releaseIdWithoutQuotes=$(sed -e 's/^"//' -e 's/"$//' <<<"$releaseId") echo "Publishing latest release draft" curl -u $GITHUB_USERNAME:$GITHUB_TOKEN --request PATCH "$githubReleasesUrl/ $releaseIdWithoutQuotes" --header "Content-Type: application/json" --header "Accept: application/json" --data "{\"tag_name\": \"$GIT_BETA_TAG\", \"target_commitish\": \"develop\", \"name\": \"$GIT_BETA_TAG\", \"draft\": false, \"prerelease\": false}" Script - Release publisher -
githubReleasesUrl="https://your-repo/releases" echo "Getting latest release draft id" releaseId=$(curl -u $GITHUB_USERNAME:$GITHUB_TOKEN
$githubReleasesUrl | jq '.[] | select(.draft == true) | "\(.id)"') releaseIdWithoutQuotes=$(sed -e 's/^"//' -e 's/"$//' <<<"$releaseId") echo "Publishing latest release draft" curl -u $GITHUB_USERNAME:$GITHUB_TOKEN --request PATCH "$githubReleasesUrl/ $releaseIdWithoutQuotes" --header "Content-Type: application/json" --header "Accept: application/json" --data "{\"tag_name\": \"$GIT_BETA_TAG\", \"target_commitish\": \"develop\", \"name\": \"$GIT_BETA_TAG\", \"draft\": false, \"prerelease\": false}" Script - Release publisher -
githubReleasesUrl="https://your-repo/releases" echo "Getting latest release draft id" releaseId=$(curl -u $GITHUB_USERNAME:$GITHUB_TOKEN
$githubReleasesUrl | jq '.[] | select(.draft == true) | "\(.id)"') releaseIdWithoutQuotes=$(sed -e 's/^"//' -e 's/"$//' <<<"$releaseId") echo "Publishing latest release draft" curl -u $GITHUB_USERNAME:$GITHUB_TOKEN --request PATCH "$githubReleasesUrl/ $releaseIdWithoutQuotes" --header "Content-Type: application/json" --header "Accept: application/json" --data "{\"tag_name\": \"$GIT_BETA_TAG\", \"target_commitish\": \"develop\", \"name\": \"$GIT_BETA_TAG\", \"draft\": false, \"prerelease\": false}" Script - Release publisher -
githubReleasesUrl="https://your-repo/releases" echo "Getting latest release draft id" releaseId=$(curl -u $GITHUB_USERNAME:$GITHUB_TOKEN
$githubReleasesUrl | jq '.[] | select(.draft == true) | "\(.id)"') releaseIdWithoutQuotes=$(sed -e 's/^"//' -e 's/"$//' <<<"$releaseId") echo "Publishing latest release draft" curl -u $GITHUB_USERNAME:$GITHUB_TOKEN --request PATCH "$githubReleasesUrl/ $releaseIdWithoutQuotes" --header "Content-Type: application/json" --header "Accept: application/json" --data "{\"tag_name\": \"$GIT_BETA_TAG\", \"target_commitish\": \"develop\", \"name\": \"$GIT_BETA_TAG\", \"draft\": false, \"prerelease\": false}" Script - Release publisher -
Overview Automated Git(Hub) tags creation for each release 🤖 Automated
release notes for each release 📃 Automated code formatting with standard rules automatically shared across all the members of a team 📏 -
📏 Automated code formatting with standard rules automatically shared across
all the members of a team -
spotless { // optional: limit format enforcement to just the
fi les changed by this feature branch ratchetFrom 'origin/main' kotlin { ktlint() // or ktfmt() or others trimTrailingWhitespace() indentWithTabs() // or spaces. Takes an integer argument if you don't like 4 endWithNewline() licenseHeader '/* (C)$YEAR */' // or licenseHeaderFile } kotlinGradle { target '*.gradle.kts' // default target for kotlinGradle ktlint() // or ktfmt() or prettier() } } Spotless - configuration -
task createSpotlessPreCommitHook() { def gitHooksDirectory = new File("$project.rootDir/.git/hooks/") if (!gitHooksDirectory.exists())
gitHooksDirectory.mkdirs() new File("$project.rootDir/.git/hooks", "pre-commit").text = """ #!/bin/bash echo "Running spotless check" ./gradlew spotlessApply if [ \$? -eq 0 ] then echo "Spotless check succeed" git add . else echo "Spotless check failed" >&2 exit 1 fi """ "chmod +x .git/hooks/pre-commit".execute() } Spotless - configuration -
data class Bike(val name: String, val model: String, val brand:
String) Spotless - Result data class Bike( val name: String, val model: String, val brand: String ) Before After -
if(contentSize = storageAvailable) { displayFullStorageMessage() } Spotless - Result if(contentSize
= storageAvailable) { displayFullStorageMessage() } Before After -
Overview Automated Git(Hub) tags creation for each release 🤖 Automated
release notes for each release 📃 Automated code formatting with standard rules automatically shared across all the members of a team 📏