Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
PilnyTomas committed Mar 7, 2024
1 parent ba97fa4 commit 5c86280
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/run_py.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Run Generator in Docker
on:
push:
branches:
- main

jobs:
run_script:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: docker/setup-buildx-action@v3
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
NO_CACHE_FILTER=run
- name: Run Generator in Docker
run: |
docker run
- name: Commit files
id: commit
run: |
git config --local user.email "[email protected]"
git config --local user.name "github-actions"
git add text.txt file.pdf
if [ -z "$(git status --porcelain)" ]; then
echo "push=false" >> $GITHUB_ENV
else
git commit -m "Add generated files" -a
echo "push=true" >> $GITHUB_ENV
fi
shell: bash

- name: Push changes
if: steps.commit.outputs.push == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Stage 1: Prepare the environment and install dependencies
FROM ubuntu:20.04 AS prepare

# Avoid stuck build due to user prompt
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install --no-install-recommends -y python3.9 python3.9-dev python3.9-venv python3-pip python3-wheel build-essential texlive-full texlive-lang-czechslovak texlive-fonts-recommended && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Stage 2: Copy necessary files and run the script
FROM prepare AS run

# Invalidate cache to force run the following code
#ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache

# Copy the necessarry files - the script, resources and orgonit
COPY script.py /

# Run the sript to generate the text outputs
ENTRYPOINT ["python", "./script.py"]

# Copy the generated files out of the container
# this is done in build
#COPY /sell_text.txt .
#COPY /galter_print.pdf .
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
# DockerCI
MY personal experiments with Docker in GitHub Action

I'm a Docker noob, so maybe this is a well known trick, but it was impossible for me to find. Hopefull it will be usefull to others.
I found tons of scenarios where they need only to run the Docker in GitHub action, but no files were generated.
I found tons of scenarios where they committed files from GitHub action, but no Docker was used.
I found tons of scenarios where they pushed commits from Docker, but it wasn't run as GitHub action.
I found tons of scenarios where they run remote Docker image, but not the local one generated by GitHub Action.
No, ChatGPT doesn't know the answer, it never does.
A hint towards my gole found here https://aschmelyun.com/blog/using-docker-run-inside-of-github-actions/

What I want:
1. User pushes any modification (expected is `data.json` or perhap update to the `script.py`)
2. User reads/downloads files generated by the script.

Why I need all this:
A little background explaining why need this strange artistry when a different task could be done differently.
1. The user is making the modifications on phone - a computer to run the script (which would be a ton easier, yes, I realize that!) is not available.
2. Installing python intepretter + the packages on the phone is also not desirable. I know it is possible, I just dont want it - it all eats too much disc space AND simple checking out on phone is also not possible via the GitHub app - the user would have to manually download the repo and unzip it to run the script, no thanks!
3. Running the script directly by the GiuHub action without a Docker - done that, works ok, but slow.

How is it gonna work:
From the user point of view
1. In the GitHub app edit `data.json` and commit it
2. Wait few seconds
3. Read/download or otherwise use the generated files.

From the Github point of view
1. On push run action which gets cached Docker image (built earlier)
2. In the docker update files from the repo (`COPY`)
3. Run the script to generate the files.
4. Hand over the new file to the GitHub action
5. GitHub action commits and pushes the new output files via the GitHubActions profile


14 changes: 14 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# action.yml
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
time: # id of output
description: 'The time we greeted you'
runs:
using: 'docker'
image: 'Dockerfile'
8 changes: 8 additions & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"my_json_data":
{
"A": "Foo",
"B": "bar",
"my_list": ["apple", "banana", "lemon"]
}
}
58 changes: 58 additions & 0 deletions script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import sys
import os
import json
import re
import unicodedata
import subprocess

data_path="data.json"
pdf_file_name="file.pdf"
latex_file_name="latex.tex"
text_file_name="text.txt"

def latex_create_pdf(text, pdf_file_name):
# Generate LaTeX code with added packages
latex_content = (
"\\documentclass{article}\n"
"\\usepackage[T1]{fontenc}\n"
"\\usepackage[utf8]{inputenc}\n"
"\\usepackage{lmodern}\n"
"\\begin{document}\n"
"\\begin{verbatim}\n"
)

# Escape special characters
escaped_text = re.sub(r'([#$%&_{}])', r'\\\1', text)

latex_content += escaped_text

latex_content += "\\end{verbatim}"
latex_content += "\\end{document}"

# Write LaTeX content to a file
with open(latex_file_name, "wb") as f:
f.write(latex_content.encode('utf-8'))

# Get the directory of the current script
script_dir = os.path.dirname(os.path.realpath(__file__))

# Specify the name of the PDF file
pdf_file_name = os.path.join(script_dir, pdf_file_name)

try:
subprocess.run(['pdflatex', latex_file_name], check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e}")

if __name__ == "__main__":

with open(data_path, 'r', encoding='utf-8') as file:
json_data = json.load(file)

json_formatted_str = json.dumps(json_data, indent=2)

with open(text_file_name, "w") as file:
file.write(json_formatted_str)

# Generate the PDF file
latex_create_pdf(json_formatted_str, pdf_file_name) #LaTeX

0 comments on commit 5c86280

Please sign in to comment.