From d85e8956c4e87913ae1866b56715baaa8f5f3d9a Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Wed, 26 Jul 2023 10:54:02 -0600 Subject: [PATCH] Add new GitHub Actions test and build steps --- .github/workflows/release.yml | 61 ++++++++++++++++++++++++++++ .github/workflows/test-and-build.yml | 59 +++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/test-and-build.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..32a0e396 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,61 @@ +name: "Build and release container image" + +on: + push: + branches: + - "main" + release: + types: + - "published" + + +# Default to bash in login mode for all steps; key to activating conda +# environment! +# https://github.com/mamba-org/provision-with-micromamba#IMPORTANT +defaults: + run: + shell: "bash -l {0}" + + + +jobs: + + build-and-release-image: + name: "Build and release container image" + runs-on: "ubuntu-latest" + needs: ["test"] + env: + IMAGE_NAME: "nsidc/usaon-vta-survey" + # GitHub Actions expressions don't have great conditional support, so + # writing a ternary expression looks a lot like bash. In Python, this + # would read as: + # github.release.tag_name if github.event_name == 'release' else 'latest' + # https://docs.github.com/en/actions/learn-github-actions/expressions + IMAGE_TAG: "${{ github.event_name == 'release' && github.release.tag_name || 'latest' }}" + steps: + - name: "Check out repository" + uses: "actions/checkout@v3" + + - name: "Build container image" + run: | + docker build -t "${IMAGE_NAME}:${IMAGE_TAG}" . + + - name: "DockerHub login" + uses: "docker/login-action@v2" + with: + username: "${{secrets.DOCKER_USER}}" + password: "${{secrets.DOCKER_PASS}}" + + - name: "GHCR login" + uses: "docker/login-action@v2" + with: + registry: "ghcr.io" + username: "${{ github.repository_owner }}" + password: "${{ secrets.GITHUB_TOKEN }}" + + - name: "Push to DockerHub and GHCR" + run: | + docker push "${IMAGE_NAME}:${IMAGE_TAG}" + + docker tag "${IMAGE_NAME}:${IMAGE_TAG}" "ghcr.io/${IMAGE_NAME}:${IMAGE_TAG}" + docker push "ghcr.io/${IMAGE_NAME}:${IMAGE_TAG}" diff --git a/.github/workflows/test-and-build.yml b/.github/workflows/test-and-build.yml new file mode 100644 index 00000000..8451afd3 --- /dev/null +++ b/.github/workflows/test-and-build.yml @@ -0,0 +1,59 @@ +name: "Test" + +on: + # TODO: We currently get double workflows when pushing to pull request + # branches. Set this to only run on pushes to `main` and pull requests + # instead? + push: + branches: + - "main" + tags: + - "v[0-9]+.[0-9]+.[0-9]+*" + pull_request: + + +# Default to bash in login mode; key to activating conda environment +# https://github.com/mamba-org/provision-with-micromamba#IMPORTANT +defaults: + run: + shell: "bash -l {0}" + + +jobs: + test: + name: "Run tests" + runs-on: "ubuntu-latest" + steps: + - name: "Check out repository" + uses: "actions/checkout@v3" + + - name: "Install Conda environment" + uses: "mamba-org/setup-micromamba@v1" + with: + environment-file: "conda-lock.yml" + # When using a lock-file, we have to set an environment name. + environment-name: "usaon-vta-survey-ci" + cache-environment: true + # Increase this key to trigger cache invalidation + cache-environment-key: 0 + + - name: "Run pre-commit checks" + run: "pre-commit run --all-files --show-diff-on-failure --color always" + + - name: "Run tests" + run: "inv test" + + + # TODO: Consider extracting everything below this line to a separate workflow + # which is triggered by pushes to the default branch and GitHub releases. + test-build: + name: "Run test-build of container image" + runs-on: "ubuntu-latest" + needs: ["test"] + steps: + - name: "Check out repository" + uses: "actions/checkout@v3" + + - name: "Test-build container image" + run: | + docker build .