Is it possible to commit artifacts into the git?

This is maybe a stupid question but I cannot solve this on my own…

I use the following .gitlab-ci.yml

compile_pdf:
  image: aergus/latex
  script:
    - latexmk -pdf test.tex
  artifacts:
    paths:
      - test.pdf

When I push a change in test.tex the runner runs successfully and I can download artifacts.zip via the web interface. However, what I would like to have is the following: the runner should deploy the generated artifact into the git. So after finishing the run, the latest pdf is checked into the git. When I pull the git later, it gives me the latest pdf…

Is this possible? If yes: how?

2 Likes

That’s not really how git is supposed to be used, but in a script we call from .gitlab-ci.yml on a runner with the shell executor in some projects we call git log, I don’t see why you wouldn’t be able to call other git commands, but you will have to look into what environment variables to set to make the commit look right (e.g. have the right author with the right email).

@drscheme, Did you ever get this to work?
I’m interested to do the same, and more advanced git operations from within a gitlab-ci pipeline.

I did not work on this any further but I learned a bit more about the ci/cd stuff since I asked.

Maybe it could work like this:

  • you install git in the container
  • you check out the repo in the container (as credentials a deploy key or something like that could be used?)
  • then you git add / commit the artifact pdf to the checked out repo

In a similar fashion I was able to build myself a deploy stage that sftps a website to a server built in the build stage. But I’ve got no idea if you can modify a repository within a build stage.

PS: In case you find a solution, let us know.

1 Like

@drscheme commit trigger CI, CI will create commit that trigger CI again…

It is endless loop

There’s a [skip ci] tag that can be added to a commit, to prevent the loop.

1 Like

Thanks for the tip! In case you are still looking for a solution, I got this to work using some help from this guide and have an example set up on my GitLab repo.

This is what my .gitlab-ci.yml looks like:

build:
  image: ctornau/latex
  stage: build
  script:
    - latexmk -pdf -pdflatex="xelatex -interaction=nonstopmode" -use-make *.tex
  artifacts:
    when: on_success
    paths:
      - ./*.pdf
    expire_in: 5 min # might not need this if deploy works


deploy:
  stage: deploy
  before_script:
    - 'which ssh-agent || ( apt-get update -qy && apt-get install openssh-client -qqy )'
    - eval `ssh-agent -s`
    - echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add - > /dev/null # add ssh ke
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_PUBLIC_KEY" >> ~/.ssh/id_rsa.pub
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  script:
    - git config --global user.email "${CI_EMAIL}"
    - git config --global user.name "${CI_USERNAME}"
    - git add -f *.pdf # Force add PDF since we .gitignored it
    - git commit -m "Compiled PDF from $CI_COMMIT_SHORT_SHA [skip ci]" || echo "No changes, nothing to commit!"
    - git remote rm origin && git remote add origin git@gitlab.com:$CI_PROJECT_PATH.git
    - git push origin HEAD:$CI_COMMIT_REF_NAME # Pushes to the same branch as the trigger
3 Likes