Skip to content

Commit

Permalink
Merge pull request #15 from MukuFlash03/tags-combo-approach
Browse files Browse the repository at this point in the history
Automating image tag update for admin dashboard. Did some cleanup, but going to finalize everything in the other PR.
  • Loading branch information
nataliejschultz authored May 7, 2024
2 parents ab51504 + f1fd42c commit 9f96044
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DOCKER_IMAGE_TAG=2024-05-03--42-05
69 changes: 69 additions & 0 deletions .github/fetch_runID.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

import json
import logging
import requests
import sys

if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)

# Just checking to see if workflows are being fetched; it works!!!
# download_url = "https://api.github.com/repos/MukuFlash03/e-mission-server/actions/workflows"
# logging.debug("About to fetch workflows present in e-mission-server from %s" % download_url)
# r = requests.get(download_url)
# if r.status_code != 200:
# logging.debug(f"Unable to fetch workflows, status code: {r.status_code}")
# sys.exit(1)
# else:
# workflows_json = json.loads(r.text)
# logging.debug(f"Successfully fetched workflows")
# print(workflows_json)

# for workflow_entry in workflows_json["workflows"]:
# print("Workflow name: %s ; id: %s " % (workflow_entry["name"], workflow_entry["id"]))



'''
Workflow "docker image" uses image_build_push.yml
From above commented out code, and checked via terminal as well,
workflow id for the "docker image" can be fetched using:
https://api.github.com/repos/MukuFlash03/e-mission-server/actions/workflows
https://api.github.com/repos/e-mission/e-mission-server/actions/workflows
For MukuFlash03: id = 75506902
For e-mission-server: id = 35580278
'''
download_url = "https://api.github.com/repos/MukuFlash03/e-mission-server/actions/workflows/75506902/runs"
# TODO: Comment the above line and Uncomment the below line representing the original emission repo
# download_url = "https://api.github.com/repos/e-mission/e-mission-server/actions/workflows/35580278/runs"
logging.debug("About to fetch workflow runs present in docker image workflow present in e-mission-server from %s" % download_url)
r = requests.get(download_url)
if r.status_code != 200:
logging.debug(f"Unable to fetch workflow runs, status code: {r.status_code}")
sys.exit(1)
else:
workflow_runs_json = json.loads(r.text)
logging.debug(f"Successfully fetched workflow runs")
# print(workflow_runs_json)

workflow_runs = workflow_runs_json["workflow_runs"]
if workflow_runs:
# TODO: Change the head_branch name which identifies only e-mission-server runs triggered by this branch
successful_runs = [run for run in workflow_runs \
if run["status"] == "completed" and \
run["conclusion"] == "success" and \
run["head_branch"] == "tags-combo-approach"
]
# print(successful_runs)
if successful_runs:
sorted_runs = successful_runs.sort(reverse=True, key=lambda x: x["updated_at"])
sorted_runs = sorted(successful_runs, reverse=True, key=lambda x: x["updated_at"])
# print(sorted_runs)
latest_run_id = sorted_runs[0]["id"]
# print(latest_run_id)
print(f"::set-output name=run_id::{latest_run_id}")
# else:
# print("No successful runs")
# else:
# print("No workflow runs found")
125 changes: 121 additions & 4 deletions .github/workflows/image_build_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ on:
push:
# Mukul:
# I've added a local test branch on my system and using it for testing image push.
# So, for testing purposes, need to checkout a branch "image-push-merge"
# So, for testing purposes, need to checkout a branch "tags-combo-approach"
# TODO: Need to change to build off master or main once it looks good.
branches: [ image-push-merge ]
branches: [ tags-combo-approach ]

workflow_dispatch:
inputs:
docker_image_tag:
description: "Latest Docker image tags passed from e-mission-server repository on image build and push"
required: true

# Env variable
env:
Expand All @@ -20,15 +25,123 @@ env:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
fetch_run_id:
runs-on: ubuntu-latest

outputs:
run_id: ${{ steps.get_run_id.outputs.run_id }}

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install requests
- name: Run Python script
id: run_script
run: |
echo "Fetching latest successful run ID from e-misison-server docker image workflow"
python .github/fetch_runID.py
- name: Get Run ID
id: get_run_id
run: echo "run_id=${{ steps.run_script.outputs.run_id }}" >> "$GITHUB_OUTPUT"

fetch_tag:
needs: fetch_run_id
runs-on: ubuntu-latest

env:
RUN_ID: ${{ needs.fetch_run_id.outputs.run_id }}

outputs:
docker_image_tag: ${{ steps.get_docker_tag.outputs.docker_image_tag }}

steps:
- uses: actions/checkout@v4

