From 02c7a0437338caf39f0ab536e52834cd1de2e5a7 Mon Sep 17 00:00:00 2001 From: crazyzlj Date: Wed, 29 Nov 2023 11:06:53 +0800 Subject: [PATCH] Merge github actions code from dev branch --- .github/workflows/cmake_build_gcc.yml | 54 ++++++++++++++++++++++ .github/workflows/cmake_build_ifort.yml | 59 +++++++++++++++++++++++++ .github/workflows/deploy_images.yml | 48 ++++++++++++++++++++ Dockerfile | 49 ++++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 .github/workflows/cmake_build_gcc.yml create mode 100644 .github/workflows/cmake_build_ifort.yml create mode 100644 .github/workflows/deploy_images.yml create mode 100644 Dockerfile diff --git a/.github/workflows/cmake_build_gcc.yml b/.github/workflows/cmake_build_gcc.yml new file mode 100644 index 0000000..078664f --- /dev/null +++ b/.github/workflows/cmake_build_gcc.yml @@ -0,0 +1,54 @@ +# Build SWAT using GCC on Linux and macOS + +name: Build by GCC on Linux/macOS + +on: + push: + paths: + - 'cmake/**' + - 'src/**' + - 'CMakeLists.txt' + pull_request: + paths: + - 'cmake/**' + - 'src/**' + - 'CMakeLists.txt' + workflow_dispatch: + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Debug + +jobs: + build-ubuntu-gfortran: + # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. + # You can convert this to a matrix build if you need cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: ubuntu-latest + + steps: + - name: Checkout SWAT + uses: actions/checkout@v3 + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + + - name: Build + # Build your program with the given configuration + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -- -j 4 + + build-mac-gfortran: + runs-on: macos-latest + steps: + - name: Checkout SWAT + uses: actions/checkout@v3 + + - name: Configure CMake + # https://github.com/actions/virtual-environments/blob/main/images/macos/macos-11-Readme.md#language-and-runtime + # Specify gfortran alias explicitly! + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_Fortran_COMPILER=gfortran-11 + + - name: Build + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} diff --git a/.github/workflows/cmake_build_ifort.yml b/.github/workflows/cmake_build_ifort.yml new file mode 100644 index 0000000..9480da4 --- /dev/null +++ b/.github/workflows/cmake_build_ifort.yml @@ -0,0 +1,59 @@ +# Build SWAT using ifort on Linux +# Refers to https://gist.github.com/scivision/d94bb10a01fa3b8ed0c9a93ee16318ba + +name: Build by IntelFortran on Linux + +on: + push: + paths: + - 'cmake/**' + - 'src/**' + - 'CMakeLists.txt' + pull_request: + paths: + - 'cmake/**' + - 'src/**' + - 'CMakeLists.txt' + workflow_dispatch: + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Debug + +jobs: + + linux-ubuntu-ifort: + runs-on: ubuntu-latest + env: + FC: ifort + + steps: + - name: Intel Apt repository + timeout-minutes: 1 + run: | + wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB + sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB + rm GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB + echo "deb https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list + sudo apt-get update + + - name: Install Intel oneAPI + timeout-minutes: 5 + run: sudo apt-get install intel-oneapi-compiler-fortran + + - name: Checkout SWAT + uses: actions/checkout@v3 + + - name: Setup Intel oneAPI environment + run: | + source /opt/intel/oneapi/setvars.sh + printenv >> $GITHUB_ENV + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + + - name: Build + # Build your program with the given configuration + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -- -j 4 diff --git a/.github/workflows/deploy_images.yml b/.github/workflows/deploy_images.yml new file mode 100644 index 0000000..217fc48 --- /dev/null +++ b/.github/workflows/deploy_images.yml @@ -0,0 +1,48 @@ +name: Create and publish SWAT Docker images + +# Configures this workflow to run every time a change is pushed to the branches. +on: + push: + branches: + - '**' # matches every branch + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v4 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e315117 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,49 @@ +## +# crazyzlj/swat:alpine +# +# Usage: +# > cd SWAT +# > docker build -t -f docker/Dockerfile . +# or build and push for amd64 and arm64 platforms simultanously +# > docker buildx build --platform linux/amd64,linux/arm64 --push -t crazyzlj/swat:alpine -f docker/Dockerfile . +# +# Copyright 2022 Liang-Jun Zhu + +# Use alpine as the build container +ARG ALPINE_VERSION=3.15 +FROM alpine:${ALPINE_VERSION} as builder + +LABEL maintainer="Liang-Jun Zhu " + +# Replace alpine repository source cdn to accelarate access speed; Setup build environment +# RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ + && apk update && apk upgrade \ + && apk add --no-cache cmake gfortran make musl-dev + +# Copy source directory +WORKDIR /SWAT +COPY . . + +# # Build for release +ARG INSTALL_DIR=/SWAT/dist +RUN cd /SWAT \ + && mkdir build \ + && cd build \ + && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \ + && make -j 2 \ + && make install \ + && cd .. + +# # Build final image +FROM alpine:${ALPINE_VERSION} as runner + +# Replace alpine repository source cdn; Add GNU gfortran library +# RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ + && apk update && apk upgrade \ + && apk add --no-cache libgfortran + +# Order layers starting with less frequently varying ones +ARG INSTALL_DIR=/SWAT/dist +COPY --from=builder ${INSTALL_DIR}/bin/ /usr/bin/