Skip to content

Commit

Permalink
Docker tags update - 1
Browse files Browse the repository at this point in the history
Wrote a Python script to replace the tags in the Dockerfiles. The script accepts two command-line arguments - file path where replacement to be made and the latest image tag.

Added a job in the YAML file to run this python script to replace the tags in the Dockerfiles.
Additionally, it also contains steps to automatically add, commit, push changes to the files in the repo.
  • Loading branch information
Mahadik, Mukul Chandrakant authored and Mahadik, Mukul Chandrakant committed Apr 27, 2024
1 parent 29e9e0c commit fd014cc
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
53 changes: 53 additions & 0 deletions .github/replaceTags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import re
import argparse

def generateUpdatedSuffix(current_image_name):
try:
pattern = "^(FROM\s+)(.+)(/)(.+)(:)(.+)(_)(\d{4}-\d{2}-\d{2}--\d{2}-\d{2})"
matches = re.search(pattern, current_image_name)

current_timestamp = matches.group(8)
print("Current timestamp = %s" % current_timestamp)

latest_timestamp = args.tag
print("Latest timestamp = %s" % latest_timestamp)

latest_image_name = ''.join([match for match in matches.groups()[:-1]]) + latest_timestamp + "\n"
print("Current image name = %s" % current_image_name)
print("Latest image name = %s" % latest_image_name)
return latest_image_name
except:
print("No matching pattern found in FROM layer while parsing Dockerfile to replace tags.")
return False

def updateDockerfile():
docker_file_path = rf"{args.file}"
docker_file = open(docker_file_path,"r")
current_file_content = docker_file.readlines()
docker_file.close()
updated_file_content = []

for line in current_file_content:
if line.strip().startswith("FROM ") and not line.strip().startswith("#"):
updatedTag = generateUpdatedSuffix(line)
if updatedTag is False:
updated_file_content.append(line)
else:
updated_file_content.append(updatedTag)
else:
updated_file_content.append(line)

with open(docker_file_path,"w") as docker_file:
docker_file.writelines(updated_file_content)

docker_file = open(docker_file_path,"r")
current_file_content = docker_file.readlines()
docker_file.close()

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help="Dockerfile path", required=True)
parser.add_argument("-t", "--tag", help="Latest docker image tag", required=True)
args = parser.parse_args()
print(args)
updateDockerfile()
42 changes: 39 additions & 3 deletions .github/workflows/image_build_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,48 @@ on:
env:
DOCKER_USER: ${{secrets.DOCKER_USER}}
DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
DOCKER_IMAGE_TAG: ${{ github.event.inputs.docker_image_tag }}


# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

fetch_tag:
if: ${{ github.event_name == "workflow_dispatch" }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Print input docker image tag
run: |
echo "Input latest docker image tag: ${{ env.DOCKER_IMAGE_TAG }}"
- 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 argparse
- name: Run Python script
id: run_script
run: |
echo "Running script to replace docker image tags in Dockerfiles"
python .github/replaceTags.py --file=docker/Dockerfile
python .github/replaceTags.py --file=Dockerfile
- name: Add, Commit, Push changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add docker/Dockerfile Dockerfile
git commit -m "Updated docker image tags in Dockerfiles to the latest timestamp: ${{ env.DOCKER_IMAGE_TAG }}"
git push origin
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
Expand Down Expand Up @@ -57,6 +96,3 @@ jobs:
# run: |
# docker push $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }}

- name: Print input docker image tag
run: |
echo "Input latest docker image tag: ${{ github.event.inputs.docker_image_tag }}"

0 comments on commit fd014cc

Please sign in to comment.