From b5e010ec1731b339b2aacd86392e24eb4388ae13 Mon Sep 17 00:00:00 2001 From: Akshay Gupta Date: Sat, 2 Sep 2023 00:06:42 +0530 Subject: [PATCH] Add action --- .github/workflows/main.yml | 26 +++++++++++++++++++++++ Dockerfile | 8 +++++++ action.yml | 30 ++++++++++++++++++++++++++ parse_inputs.sh | 43 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 Dockerfile create mode 100644 action.yml create mode 100755 parse_inputs.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..db5e9fa --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,26 @@ +name: Test + +on: + push: + workflow_dispatch: + inputs: + tramline-input: + required: false + +jobs: + deploy_job: + runs-on: ubuntu-latest + name: A job to test tramline deploys + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Deploy + uses: ./ # Uses the action in the root directory + id: tramline + with: + input: ${{ github.event.inputs.tramline-input }} + - name: Get the output + run: | + echo "vcode: ${{ steps.tramline.outputs.version_code }}" + echo "vname: ${{ steps.tramline.outputs.version_name }}" + echo "cref: ${{ steps.tramline.outputs.commit_ref }}" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fa0188a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +# Container image that runs your code +FROM alpine:3.10 + +# Copies your code file from your action repository to the filesystem path `/` of the container +COPY entrypoint.sh /entrypoint.sh + +# Code file to execute when the docker container starts up (`entrypoint.sh`) +ENTRYPOINT ["/entrypoint.sh"] diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..0eced29 --- /dev/null +++ b/action.yml @@ -0,0 +1,30 @@ +# action.yml +name: 'Configure Tramline' +description: 'Checkout the correct ref & parse JSON inputs' +inputs: + input: + description: "Input from Tramline as a JSON blob" + required: true + default: "{}" +outputs: + version_code: + description: 'the version code (build number) for the build' + value: ${{ steps.tramline.outputs.version_code }} + version_name: + description: 'the version name for the build' + value: ${{ steps.tramline.outputs.version_name }} + commit_ref: + description: 'the commit for which this workflow is triggered' + value: ${{ steps.tramline.outputs.commit_ref }} +runs: + using: "composite" + steps: + - name: Parse inputs from Tramline + run: ./parse_inputs.sh ${{ inputs.input }} + id: tramline + shell: bash + + - name: Checkout the repo + uses: actions/checkout@v3 + with: + ref: ${{ steps.tramline.outputs.commit_ref }} diff --git a/parse_inputs.sh b/parse_inputs.sh new file mode 100755 index 0000000..7bc3069 --- /dev/null +++ b/parse_inputs.sh @@ -0,0 +1,43 @@ +#!/bin/bash -l + +echo "Starting tramline input parsing..." + +# Install jq if it doesn't exist +if ! command -v jq &> /dev/null +then + echo "jq is not installed. Installing..." + sudo apt-get update + sudo apt-get install -y jq +else + echo "jq is already installed. Skipping..." +fi + +# Parse JSON and store keys in an array +echo $1 > tramline_input.json +keys=() +while read -r value +do + keys+=("$value") +done < <(jq -c 'keys_unsorted[]' tramline_input.json) + +# Exit if there are parse errors +if [ -z "$keys" ]; then + echo "Error parsing Tramline inputs. Exiting..." + exit 1 +fi + +# Loop through the keys and set variables +for key in "${keys[@]}"; do + # Get the value of the key from JSON + value=$(jq -r ".$key" tramline_input.json) + # Remove double quotes from key + key="${key//\"/}" + # Assign the value to a variable with a name based on the key + output="${key}=${value}" + # Output + echo $output >> "$GITHUB_OUTPUT" +done + +# Cleanup +rm -f tramline_input.json +echo "Finishing up tramline input parsing..."