From 1d90ef48e78657989aa988a3c2ee22671a5bcb09 Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Sat, 13 Jan 2024 21:39:21 +0530 Subject: [PATCH] feat: Build images for forks (#108) Signed-off-by: Mriyam Tamuli --- .github/dependabot.yml | 7 +++ .github/workflows/images-build-and-push.yaml | 60 ++++++++++++++++++++ .github/workflows/images-cleanup.yaml | 27 +++++++++ .github/workflows/images.yaml | 39 ------------- 4 files changed, 94 insertions(+), 39 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/images-build-and-push.yaml create mode 100644 .github/workflows/images-cleanup.yaml delete mode 100644 .github/workflows/images.yaml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..2c7d170 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +version: 2 +updates: + # Maintain dependencies for GitHub Actions + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" diff --git a/.github/workflows/images-build-and-push.yaml b/.github/workflows/images-build-and-push.yaml new file mode 100644 index 0000000..e722131 --- /dev/null +++ b/.github/workflows/images-build-and-push.yaml @@ -0,0 +1,60 @@ +name: Create and publish the container image. + +on: + push: + branches: + tags: + +env: + BASE_REPOSITORY: warmmetal/csi-driver-image + BASE_DEFAULT_BRANCH: master + BASE_IMAGE_NAME: warmmetal/csi-image + FORK_IMAGE_NAME: ghcr.io/${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + if: github.repository == env.BASE_REPOSITORY + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Log in to the GitHub Container registry + uses: docker/login-action@v3 + if: github.repository != env.BASE_REPOSITORY + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: | + name=${{ env.BASE_IMAGE_NAME }},enable=${{ github.repository == env.BASE_REPOSITORY }} + name=${{ env.FORK_IMAGE_NAME }},enable=${{ github.repository != env.BASE_REPOSITORY }} + tags: | + # set 'latest' tag for default branch + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', env.BASE_DEFAULT_BRANCH) }} + # set image tag based on GitHub release tag + type=semver,pattern={{version}},prefix=v + # set the image tag based on the branch + # https://github.com/docker/metadata-action/issues/247#issuecomment-1511259674 + type=ref,event=branch,enable=${{ github.ref_name != env.BASE_DEFAULT_BRANCH }} + flavor: | + latest=false + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/images-cleanup.yaml b/.github/workflows/images-cleanup.yaml new file mode 100644 index 0000000..e943751 --- /dev/null +++ b/.github/workflows/images-cleanup.yaml @@ -0,0 +1,27 @@ +name: Delete old container images + +on: + schedule: + - cron: "0 0 * * *" # Source: https://crontab.guru/every-day + +permissions: + packages: write + +env: + BASE_REPOSITORY: warmmetal/csi-driver-image + +jobs: + clean-ghcr: + name: Delete old unused container images + runs-on: ubuntu-latest + steps: + - uses: actions/delete-package-versions@v4 + if: github.repository != env.BASE_REPOSITORY + with: + owner: ${{ github.repository_owner }} + package-name: ${{ github.event.repository.name }} + package-type: 'container' + min-versions-to-keep: 1 + # https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string + # Ignore any semver or 'latest' + ignore-versions: '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?|latest$' diff --git a/.github/workflows/images.yaml b/.github/workflows/images.yaml deleted file mode 100644 index f7ae14e..0000000 --- a/.github/workflows/images.yaml +++ /dev/null @@ -1,39 +0,0 @@ -name: Create and publish the container image. - -on: - push: - branches: [master] - tags: - -jobs: - build-and-push-image: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Login to Docker Hub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@v5 - with: - images: | - warmmetal/csi-image - tags: | - # set latest tag for default branch - type=raw,value=latest,enable={{is_default_branch}} - # set image tag based on GitHub release tag - type=semver,pattern={{version}} - - - name: Build and push Docker image - uses: docker/build-push-action@v5 - with: - context: . - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }}