nunop
nunop

Reputation: 2207

Failing Compile Automation with Pandoc and Travis

I am trying to automate compilation of Markdown with Pandoc and Travis through Github (while publishing the resulting HTML and Pdf files on Github Pages) without much success.

Followed this guide (which attempts the same but with Asciidoc and Asciidoctor). Using Docker image jagregory/pandoc (currently uses Pandoc's version 1.19.2.1) and adapted the script lines on the Yaml file. Tried several scripts and until far had little if no success. As I write my Yaml file is as follows:

sudo: required

services:
  - docker                  

before_install:            
  - mkdir -p output
  - docker pull jagregory/pandoc

script:
  - docker run -v $TRAVIS_BUILD_DIR:/documents/ --name pandoc-to-html jagregory/pandoc -v --standalone --toc --filter=pandoc-citeproc --data-dir=/documents/output body.md -o uwa.html
  - docker run -v $TRAVIS_BUILD_DIR:/documents/ --name pandoc-to-pdf jagregory/pandoc --standalone  --latex-engine=xelatex --filter=pandoc-citeproc --data-dir=/documents/output body.md -o uwa.pdf

after_error: 
  - docker logs pandoc-to-html
  - docker logs pandoc-to-pdf

after_failure:
  - docker logs pandoc-to-html
  - docker logs pandoc-to-pdf

after_success:      
  - cd output ; mv README.html index.html ; cp -R ../images images
  - git init
  - git config user.name "${GH_USER_NAME}"
  - git config user.email "{GH_USER_EMAIL}"
  - git add . ; git commit -m "Deploy to GitHub Pages"
  - git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:gh-pages > /dev/null 2>&1

I get the following on the command line:

enter image description here

It obviously doesn't find the file body.md and I am sure the issue is pretty simple but I cannot figure out what I am doing wrong.

Upvotes: 1

Views: 137

Answers (1)

nunop
nunop

Reputation: 2207

I eventually got a passed build in Travis thanks to @tarleb suggestions above. After tweaking around with the script, I also managed to push to Github.

Here's my current .travis.yml configuration:

sudo: required

services:
  - docker                  

before_install:            
  - mkdir -p output
  - docker pull jagregory/pandoc

script:
  - OUT=output; for FILE in *.md; do NAME=$(printf '%s\n' "$FILE" | cut -f 1 -d '.'); if [[ "$NAME" =~ ^readme|README$ ]]; then [[ "$NAME" =~ ^readme$ ]] && RDME=readme; [[ "$NAME" =~ ^README$ ]] && RDME=README; echo -e "\\e[34mThere's a \\e[33m$RDME \\e[34mfile. Good."; fi; echo -e "\\e[34mStart compiling..."; if ! [[ "$NAME" =~ ^readme|README$ ]]; then echo -e "\\e[34mCompiling $NAME.pdf..." && docker run -v $TRAVIS_BUILD_DIR:/source jagregory/pandoc -f markdown "$FILE" -t latex -o "$OUT/$NAME.pdf" && echo -e "\\e[34mCompiling $NAME.html..." && docker run -v $TRAVIS_BUILD_DIR:/source jagregory/pandoc -f markdown "$FILE" -t html5 -o "$OUT/$NAME.html"; fi; if [[ "$NAME" =~ ^readme|README$ ]]; then echo -e "\\e[34mCompiling $RDME.html..." && docker run -v $TRAVIS_BUILD_DIR:/source jagregory/pandoc -f markdown "$FILE" -t html5 -o $OUT/$RDME.html; fi; done; echo -e "\\e[32mDone compiling!"; if [ -f "$RDME".md ]; then echo -e "\\e[34mCopying $RDME.md to /$OUT..." && cp "$RDME".md $OUT && echo -e "\\e[32mDone copying!"; elif ! [ -f "$RDME".md ]; then echo -e "\\e[31mThere's no readme.md to copy!"; fi; if [ -f "$OUT/$RDME".html ]; then echo -e "\\e[34mRenaming $RDME.html to index.html..." && mv "$OUT/$RDME".html $OUT/index.html && echo -e "\\e[32mAll tidy!"; else echo -e "\\e[31mThere's no readme.html to rename!"; fi;

after_success:      
  - cd output
  - git init
  - git config user.name "${GH_USER_NAME}" 
  - git config user.email "${GH_USER_EMAIL}" 
  - git add .
  - git commit -m "Deploy to GitHub Pages"
  - git push -f -q "https://${GH_TOKEN}@github.com/${GH_REF}" master:gh-pages

Here's the bash script above:

OUT=output
for FILE in *.md; do
    NAME=$(printf '%s\n' "$FILE" | cut -f 1 -d '.');
    if [[ "$NAME" =~ ^readme|README$ ]]; then
        [[ "$NAME" =~ ^readme$ ]] && RDME=readme;
        [[ "$NAME" =~ ^README$ ]] && RDME=README;
    echo -e "\\e[34mThere's a \\e[33m$RDME \\e[34mfile. Good.";
    fi;
echo -e "\\e[34mStart compiling...";
if ! [[ "$NAME" =~ ^readme|README$ ]]; then
    echo -e "\\e[34mCompiling $NAME.pdf..." &&
    pandoc -f markdown "$FILE" -t latex -o "$OUT/$NAME.pdf" &&
    echo -e "\\e[34mCompiling $NAME.html..." &&
    pandoc -f markdown "$FILE" -t html5 -o "$OUT/$NAME.html";
fi;
if [[ "$NAME" =~ ^readme|README$ ]]; then
    echo -e "\\e[34mCompiling $RDME.html..." &&
    pandoc -f markdown "$FILE" -t html5 -o $OUT/$RDME.html;
fi;
done;
echo -e "\\e[32mDone compiling!";
if [ -f "$RDME".md ]; then
    echo -e "\\e[34mCopying $RDME.md to /$OUT..." &&
    cp "$RDME".md $OUT &&
    echo -e "\\e[32mDone copying!";
elif ! [ -f "$RDME".md ]; then
    echo -e "\\e[31mThere's no readme.md to copy!";
fi;
if [ -f "$OUT/$RDME".html ]; then
    echo -e "\\e[34mRenaming $RDME.html to index.html..." &&
    mv "$OUT/$RDME".html $OUT/index.html &&
    echo -e "\\e[32mAll tidy!";
else
    echo -e "\\e[31mThere's no readme.html to rename!";
fi;

Here's the job log: enter image description here

What this script basically does is it compiles every Markdown file in /source into pdf and HTML, and then moves them to /source/output. The only exception is the readme.md file, which is compiled to HTML only, renamed to index.html and copied to /source/output.

A couple of things to pay attention to is to correctly type the output path in the script block (output/foo.pdf) and the URL of the repository where to push to ("https://${GH_TOKEN}@github.com/${GH_REF}").

Last but not least, not only the source where the repository will be published from needs to be set correctly but the default branch needs to be changed too, from master to gh-pages. To do both, navigate to Settings on the repository's page, then Options-->GitHub Pages-->Source, then select gh-branch pages from the dropdown menu (the ´gh-pages´ branch needs to have been created first for this option to appear). Finally, navigate to Settings-->Branches-->Default Branch and select gh-branch from the dropdown menu again.

The environment variables also need to be set correctly in Travis.

Upvotes: 1

Related Questions