From d1111047de1acebada93ec5ceaac7fed4f1747d3 Mon Sep 17 00:00:00 2001 From: Lukas Trippe Date: Fri, 10 Jan 2025 12:36:58 +0100 Subject: [PATCH] ci: build and push dev images (#1448) * add push images workflow * allow for workflow dispatch trigger * remove trigger --- .github/workflows/push-images.yaml | 52 ++++++++++++++++++++++++++++++ .github/workflows/test.yaml | 4 --- .github/workflows/validate.yaml | 1 - docker/dev-env/Dockerfile | 30 +++++++++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/push-images.yaml create mode 100644 docker/dev-env/Dockerfile diff --git a/.github/workflows/push-images.yaml b/.github/workflows/push-images.yaml new file mode 100644 index 000000000..4ae88b415 --- /dev/null +++ b/.github/workflows/push-images.yaml @@ -0,0 +1,52 @@ +name: Push images + +on: + push: + + branches: + - master + workflow_dispatch: + +env: + IMAGE_NAME: eur-dev-env + BASE_ENV: envs/linux-pinned.yaml + +jobs: + push-image: + name: dev-env + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: 'Get relevant env' + run: | + hash_last_changed=$(git log -1 --pretty=format:%H -- ${{ env.BASE_ENV }}) + echo "hash_last_changed=$hash_last_changed" >> $GITHUB_ENV + + - name: 'Login to GitHub Container Registry' + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.REGISTRY_TOKEN }} + + - name: 'Build new image' # only build if the pinned envs file was modified + if: env.hash_last_changed == github.sha || github.event_name == 'workflow_dispatch' + run: | + docker build . --file docker/dev-env/Dockerfile --tag ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + docker push ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + + # Add latest tag if on main branch + if [ "${{ github.ref }}" == "refs/heads/master" ]; then + docker tag ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ github.sha }} ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest + docker push ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest + fi + + - name: 'Add SHA tag to existing image' # when rebuild is not needed + if: env.hash_last_changed != github.sha + run: |- + docker pull ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ env.hash_last_changed }} + docker tag ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ env.hash_last_changed }} ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + docker push ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ github.sha }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0bc14b55d..ba1e5fa0b 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,7 +1,3 @@ -# SPDX-FileCopyrightText: Contributors to PyPSA-Eur -# -# SPDX-License-Identifier: CC0-1.0 - name: Test workflows on: diff --git a/.github/workflows/validate.yaml b/.github/workflows/validate.yaml index 711980e63..0c4cfc04a 100644 --- a/.github/workflows/validate.yaml +++ b/.github/workflows/validate.yaml @@ -4,7 +4,6 @@ on: pull_request: branches: - master - - main concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/docker/dev-env/Dockerfile b/docker/dev-env/Dockerfile new file mode 100644 index 000000000..6b7b64b16 --- /dev/null +++ b/docker/dev-env/Dockerfile @@ -0,0 +1,30 @@ +# SPDX-FileCopyrightText: Contributors to PyPSA-Eur +# +# SPDX-License-Identifier: CC0-1.0 + +FROM condaforge/mambaforge + +RUN conda update -n base conda +RUN conda install -n base conda-libmamba-solver +RUN conda config --set solver libmamba + +RUN apt-get update && apt-get install -y bash git make + +RUN conda --version + +WORKDIR /pypsa-eur + +COPY ./envs ./temp + +RUN conda env create -n pypsa-eur -f temp/linux-pinned.yaml +RUN conda init bash +RUN echo "conda activate pypsa-eur" >> ~/.bashrc + +SHELL ["/bin/bash", "--login", "-c"] +ENV PATH=/opt/conda/envs/pypsa-eur/bin:$PATH + +RUN rm -r temp +RUN conda clean -afy && \ + rm -rf /tmp/* + +CMD ["bash"]