diff --git a/.github/workflows/apptainer-build-image.yml b/.github/workflows/apptainer-build-image.yml new file mode 100644 index 0000000..83ee28f --- /dev/null +++ b/.github/workflows/apptainer-build-image.yml @@ -0,0 +1,159 @@ +name: build-image + +on: + workflow_call: + inputs: + image-name: + required: true + type: string + build-context: + required: true + type: string + dockerfile: + required: true + type: string + r-version: + required: true + type: string + parent-image: + required: false + default: '' + type: string + model-version: + required: false + default: '' + type: string + dockerhub-repo: + required: false + default: "hdpriest0uiuc" + type: string + platforms: + required: false + default: "linux/amd64" + type: string + secrets: + DOCKERHUB_USERNAME: + description: 'DockerHub username used to push images' + required: false + DOCKERHUB_PASSWORD: + description: 'DockerHub password used to push images' + required: false + +env: + DEFAULT_R_VERSION: "4.4" + GITHUB_PAT: ${{ secrets.GH_TOKEN }} + +jobs: + build: + runs-on: ubuntu-24.04 + permissions: + packages: write + + steps: + + - name: lowercase image name + id: name + run: | + echo "image_name=$(echo ${{ inputs.image-name }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT + echo "repository=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT + + - name: set PARENT_IMAGE only if specified + id: parent + shell: bash + run: | + echo "PARENT_IMAGE_IF_SET=$( + [[ -n '${{ inputs.parent-image }}' ]] && + echo "PARENT_IMAGE=ghcr.io/${{ steps.name.outputs.repository }}/"'${{ inputs.parent-image }}' + )" >> $GITHUB_OUTPUT + + - name: set MODEL_VERSION only if specified + id: modelver + shell: bash + run: | + echo "MODEL_VERSION_IF_SET=$( + [[ -n '${{ inputs.model-version }}' ]] && + echo 'MODEL_VERSION=${{ inputs.model-version }}' + )" >> $GITHUB_OUTPUT + + - uses: actions/checkout@v4 + + # create metadata for image + - name: Docker meta + env: + check_var: ${{ secrets.DOCKERHUB_USERNAME }} + is_default_R: ${{ inputs.r-version == env.DEFAULT_R_VERSION }} + id: meta + uses: docker/metadata-action@v5 + with: + # list of Docker images to use as base name for tags + images: | + name=ghcr.io/${{ steps.name.outputs.repository }}/${{ steps.name.outputs.image_name }} + name=${{ inputs.dockerhub-repo }}/${{ steps.name.outputs.image_name }},enable=${{ env.check_var != null }} + # generate Docker tags based on the following events/attributes + tags: | + type=raw,value=latest + # type=schedule + # type=ref,event=branch,enable=${{ env.is_default_R }} + # type=ref,event=branch,suffix=-R${{ inputs.r-version }} + # type=ref,event=pr + # type=semver,pattern={{version}},enable=${{ env.is_default_R }} + # type=semver,pattern={{major}}.{{minor}},enable=${{ env.is_default_R }} + # type=semver,pattern={{major}},enable=${{ env.is_default_R }} + # type=semver,pattern={{version}},suffix=-R${{ inputs.r-version }} + + # setup docker build + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3 + + - name: Inspect Builder + run: | + echo "Name: ${{ steps.buildx.outputs.name }}" + echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" + echo "Status: ${{ steps.buildx.outputs.status }}" + echo "Flags: ${{ steps.buildx.outputs.flags }}" + echo "Platforms: ${{ steps.buildx.outputs.platforms }}" + + # login to registries + - name: Login to DockerHub + env: + check_var: ${{ secrets.DOCKERHUB_USERNAME }} + if: env.check_var != null + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + # build the docker images + - name: Build and push ${{ steps.name.outputs.image_name }} + uses: docker/build-push-action@v6 + with: + context: ${{ inputs.build-context }} + file: ${{ inputs.dockerfile }} + push: true + platforms: ${{ inputs.platforms }} + cache-from: type=gha + cache-to: type=gha,mode=max + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + VERSION=${{ steps.meta.outputs.version }} + IMAGE_VERSION=${{ steps.meta.outputs.version }} + PECAN_VERSION=${{ steps.meta.outputs.version }} + R_VERSION=${{ inputs.r-version }} + ${{ steps.parent.outputs.PARENT_IMAGE_IF_SET }} + ${{ steps.modelver.outputs.MODEL_VERSION_IF_SET }} + GITHUB_PAT=${{ secrets.GITHUB_TOKEN }} + PECAN_GIT_BRANCH=${{ github.head_ref || github.ref_name }} + PECAN_GIT_CHECKSUM=${{ github.sha }} + PECAN_GIT_DATE=${{ github.event.repository.updated_at }}