- name: Use Run ID from previous fetch_run_id job
run: echo Run ID from previous job ${{ env.RUN_ID }}

- name: Download artifact
uses: actions/download-artifact@v4
with:
name: docker-image-tag
# TODO: Create a token with basic repo permissions
# TODO: Change user / organization to emission instead of MukuFlash03
github-token: ${{ secrets.GH_PAT_TAG }}
repository: MukuFlash03/e-mission-server
run-id: ${{ env.RUN_ID }}

- name: Print artifact tag
id: get_docker_tag
run: |
cat tag_file.txt
docker_image_tag=$(cat tag_file.txt)
echo $docker_image_tag
echo "docker_image_tag=$(echo $docker_image_tag)" >> $GITHUB_OUTPUT
# - name: Upload Artifact
# uses: actions/upload-artifact@v4
# with:
# name: docker-image-tag
# path: tag_file.txt
# overwrite: true

# - name: List artifact
# run: ls -R

# This workflow contains a single job called "build"
build:
needs: fetch_tag

# The type of runner that the job will run on
runs-on: ubuntu-latest

env:
DOCKER_IMAGE_TAG_1: ${{ needs.fetch_tag.outputs.docker_image_tag }}
DOCKER_IMAGE_TAG_2: ${{ github.event.inputs.docker_image_tag }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Print input docker image tag
run: |
echo "Event name: ${{ github.event_name }}"
echo "Latest docker image tag (push): ${{ env.DOCKER_IMAGE_TAG_1 }}"
echo "Latest docker image tag (workflow_dispatch): ${{ env.DOCKER_IMAGE_TAG_2 }}"
- name: Update .env file
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "Workflow_dispatch: New server image built and pushed, Updating image tag in .env"
echo "DOCKER_IMAGE_TAG=$DOCKER_IMAGE_TAG_2" > .env
else
echo "Push event: Restoring latest server image tag in .env"
echo "DOCKER_IMAGE_TAG=$DOCKER_IMAGE_TAG_1" > .env
fi
- name: Add, Commit, Push changes to .env file
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
if git diff --quiet; then
echo "Latest timestamp already present in .env file, no changes to commit"
else
git add .env
git commit -m "Updated docker image tag in .env file to the latest timestamp"
git push origin
fi
- name: docker login
run: | # log into docker hub account
docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
Expand All @@ -44,7 +157,11 @@ jobs:
# Runs a set of commands using the runners shell
- name: build docker image
run: |
docker build -t $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }} .
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
docker build --build-arg DOCKER_IMAGE_TAG=$DOCKER_IMAGE_TAG_2 -t $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }} .
else
docker build --build-arg DOCKER_IMAGE_TAG=$DOCKER_IMAGE_TAG_1 -t $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }} .
fi
docker images
- name: push docker image
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Please change once all PR changes are final, so it reads from shankari/e-mission-server
# FROM shankari/e-mission-server:master_2024-02-10--19-38
FROM mukuflash03/e-mission-server:image-push-merge_2024-04-30--59-58
ARG DOCKER_IMAGE_TAG

FROM shankari/e-mission-server:master_${DOCKER_IMAGE_TAG}

ENV DASH_DEBUG_MODE True
ENV SERVER_PORT 8050
Expand Down
2 changes: 2 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ services:
build:
context: .
dockerfile: docker/Dockerfile
args:
DOCKER_IMAGE_TAG: ${DOCKER_IMAGE_TAG}
image: e-mission/opdash:0.0.1
ports:
- "8050:8050"
Expand Down
2 changes: 2 additions & 0 deletions docker-compose-prod-nginx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
build:
context: .
dockerfile: docker/Dockerfile
args:
DOCKER_IMAGE_TAG: ${DOCKER_IMAGE_TAG}
image: e-mission/opdash:0.0.1
environment:
DASH_DEBUG_MODE: "True"
Expand Down
2 changes: 2 additions & 0 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ services:
build:
context: .
dockerfile: docker/Dockerfile
args:
DOCKER_IMAGE_TAG: ${DOCKER_IMAGE_TAG}
image: e-mission/opdash:0.0.1
ports:
- "8050:8050"
Expand Down
6 changes: 3 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Please change once all PR changes are final, so it reads from shankari/e-mission-server
# FROM shankari/e-mission-server:master_2024-02-10--19-38
FROM mukuflash03/e-mission-server:image-push-merge_2024-04-30--59-58
ARG DOCKER_IMAGE_TAG

FROM shankari/e-mission-server:master_${DOCKER_IMAGE_TAG}

ENV DASH_DEBUG_MODE True
ENV SERVER_PORT 8050
Expand Down

0 comments on commit 9f96044

Please sign in to comment.