Skip to content

Commit

Permalink
Merge pull request #5 from azohra/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Azbagheri authored Sep 27, 2019
2 parents 716b82f + fc91f2a commit bee583d
Show file tree
Hide file tree
Showing 19 changed files with 426 additions and 100 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: PR-workflow

on: pull_request

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Lint
uses: zbeekman/[email protected]

unit_tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Run tests
run: ./tests/test_runner
shell: bash
needs: lint
21 changes: 21 additions & 0 deletions .github/workflows/release-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Release

on:
push:
branches:
- master

jobs:
github_release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Tag commit and release
uses: ncipollo/release-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
tag: v0.4.0
commit: master
name: Fetching logs post build vs streaming
bodyFile: ./docs/release_notes/v0.4.0.md
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
.DS_Store
.DS_Store
test_script.sh
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ Usage: gitrise [options]

## API Reference

Please see [here](https://api-docs.bitrise.io/#/) for Bitrise API Reference
Please see [here](https://api-docs.bitrise.io/#/) for Bitrise API Reference

## Tests

To run the unit tests, use the following command in the project directory
```bash
./tests/test_runner
```
8 changes: 8 additions & 0 deletions docs/release_notes/v0.4.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Release Notes


* Changed the way logs were fetched from Bitrise. Instead of streaming, build logs are now pulled after the build is completed.

* Improved the code qulity by adding error handling mechanisms and a lot more unit tests.

* Switched from Travis CI to Github Actions to run CI/CD pipelines. Releases are now done automatically using the CD pipeline.
180 changes: 115 additions & 65 deletions gitrise.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/bin/bash
VERSION="0.3.0"
# shellcheck disable=SC2155
# disbales "Declare and assign separately to avoid masking return values."

VERSION="0.4.0"
APP_NAME="Gitrise Trigger"

build_complete=0
build_output=""
build_index=0
build_slug=""
build_url=""
build_status=0
previous_build_status_text=""
exit_code=""
log_url=""

usage() {
echo ""
Expand All @@ -22,7 +27,7 @@ usage() {

# parsing space separated options
POSITIONAL=()
while [[ $# -gt 0 ]]; do
while [ $# -gt 0 ]; do
key="$1"
case $key in
-v|--version)
Expand Down Expand Up @@ -66,15 +71,9 @@ while [[ $# -gt 0 ]]; do
esac
done


# restore positional parameters
set -- "${POSITIONAL[@]}"

# if [[ -z $WORKFLOW || -z $BRANCH || -z $PROJECT_SLUG || -z $ACCESS_TOKEN ]]; then
# echo "Please re-run -h or --help for help."
# exit 1
# fi

# map environment variables to objects Bitrise will accept.
# ENV_STRING is passed as argument
process_env_vars () {
Expand All @@ -92,90 +91,141 @@ process_env_vars () {
IFS=',' read -r -a env_array <<< "$env_string"
for i in "${env_array[@]}"
do
# shellcheck disable=SC2162
# disables "read without -r will mangle backslashes"
IFS=':' read -a array_from_pair <<< "$i"
key="${array_from_pair[0]}"
value="${array_from_pair[1]}"
# shellcheck disable=SC2089
# disables "Quotes/backslashes will be treated literally. Use an array."
result+="{\"mapped_to\":\"$key\",\"value\":\"$value\",\"is_expand\":true},"
done
echo "[$(sed 's/,$//' <<< $result)]"
echo "[${result/%,}]"
}

intro () {
if [ "$TESTING_ENABLED" ]; then
if [ "${TESTING_ENABLED}" = "true" ]; then
echo "Gitrise is running in testing mode"
else
printf "%s VERSION %s \nLaunched on $(date)" "$APP_NAME" "$VERSION"
printf "%s VERSION %s \nLaunched on $(date)\n" "$APP_NAME" "$VERSION"
fi
}

pre_build () {
# shellcheck disable=SC2120
# disables "foo references arguments, but none are ever passed."
trigger_build () {
local result=""
if [ ! "$TESTING_ENABLED" ]; then
if [ -z "${TESTING_ENABLED}" ]; then
local environments=$(process_env_vars "$ENV_STRING")
local payload="{\"hook_info\":{\"type\":\"bitrise\"},\"build_params\":{\"branch\":\"$BRANCH\",\"workflow_id\":\"$WORKFLOW\",\"environments\":$environments \
}}"
local command="curl --silent -X POST https://api.bitrise.io/v0.1/apps/$PROJECT_SLUG/builds \
--data '$payload' \
--header 'Authorization: $ACCESS_TOKEN'"
result=$(eval "${command}")
result=$(eval "${command}")
else
result=$(<./testdata/build_trigger_response.json)

result=$(<./testdata/"$1"_build_trigger_response.json)
fi
status=$(echo "$result" | jq ".status" | sed 's/"//g' )
if [ "$status" != "ok" ]; then
msg=$(echo "$result" | jq ".message" | sed 's/"//g')
echo "ERROR: $msg"
exit 1
else
build_url=$(echo "$result" | jq ".build_url" | sed 's/"//g')
build_slug=$(echo "$result" | jq ".build_slug" | sed 's/"//g')
fi
local build_url=$(echo "${result}" | jq ".build_url" | sed 's/"//g')
build_slug=$(echo "${result}" | jq ".build_slug" | sed 's/"//g')
printf "\nHold on... We're about to liftoff! 🚀\n \nBuild URL: %s" "${build_url}"
printf "\nHold on... We're about to liftoff! 🚀\n \nBuild URL: %s\n" "${build_url}"
}

mid_build () {
while [ "$build_complete" != "1" ]; do
local __command="curl --silent -X GET https://api.bitrise.io/v0.1/apps/$PROJECT_SLUG/builds/$build_slug/log --header 'Authorization: $ACCESS_TOKEN'"
local __next_output=$(eval ${__command} | jq ".log_chunks[0]" | jq ".chunk" | sed 's/"//g')
local __next_index=$(eval ${__command} | jq ".log_chunks[0]" | jq ".position")
local __new_chunks=${__next_output#${__build_output}}
local __command="curl --silent -X GET https://api.bitrise.io/v0.1/apps/$PROJECT_SLUG/builds/$build_slug --header 'Authorization: $ACCESS_TOKEN'"
local __state=$(eval ${__command} | jq ".data" | jq ".status_text" | sed 's/"//g')
if [ ${__state} != "in-progress" ]; then
build_complete=1
fi
if [ "${__new_chunks}" = "null" ]; then
echo "Waiting for worker... "
sleep 1
else
if [ "$build_index" != "${__next_index}" ]; then
printf -- "$(echo ${__new_chunks} | tr -d '%')"
build_output=${__next_output}
build_index=${__next_index}
fi
get_build_status () {
local response=""
while [ "${build_status}" = 0 ]; do
if [ -z "${TESTING_ENABLED}" ]; then
sleep 10
local command="curl --silent -X GET https://api.bitrise.io/v0.1/apps/$PROJECT_SLUG/builds/$build_slug --header 'Authorization: $ACCESS_TOKEN'"
response=$(eval "${command}")
else
response=$(< ./testdata/build_status_response.json)
fi
local current_build_status_text=$(echo "$response" | jq ".data .status_text" | sed 's/"//g')
if [ "$previous_build_status_text" != "$current_build_status_text" ]; then
echo "Build $current_build_status_text"
previous_build_status_text="${current_build_status_text}"
fi
build_status=$(echo "$response" | jq ".data .status")
done

if [ "$build_status" = 1 ]; then exit_code=0; else exit_code=1; fi
}

pst_build () {
local __command="curl --silent -X GET https://api.bitrise.io/v0.1/apps/$PROJECT_SLUG/builds/$build_slug --header 'Authorization: $ACCESS_TOKEN'"
local __state=$(eval ${__command} | jq ".data" | jq ".status")
echo
echo
if [ "${__state}" -eq "0" ]; then
echo "Oh No! Build $build_slug has timed out! 😱"
exit 1
elif [ "${__state}" -eq "1" ]; then
echo "Build successful 🎉"
exit 0
elif [ "${__state}" -eq "2" ]; then
echo "Build Failed 🚨"
exit 1
elif [ "${__state}" -eq "3" ]; then
echo "Build Aborted 💥"
exit 1
build_status_message () {
local status="$1"
case "$status" in
"0")
echo "Build TIMED OUT based on mobile trigger internal setting"
;;
"1")
echo "Build Successful 🎉"
;;
"2")
echo "Build Failed 🚨"
;;
"3")
echo "Build Aborted 💥"
;;
*)
echo "Invalid build status 🤔"
exit 1
;;
esac
}

# shellcheck disable=SC2120
# disables "foo references arguments, but none are ever passed."
get_log_info(){
local log_is_archived=false
local counter=0
local retry=4
local polling_interval=5
local response=""
while ! "$log_is_archived" && [[ "$counter" -lt "$retry" ]]; do
if [ -z "${TESTING_ENABLED}" ] ; then
sleep "$polling_interval"
local command="curl --silent -X GET https://api.bitrise.io/v0.1/apps/$PROJECT_SLUG/builds/$build_slug/log --header 'Authorization: $ACCESS_TOKEN'"
response=$(eval "$command")
else
response="$(< ./testdata/"$1"_log_info_response.json)"
fi
log_is_archived=$(echo "$response" | jq ".is_archived")
((counter++))
done
log_url=$(echo "$response" | jq ".expiring_raw_log_url" | sed 's/"//g')
if ! "$log_is_archived" || [ -z "$log_url" ]; then
echo "LOGS WERE NOT AVAILABLE - go to $build_url to see log."
exit ${exit_code}
fi
}

get_logs(){
local url="$1"
local logs=$(curl --silent -X GET "$url")

echo "================================================================================"
echo "============================== Bitrise Logs Start =============================="
echo "$logs"
echo "================================================================================"
echo "============================== Bitrise Logs End =============================="

}
# No function execution when the script is sourced
if [ "$0" = "$BASH_SOURCE" ] && [ -z "${TESTING_ENABLED}" ]; then
# shellcheck disable=SC2119
# disables "use foo "$@" if function's $1 should mean script's $1."
if [ "$0" = "${BASH_SOURCE[0]}" ] && [ -z "${TESTING_ENABLED}" ]; then
intro
pre_build
mid_build
pst_build
fi
trigger_build
get_build_status
get_log_info
get_logs "$log_url"
build_status_message "$build_status"
exit ${exit_code}
fi
43 changes: 43 additions & 0 deletions testdata/archived_log_info_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"expiring_raw_log_url":"https://bitrise_test_url.com",
"generated_log_chunks_num":13,
"is_archived":true,
"log_chunks":
[
{
"chunk":"+-------it: bash ------------------------|\n+ brew cask install docker-toolbox\n==> Satisfying dependencies\n==> Installing Cask dependencies: virtualbox\n==> Caveats\nTo install and/or use virtualbox you may need to enable its kernel extension in:\n System Preferences → Security & Privacy → General\nFor more information refer to vendor documentation or this Apple Technical Note:\n https://developer.apple.com/library/content/technotes/tn2459/_index.html\n\n==> Satisfying dependencies\n==> Downloading https://download.virtualbox.org/virtualbox/6.0.10/VirtualBox-6.0.10-132072-OSX.dmg\n",
"position":4
},
{
"chunk":"==> Verifying SHA-256 checksum for Cask 'virtualbox'.\n==> Installing Cask virtualbox\n",
"position":5
},
{
"chunk":"==> Running installer for virtualbox; your password may be necessary.\n==> Package installers may write to any location; options such as --appdir are ignored.\n",
"position":6
},
{
"chunk":"installer: Package nat base path /\ninstaller: The install was successful.\n🍺 virtualbox was successfully installed!\n",
"position":7},
{
"chunk":"==> Downloading https://github.com/",
"position":8},
{
"chunk":"==> Verifying SHA-256 checksum for Cask 'docker-toolbox'.\n==> Installing Cask docker-toolbox\n==>e necessary.\n==> Package installers may write to any location; options such as --appdir are ignored.\n",
"position":9},
{
"chunk":"installer: Package name is Docker Toolbox\ninstaller: choices changes file '/var/folders/90/5stft2v13fb_m_gv3c8x9nwc0000gn/T/choices20190822-1125-typiw2.xml' applied\ninstaller: Installing at base path /\ninstaller: The install was successful.\n==> Changing ownership of paths required by docker-toolbox; your password may be necessary\n",
"position":10},
{
"chunk":"🍺 docker-toolbox was successfully Enabling it in the BIOS is mandatory\"\n| |\n+---+---------------------------------------------------------------+----------+\n| \u001b[31;1mx\u001b[0m | \u001b[31;1mInstall Docker (exit code: 3)\u001b[0m | 82 sec |\n+---+---------------------------------------------------------------+----------+\n| Issue tracker: https://github.com/bitrise-io/steps-script/issues |\n| Source: https://github.com/bitrise-io/steps-script |\n+---+---------------------------------------------------------------+----------+\n\n ▼\n\n+------------------------------------------------------------------------------+\n| (4) [email protected] |\n+------------------------------------------------------------------------------+\n| id: deploy-to-bitrise-io |\n| version: 1.3.10 |\n| collection: https://github.com/bitrise-io/bitrise-steplib.git |\n| toolkit: go |\n| time: 2019-08-22T13:25:58-07:00 |\n+------------------------------------------------------------------------------+\n| |\n",
"position":11},
{
"chunk":"\n\u001b[34;1mConfigs:\u001b[0m\n- BuildURL: https://app.bi;1mSuccess\u001b[0m\nYou can find the Artifact on Bitrise, on the Build's page: https://app.bitrise.io/build/525060619ad47aa4\n| |\n+---+---------------------------------------------------------------+----------+\n| \u001b[32;1m✓\u001b[0m | \u001b[32;[email protected]\u001b[0m | 4.31 sec |\n+---+---------------------------------------------------------------+----------+\n| Update available: 1.3.10 -> 1.6.1 |\n+---+---------------------------------------------------------------+----------+\n\n\n+------------------------------------------------------------------------------+\n| bitrise summary |\n+---+---------------------------------------------------------------+----------+\n| | title | time (s) |\n+---+---------------------------------------------------------------+----------+\n| \u001b[32;1m✓\u001b[0m | \u001b[32;[email protected]\u001b[0m | 1.92 sec |\n+---+---------------------------------------------------------------+----------+\n| Update available: 3.1.1 -> 4.0.3 |\n+---+---------------------------------------------------------------+----------+\n| \u001b[32;1m✓\u001b[0m | \u001b[32;[email protected]\u001b[0m | 6.70 sec |\n+---+---------------------------------------------------------------+----------+\n| Update available: 4.0.5 -> 4.0.15 |\n+---+---------------------------------------------------------------+----------+\n| \u001b[32;1m✓\u001b[0m | \u001b[32;[email protected]\u001b[0m | 2.27 sec |\n+---+---------------------------------------------------------------+----------+\n| \u001b[31;1mx\u001b[0m | \u001b[31;1mInstall Docker (exit code: 3)\u001b[0m | 82 sec |\n+---+---------------------------------------------------------------+----------+\n| Issue tracker: https://github.com/bitrise-io/steps-script/issues |\n| Source: https://github.com/bitrise-io/steps-script |\n+---+---------------------------------------------------------------+----------+\n| \u001b[32;1m✓\u001b[0m | \u001b[32;[email protected]\u001b[0m | 4.31 sec |\n+---+---------------------------------------------------------------+----------+\n| Update available: 1.3.10 -> 1.6.1 |\n+---+---------------------------------------------------------------+----------+\n| Total runtime: 97 sec |\n+------------------------------------------------------------------------------+\n\n\u001b[34;1m\u001b[0m\n\u001b[34;1mSubmitting anonymized usage informations...\u001b[0m\n\u001b[34;1mFor more information visit:\u001b[0m\n\u001b[34;1mhttps://github.com/bitrise-io/bitrise-plugins-analytics/blob/master/README.md\u001b[0m\n",
"position":12},
{
"chunk":"exit status 1",
"position":14
}
],
"timestamp":null
}
Loading

0 comments on commit bee583d

Please sign in to comment.