Skip to main content

Latex Build

Build using Latex-Builder

After changing into the directory with main.tex (and possibly bibliography.bib):

rm upload.zip 2> /dev/null && zip upload.zip * && curl -fsSL https://lb.nima-dev.com/sync/upload/main.docx -F 'file=@upload.zip' -o main.docx

We can also have conditional execution of local build if the remote one didn't work:

rm upload.zip 2> /dev/null && zip upload.zip *
curl -fsSL https://lb.nima-dev.com/sync/upload/main.docx -F 'file=@upload.zip' -o main.docx
# if last command (cURL) returned zero (returns 22 on bad input error)
if [ 0 -eq $? ]; then
  echo "sucess"
else
  echo "failed"
fi;

Build Latex Script

To build Latex, all we need to do is create a workflow for the latex build to release PDF on tags. The build script can be something like the following. It can be placed in the .github/build.sh or the .travis/build.sh path.

#! /bin/bash

build_latex()
{
    FOLDER=$1
    FILE_PATH=$2
    LATEX_DOCKER="nimamahmoudi/latex-parser"
    echo "Building the latex file: $FOLDER / $FILE_PATH"
    echo "Docker: $LATEX_DOCKER"
    docker run -t --rm --mount src=$TRAVIS_BUILD_DIR/,target=/repo,type=bind $LATEX_DOCKER /bin/bash -c "cd /repo/$FOLDER && latexmk -f -interaction=nonstopmode -pdf $FILE_PATH"
    mkdir ${FOLDER}/output
    mv ${FOLDER}/${FILE_PATH}.pdf ${FOLDER}/output/${FILE_PATH}.pdf
}

build_docx()
{
    FOLDER=$1
    FILE_PATH=$2
    BIB_FILE=$3
    CSL_FILE_NAME=$4
    LATEX_DOCKER="nimamahmoudi/latex-parser"
    CSL_URL="https://raw.githubusercontent.com/nimamahmoudi/latex-styles/master/"$CSL_FILE_NAME
    curl -L $CSL_URL > $FOLDER/$CSL_FILE_NAME
    echo "Building the latex file: $FOLDER / $FILE_PATH"
    echo "Docker: $LATEX_DOCKER"
    docker run -t --rm --mount src=$TRAVIS_BUILD_DIR/,target=/repo,type=bind $LATEX_DOCKER /bin/bash -c "cd /repo/$FOLDER && pandoc $FILE_PATH.tex -f latex --bibliography=$BIB_FILE --csl=$CSL_FILE_NAME -t docx -o ${FILE_PATH}.docx"
    mkdir ${FOLDER}/output
    mv ${FOLDER}/${FILE_PATH}.docx ${FOLDER}/output/${FILE_PATH}.docx
}

build_dummy()
{
    FOLDER=$1
    FILE_PATH=$2
    echo "Building the latex file: $FOLDER / $FILE_PATH"
    mkdir ${FOLDER}/output
    touch ${FOLDER}/output/${FILE_PATH}.pdf
}


# For Github Workflows:
export TRAVIS_BUILD_DIR=$(pwd)
# Copy everything to output folder
mkdir output

build_latex cv-short main
cp cv-short/output/main.pdf output/cv-short.pdf

build_latex cv-full main
cp cv-full/output/main.pdf output/cv-full.pdf

# For GitHub Workflows (since it cannot release more than one file now):
zip -r --junk-paths output.zip output/*

To use the script for CI/CD, we can just set up the Travis-CI or GitHub to use it.

Build Latex in Travis-CI

First, use the following for .travis.yml:

os: linux
sudo: required
dist: bionic
# docker is for latex use only
services: docker
script:
- source .travis/build.sh
after_success:
- ls
- git add output.docx
- git add output.pdf
- git add main.pdf
before_deploy:
# Set up git user name and tag this commit
- git config --local user.name "Nima Mahmoudi"
- git config --local user.email "nima_mahmoudi@live.com"
- export TRAVIS_TAG=${TRAVIS_TAG:-$(date +'%Y%m%d%H%M%S')-$(git log --format=%h -1)}
- git tag $TRAVIS_TAG

Then, we need to setup the release deployment. First, cd into the git directory, then do the following:

# Setup the releases in travis for uploading the file back
travis setup releases --com

Now all is left to do is add skip_cleanup: true to deploy since we want to be able to upload the files.

Build Latex in GitHub Workflow

We can do this by just copying the following onto the path .github/workflows/texworkflow.yml:

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
    - '*' # Push events to matching v*, i.e. v1.0, v20.15.10


name: Upload Release Asset

jobs:
  build:
    name: Upload Release Asset
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@master
      - name: Build project # This would actually build your project
        run: |
          source .github/build.sh
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1.0.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false
      - name: Upload Release Asset
        id: upload-release-asset 
        uses: actions/upload-release-asset@v1.0.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
          asset_path: ./output.zip
          asset_name: output.zip
          asset_content_type: application/zip