Repository: nansencenter/nansat (language: python)

Issue:
Migrate CI to Github Actions
Travis CI has changed its pricing policy; it is no longer free for open-source repositories (not without negotiating a quota).
We are migrating our CI to Github Actions.


Discussion / hints:

Fix patch:
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index b7484fbe5..000000000
--- a/.travis.yml
+++ /dev/null
@@ -1,94 +0,0 @@
----
-language: python
-python:
-  - '3.7'
-services:
-  - docker
-
-# Environment variables defined as part of the Travis CI repository configuration are:
-# - DOCKER_ORG: the docker hub organization (or user) to which the image will be pushed
-# - DOCKER_USER: the docker hub user used to log in to the docker hub
-# - DOCKER_PASS: the password of this user
-# - PYPI_TOKEN: the token associated to the pypi account for deployment. More information can be found here: https://pypi.org/help/#apitoken
-
-jobs:
-  include:
-    # Unit tests are run
-    - stage: 'Unit tests'
-      if: type = push
-      env:
-        - BASE_IMAGE="${DOCKER_ORG}/nansat_base:latest-slim"
-      install:
-        - docker pull "${BASE_IMAGE}"
-      # The COVERALLS_REPO_TOKEN environment variable is defined in the Travis CI repository settings:
-      # https://travis-ci.org/nansencenter/nansat/settings
-      script:
-        - >
-          docker run --rm
-          -v "$(pwd):/src"
-          -e "COVERALLS_REPO_TOKEN=$COVERALLS_REPO_TOKEN"
-          -e "TRAVIS=true"
-          -e "TRAVIS_JOB_ID=$TRAVIS_JOB_ID"
-          -e "TRAVIS_BRANCH=$TRAVIS_BRANCH"
-          -e "TRAVIS_PULL_REQUEST=$TRAVIS_PULL_REQUEST"
-          "${BASE_IMAGE}"
-          bash -c "apt update && apt install -y g++ &&
-          python setup.py sdist &&
-          coverage run --omit=nansat/mappers/*,nansat/tests/*,nansat/nansatmap.py --source=nansat setup.py test && coveralls"
-      deploy:
-        - provider: pypi
-          user: "__token__"
-          password: "${PYPI_TOKEN}"
-          skip_existing: true
-          on:
-            tags: true
-
-    - stage: 'Build docker images'
-      name: 'Build standard image'
-      if: type = pull_request OR tag IS present
-      env:
-        - IMAGE_NAME="${DOCKER_ORG}/nansat"
-        - BASE_IMAGE="${DOCKER_ORG}/nansat_base:latest"
-        - DOCKER_TMP_TAG='tmp'
-      install:
-        - docker pull "${IMAGE_NAME}" || true
-      script:
-        - >
-          docker build .
-          --cache-from "${IMAGE_NAME}"
-          --build-arg "BASE_IMAGE=${BASE_IMAGE}"
-          --build-arg "NANSAT_VERSION=${TRAVIS_TAG}"
-          -t "${IMAGE_NAME}:${DOCKER_TMP_TAG}"
-        - docker run --rm "${IMAGE_NAME}:${DOCKER_TMP_TAG}" python -c 'import nansat'
-      before_deploy:
-        - docker login -u "${DOCKER_USER}" -p "${DOCKER_PASS}"
-      deploy:
-        - provider: script
-          on:
-            tags: true
-          script: /bin/bash scripts/docker_push.sh "${TRAVIS_TAG}" latest
-
-    - name: 'Build slim image'
-      if: type = pull_request OR tag IS present
-      env:
-        - IMAGE_NAME="${DOCKER_ORG}/nansat"
-        - BASE_IMAGE="${DOCKER_ORG}/nansat_base:latest-slim"
-        - DOCKER_TAG_SUFFIX='-slim'
-        - DOCKER_TMP_TAG='tmp'
-      install:
-        - docker pull "${IMAGE_NAME}:latest${DOCKER_TAG_SUFFIX}" || true
-      script:
-        - >
-          docker build .
-          --cache-from "${IMAGE_NAME}:latest${DOCKER_TAG_SUFFIX}"
-          --build-arg "BASE_IMAGE=${BASE_IMAGE}"
-          -t "${IMAGE_NAME}:${DOCKER_TMP_TAG}"
-        - docker run --rm "${IMAGE_NAME}:${DOCKER_TMP_TAG}" python -c 'import nansat'
-      before_deploy:
-        - docker login -u "${DOCKER_USER}" -p "${DOCKER_PASS}"
-      deploy:
-        - provider: script
-          on:
-            tags: true
-          script: /bin/bash scripts/docker_push.sh "${TRAVIS_TAG}${DOCKER_TAG_SUFFIX}" "latest${DOCKER_TAG_SUFFIX}"
-...
diff --git a/Dockerfile b/Dockerfile
index 002bab568..9a4a46b6c 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,6 +3,8 @@ FROM ${BASE_IMAGE}
 # Necessary to access the BASE_IMAGE variable during the build
 ARG BASE_IMAGE
 
+ARG NANSAT_RELEASE
+
 COPY utilities /tmp/utilities
 COPY nansat /tmp/nansat
 COPY setup.py /tmp/
diff --git a/scripts/docker_push.sh b/scripts/docker_push.sh
deleted file mode 100644
index 48dcd9d1f..000000000
--- a/scripts/docker_push.sh
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/bin/bash
-
-for tag in $*;do
-    echo "Tag ${IMAGE_NAME}:${DOCKER_TMP_TAG} as ${IMAGE_NAME}:${tag}"
-    docker tag "${IMAGE_NAME}:${DOCKER_TMP_TAG}" "${IMAGE_NAME}:${tag}"
-    echo "Push ${IMAGE_NAME}:${tag}"
-    docker push "${IMAGE_NAME}:${tag}"
-done
\ No newline at end of file
diff --git a/setup.py b/setup.py
index 4b3c9d30f..7e437c271 100644
--- a/setup.py
+++ b/setup.py
@@ -45,11 +45,8 @@
 AUTHOR              = ("Anton Korosov, Morten W. Hansen, Kunt-Frode Dagestad, Aleksander Vines, Asuka Yamakawa, Artem Moiseev, Mohamed Babiker")
 AUTHOR_EMAIL        = "nansat-dev@googlegroups.com"
 PLATFORMS           = ["Linux", "OS X", "Windows"]
-MAJOR               = 1
-MINOR               = 4
-MICRO               = 5
 ISRELEASED          = True
-VERSION             = '%d.%d.%d' % (MAJOR, MINOR, MICRO) # Remember to remove "dev" when releasing
+VERSION             = os.getenv('NANSAT_RELEASE', '0.0.0dev').lstrip('v')
 REQS                = [
                         "Pillow",
                         "pythesint",


Test patch:
diff --git a/.github/workflows/tests_build.yml b/.github/workflows/tests_build.yml
new file mode 100644
index 000000000..30fd528eb
--- /dev/null
+++ b/.github/workflows/tests_build.yml
@@ -0,0 +1,188 @@
+---
+name: "Unit tests and build"
+on:
+  push:
+    branches: ['**']
+  release:
+    types: [released]
+env:
+  IMAGE_NAME: "${{ secrets.DOCKER_ORG }}/nansat"
+  BASE_IMAGE_NAME: "${{ secrets.DOCKER_ORG }}/nansat_base"
+  BASE_STANDARD_IMAGE_TAG: '2.0.3'
+  BASE_SLIM_IMAGE_TAG: '2.0.3-slim'
+jobs:
+  tests:
+    name: 'Run unit tests'
+    runs-on: 'ubuntu-20.04'
+    steps:
+      - name: 'Checkout repository'
+        uses: actions/checkout@v2
+
+      - name: Login to DockerHub
+        uses: docker/login-action@v1
+        with:
+          username: ${{ secrets.DOCKER_USER }}
+          password: ${{ secrets.DOCKER_PASS }}
+
+      - name: 'Run tests'
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+        run: >
+             docker run --rm
+             -v "$(pwd):/src"
+             -e "GITHUB_ACTIONS=$GITHUB_ACTIONS"
+             -e "GITHUB_REF=$GITHUB_REF"
+             -e "GITHUB_SHA=$GITHUB_SHA"
+             -e "GITHUB_HEAD_REF=$GITHUB_HEAD_REF"
+             -e "GITHUB_REPOSITORY=$GITHUB_REPOSITORY"
+             -e "GITHUB_RUN_ID=$GITHUB_RUN_ID"
+             -e "GITHUB_TOKEN=$GITHUB_TOKEN"
+             "${BASE_IMAGE_NAME}:${BASE_SLIM_IMAGE_TAG}"
+             bash -c "
+             apt update && apt install -y g++ &&
+             python setup.py sdist &&
+             coverage run --omit=nansat/mappers/*,nansat/tests/*,nansat/nansatmap.py --source=nansat setup.py test &&
+             coveralls"
+
+
+  publish_python_package:
+    name: Publish the Python package to PyPI
+    runs-on: 'ubuntu-20.04'
+    needs: 'tests'
+    if: github.event_name == 'release'
+    steps:
+      - name: 'Checkout repository'
+        uses: actions/checkout@v2
+
+      - name: Set up Python 3.7
+        uses: actions/setup-python@v1
+        with:
+          python-version: 3.7
+
+      - name: Build package
+        run: >
+             NANSAT_RELEASE="${GITHUB_REF#refs/tags/}"
+             python setup.py sdist
+        shell: bash
+
+      - name: Publish
+        uses: pypa/gh-action-pypi-publish@release/v1
+        with:
+          # Storing the PyPI URL in the repositories' secrets makes
+          # publishing to the test PyPI from forks easy
+          repository_url: ${{ secrets.PYPI_REPOSITORY_URL }}
+          password: ${{ secrets.PYPI_TOKEN }}
+
+
+  build_standard_docker_image:
+    runs-on: 'ubuntu-20.04'
+    needs: 'tests'
+    steps:
+      - name: 'Checkout repository'
+        uses: actions/checkout@v2
+
+      - name: "Extract tag name"
+        id: get_version
+        run: |
+          if [[ $GITHUB_REF == refs/tags/* ]];then
+            TAG="${GITHUB_REF#refs/tags/}"
+          else
+            TAG='tmp'
+          fi
+          echo "::set-output name=VERSION::${TAG}"
+
+      - name: Set up Docker Buildx
+        uses: docker/setup-buildx-action@v1
+
+      - name: Cache Docker layers
+        uses: actions/cache@v2
+        with:
+          path: /tmp/.buildx-cache
+          key: ${{ runner.os }}-buildx-standard-${{ github.sha }}
+          restore-keys: |
+            ${{ runner.os }}-buildx-standard-
+
+      - name: Login to DockerHub
+        uses: docker/login-action@v1
+        with:
+          username: ${{ secrets.DOCKER_USER }}
+          password: ${{ secrets.DOCKER_PASS }}
+
+      - name: Build docker image
+        uses: docker/build-push-action@v2
+        with:
+          context: .
+          build-args: |
+            BASE_IMAGE=${{ env.BASE_IMAGE_NAME }}:${{ env.BASE_STANDARD_IMAGE_TAG }}
+            NANSAT_RELEASE=${{ steps.get_version.outputs.VERSION }}
+          push: ${{ startsWith(github.ref, 'refs/tags/') }}
+          tags: |
+            ${{ env.IMAGE_NAME }}:latest
+            ${{ env.IMAGE_NAME }}:${{ steps.get_version.outputs.VERSION }}
+          cache-from: type=local,src=/tmp/.buildx-cache
+          cache-to: type=local,dest=/tmp/.buildx-cache-new
+
+        # Temp fix
+        # https://github.com/docker/build-push-action/issues/252
+        # https://github.com/moby/buildkit/issues/1896
+      - name: Move cache
+        run: |
+          rm -rf /tmp/.buildx-cache
+          mv /tmp/.buildx-cache-new /tmp/.buildx-cache
+
+  build_slim_docker_image:
+    runs-on: 'ubuntu-20.04'
+    needs: 'tests'
+    steps:
+      - name: 'Checkout repository'
+        uses: actions/checkout@v2
+
+      - name: "Extract tag name"
+        id: get_version
+        run: |
+          if [[ $GITHUB_REF == refs/tags/* ]];then
+            TAG="${GITHUB_REF#refs/tags/}"
+          else
+            TAG='tmp'
+          fi
+          echo "::set-output name=VERSION::${TAG}"
+
+      - name: Set up Docker Buildx
+        uses: docker/setup-buildx-action@v1
+
+      - name: Cache Docker layers
+        uses: actions/cache@v2
+        with:
+          path: /tmp/.buildx-cache
+          key: ${{ runner.os }}-buildx-slim-${{ github.sha }}
+          restore-keys: |
+            ${{ runner.os }}-buildx-slim-
+
+      - name: Login to DockerHub
+        uses: docker/login-action@v1
+        with:
+          username: ${{ secrets.DOCKER_USER }}
+          password: ${{ secrets.DOCKER_PASS }}
+
+      - name: Build docker image
+        uses: docker/build-push-action@v2
+        with:
+          context: .
+          build-args: |
+            BASE_IMAGE=${{ env.BASE_IMAGE_NAME }}:${{ env.BASE_SLIM_IMAGE_TAG }}
+            NANSAT_RELEASE=${{ steps.get_version.outputs.VERSION }}
+          push: ${{ startsWith(github.ref, 'refs/tags/') }}
+          tags: |
+            ${{ env.IMAGE_NAME }}:latest-slim
+            ${{ env.IMAGE_NAME }}:${{ steps.get_version.outputs.VERSION }}-slim
+          cache-from: type=local,src=/tmp/.buildx-cache
+          cache-to: type=local,dest=/tmp/.buildx-cache-new
+
+        # Temp fix
+        # https://github.com/docker/build-push-action/issues/252
+        # https://github.com/moby/buildkit/issues/1896
+      - name: Move cache
+        run: |
+          rm -rf /tmp/.buildx-cache
+          mv /tmp/.buildx-cache-new /tmp/.buildx-cache
+...
\ No newline at end of file