Skip to content

Commit

Permalink
Artifact download test - 1
Browse files Browse the repository at this point in the history
Added working code from join repo to fetch docker image tags using artifact download.
  • Loading branch information
Mahadik, Mukul Chandrakant authored and Mahadik, Mukul Chandrakant committed Apr 26, 2024
1 parent f924af0 commit 237df16
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 10 deletions.
67 changes: 67 additions & 0 deletions .github/fetch_runID.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

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"
# 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:
successful_runs = [run for run in workflow_runs \
if run["status"] == "completed" and \
run["conclusion"] == "success" and \
run["head_branch"] == "tags-artifact"
]
# 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")
79 changes: 69 additions & 10 deletions .github/workflows/image_build_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ 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-artifact"
# TODO: Need to change to build off master or main once it looks good.
branches: [ image-push-merge ]
branches: [ tags-artifact ]


# Env variable
Expand All @@ -20,11 +20,46 @@ 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@v2

- 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"

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

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

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

# 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
Expand All @@ -42,11 +77,35 @@ jobs:
run: echo running in repo ${GITHUB_REPOSITORY#*/} branch ${GITHUB_REF##*/} on ${{ steps.date.outputs.date }}

# 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 }} .
docker images
- name: push docker image
run: |
docker push $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }}
# - name: build docker image
# run: |
# docker build -t $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }} .
# docker images

# - name: push docker image
# run: |
# docker push $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }}

- name: Use Run ID from previous fetch_run_id job
run: echo Run ID from previous job ${{ needs.fetch_run_id.outputs.run_id }}

- name: Download artifact
uses: actions/download-artifact@v4
with:
name: docker-image-tag
github-token: ${{ secrets.GH_PAT_TAG }}
repository: MukuFlash03/e-mission-server
run-id: ${{ env.RUN_ID }}

- name: Print artifact tag
run: cat tag_file.txt

- 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

0 comments on commit 237df16

Please sign in to comment.