From bc82c9dd3876faa088b97beb47eefcee681035c1 Mon Sep 17 00:00:00 2001 From: SathishMani219 Date: Mon, 5 Feb 2024 12:39:29 +0000 Subject: [PATCH] push workflows and actions --- .../actions/whats-my-ip-address/action.yml | 58 ++++++++++++++++++ .github/workflows/buildimage.yml | 61 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 .github/actions/whats-my-ip-address/action.yml create mode 100644 .github/workflows/buildimage.yml diff --git a/.github/actions/whats-my-ip-address/action.yml b/.github/actions/whats-my-ip-address/action.yml new file mode 100644 index 0000000..bb65c67 --- /dev/null +++ b/.github/actions/whats-my-ip-address/action.yml @@ -0,0 +1,58 @@ +name: Whats My Ip Address +description: Gets the workflow runner ip address + +outputs: + ip: + description: The workflow runner's ip address + value: ${{ steps.whats-my-ip.outputs.ip }} + +runs: + using: composite + + steps: + - name: Get Workflow runner IP + shell: bash + id: whats-my-ip + run: | + # Resolving IP Address + + # Function for unbuffered echo + function unbuffered_echo { + stdbuf -o 0 echo "$1" + } + + # Maximum number of tries + max_tries=10 + + # Delay between tries in seconds + delay=0 + + #Curl Timeout + curl_timeout=30 + + for try in $(seq $max_tries); do + # Make a GET request to the API + response=$(curl --fail -m $curl_timeout -s https://api.ipify.org?format=json || true) + + unbuffered_echo "Response: $response" + + # Extract the IP address from the JSON response + ip=$(echo $response | jq -r '.ip') + + # If the request was successful, print the IP and exit + if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + unbuffered_echo "IP Address: $ip" + echo "ip=$ip" >> $GITHUB_OUTPUT + exit 0 + fi + + # If the request failed, wait and then try again + unbuffered_echo "Attempt $try failed, retrying in $delay seconds..." + sleep $delay + + delay=$((delay + 2)) + done + + # If we reach this point, all attempts have failed + unbuffered_echo "Failed to retrieve IP after $max_tries attempts." + exit 1 \ No newline at end of file diff --git a/.github/workflows/buildimage.yml b/.github/workflows/buildimage.yml new file mode 100644 index 0000000..c25e1e7 --- /dev/null +++ b/.github/workflows/buildimage.yml @@ -0,0 +1,61 @@ + +name: Build Image and Publish to GCR + +on: + workflow_dispatch : + # workflow_call: + inputs: + environment: + type: string + required: true + +#on: +# push: +# branches: [ "main" ] + +env: + DOCKER_IMAGE: Knowledgebase + +jobs: + + build-image: + + runs-on: ubuntu-22.04 + name: Build & Publish Image + + steps: + + - name: Checkout Repository + uses: actions/checkout@v3 + with: + ref: ${{ github.ref }} + + - name: Get GitHub Runner IP + id: whats-my-ip + uses: ./.github/actions/whats-my-ip-address + + + - name: GitHub Container Registry Login + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build Image & Publish To GCR + uses: docker/build-push-action@v4 + with: + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + registry: docker.pkg.github.com + context: ./ + file: ./src/App/Dockerfile + run: | + docker build . --tag ghcr.io/DFE-Digital/sts-knowledgebase/App:latest + docker push ghcr.io/DFE-Digital/sts-knowledgebase/App:latest + tags: | + ghcr.io/DFE-Digital/sts-knowledgebase/${{ env.DOCKER_IMAGE}}/${{github.run_number}} + push: true + + - run : + echo "${{vars.AZ_ENVIRONMENT}}"