stages:
- deploy
- deploy-to-aur

deploy-to-github:
  stage: deploy
  image: iffregistry.fz-juelich.de/docker-images/gr-build-images/deploy
  variables:
    GIT_STRATEGY: none
  only:
  - master@heimbach/shallow-appify
  - develop@heimbach/shallow-appify
  - tags@heimbach/shallow-appify
  script:
  - mkdir --mode=700 ~/.ssh/
  - (umask 0377 && echo "${GITHUB_DEPLOY_KEY}" > ~/.ssh/id_rsa
                && echo "github.com ${GITHUB_HOST_KEY}" >> ~/.ssh/known_hosts)
  - git clone --mirror "${CI_REPOSITORY_URL}" "${CI_PROJECT_NAME}_mirror"
  - cd "${CI_PROJECT_NAME}_mirror";
    git push --mirror "git@github.com:IngoHeimbach/${CI_PROJECT_NAME}.git";
    cd ..

deploy-to-pypi:
  stage: deploy
  image: python:3-slim
  only:
  - tags@heimbach/shallow-appify
  script:
  - pip install twine
  - echo "[distutils]"                    > ~/.pypirc;
    echo "index-servers ="               >> ~/.pypirc;
    echo "    pypi"                      >> ~/.pypirc;
    echo "[pypi]"                        >> ~/.pypirc;
    echo "username = __token__"          >> ~/.pypirc;
    echo "password = ${PYPI_DEPLOY_KEY}" >> ~/.pypirc;
  - python setup.py sdist
  - python setup.py bdist_wheel --universal
  - twine upload dist/*

deploy-to-aur:
  stage: deploy-to-aur
  image: iffregistry.fz-juelich.de/docker-images/gr-build-images/$CI_JOB_NAME
  only:
  - tags@heimbach/shallow-appify
  script:
  - mkdir --mode=700 ~/.ssh/
  - (umask 0377 && echo "$AUR_PRIVATE_KEY" > ~/.ssh/id_rsa
                && echo "aur.archlinux.org $AUR_HOST_KEY" >> ~/.ssh/known_hosts)
  - git clone "ssh://aur@aur.archlinux.org/shallow-appify.git" "shallow-appify-aur"
  - cd "shallow-appify-aur"
  - sed -i -e "/^pkgver=/c\pkgver=\"${CI_COMMIT_TAG#v}\"" -e "/^pkgrel=/c\pkgrel=\"1\"" PKGBUILD
  - (
      source PKGBUILD;
      curl -o source -L "${source[0]}";
      SHA256SUM="$(sha256sum source | awk '{ print $1 }')";
      sed -i "/^sha256sums=/c\sha256sums=(\"${SHA256SUM}\")" PKGBUILD;
    )
  - makepkg --printsrcinfo > .SRCINFO
  - git commit -a -m "Update to version ${CI_COMMIT_TAG#v}"
  - git push

