From 6c94c26685db515106394303708c4bd2e1352f7d Mon Sep 17 00:00:00 2001 From: "K.B.Dharun Krishna" Date: Sat, 11 May 2024 14:53:11 +0530 Subject: [PATCH] feat: add support for image caching Signed-off-by: K.B.Dharun Krishna --- .github/workflows/vib-build.yml | 56 +++++++++++++++---- .github/workflows/vib-pr.yml | 27 --------- README.md | 12 ++-- includes.container/image-info/base-image-name | 2 +- includes.container/image-info/image-name | 2 +- 5 files changed, 54 insertions(+), 45 deletions(-) delete mode 100644 .github/workflows/vib-pr.yml diff --git a/.github/workflows/vib-build.yml b/.github/workflows/vib-build.yml index 5bdf87f..ad08d2a 100644 --- a/.github/workflows/vib-build.yml +++ b/.github/workflows/vib-build.yml @@ -3,8 +3,11 @@ name: Vib Build on: push: branches: [ "main" ] + tags: + - '*' schedule: - cron: '21 3 * * *' + pull_request: workflow_dispatch: env: @@ -13,11 +16,14 @@ env: jobs: check_update: runs-on: ubuntu-latest + outputs: has_updates: ${{ steps.set_output.outputs.has_updates }} base_image: ${{ steps.read_base_recipe.outputs.base_image }} + permissions: - contents: write + contents: write # Allow actions to create digest, etc. + steps: - name: Checkout code uses: actions/checkout@v4 @@ -34,7 +40,7 @@ jobs: echo "base_image=$BASE_IMAGE" >> "$GITHUB_OUTPUT" echo "BASE_IMAGE=$BASE_IMAGE" >> "$GITHUB_ENV" - - name: get last successful run + - name: Get last successful run if: ${{ github.ref_type == 'branch' }} env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -86,8 +92,10 @@ jobs: runs-on: ubuntu-latest needs: check_update if: ${{ needs.check_update.outputs.has_updates == 'true' }} + permissions: - packages: write + packages: write # Allow pushing images to GHCR. + steps: - uses: actions/checkout@v4 @@ -100,17 +108,45 @@ jobs: run: | REPO_OWNER_LOWERCASE="$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" echo "REPO_OWNER_LOWERCASE=$REPO_OWNER_LOWERCASE">> "$GITHUB_ENV" - echo "IMAGE_TAG=ghcr.io/$REPO_OWNER_LOWERCASE/${{ env.CUSTOM_IMAGE_NAME }}:main">> "$GITHUB_ENV" + echo "IMAGE_URL=ghcr.io/$REPO_OWNER_LOWERCASE/${{ env.CUSTOM_IMAGE_NAME }}">> "$GITHUB_ENV" - name: Set image info run: | echo -n "${{ needs.check_update.outputs.base_image }}" > ./includes.container/image-info/base-image-name echo -n "${{ env.REPO_OWNER_LOWERCASE }}/${{ env.CUSTOM_IMAGE_NAME }}" > ./includes.container/image-info/image-name - - name: Build the Docker image - run: docker image build -f Containerfile --tag "${{ env.IMAGE_TAG }}" . + - name: Docker meta + id: docker_meta + uses: docker/metadata-action@v5 + with: + images: | + ${{ env. IMAGE_URL }} + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{raw}} + type=semver,pattern=v{{major}} + type=ref,event=branch - - name: Push To GHCR - run: | - docker login ghcr.io -u ${{ github.repository_owner }} -p ${{ secrets.GITHUB_TOKEN }} - docker image push "${{ env.IMAGE_TAG }}" + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to GitHub Package Registry + uses: docker/login-action@v3 + if: ${{ github.event_name == 'push' }} + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and Push the Docker image + id: push + uses: docker/build-push-action@v5 + with: + context: . + file: Containerfile + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.docker_meta.outputs.tags }} + labels: ${{ steps.docker_meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.github/workflows/vib-pr.yml b/.github/workflows/vib-pr.yml deleted file mode 100644 index f8e9ccd..0000000 --- a/.github/workflows/vib-pr.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Vib PR - -on: - pull_request: - branches: [ "main" ] - -jobs: - - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - - uses: vanilla-os/vib-gh-action@v0.7.0 - with: - recipe: 'recipe.yml' - plugins: 'Vanilla-OS/vib-fsguard:v1.4' - - - name: Build the Docker image - run: docker image build -f Containerfile --tag testing . - - - uses: actions/upload-artifact@v4 - with: - name: Containerfile - path: Containerfile diff --git a/README.md b/README.md index b90af5c..05c894e 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,18 @@ It is suggested to check the [Vib repository's README](https://github.com/Vanill ## Getting Started -- First, click on the "Use this template" button in the top right corner, then from the drop-down menu select "Create a new repository". This would create a new repository with the same files and directories as this repository. -- Go to Settings → Actions → General and ensure "Allow all actions and reusable workflows" are enabled. +- First, click on the Use this template button in the top right corner, then from the drop-down menu select Create a new repository. This would create a new repository with the same files and directories as this repository. +- Go to **Settings → Actions → General** and ensure "_Allow all actions and reusable workflows_" are enabled. - Now, clone the repository to your local machine and let's start customizing your image. You can also use the GitHub online editor if you prefer. -- Open the `vib-build.yml` workflow file and replace the custom image name with an image name of your choosing in line 11. -- Open the `recipe.yml` file and replace the image name and ID with your image name and ID in lines 2 and 3. +- Open the `vib-build.yml` workflow file and replace the custom image name with an image name of your choosing in line 14. +- Open the `recipe.yml` file and replace the image name and ID with your image name and ID in lines 1 and 2. - Now, perform your additions and modifications to the recipe as per your requirements. -- If you just want to install .deb files, you can just put them in `includes.container/deb-pkgs` +- If you just want to install `.deb` files, you can just put them in `includes.container/deb-pkgs` - Optionally, add your modules to the `modules` directory and add them to the package-modules `includes` in `recipe.yml`. - You can check the Actions tab in GitHub to see the build progress of your image. > [!NOTE] -> It is suggested to add a `vib-image` tag to your repository for your image to be easily discoverable. +> It is suggested to add `vib-image` and `vib` tags to your repository for your image to be easily discoverable to others. ## Use your custom image diff --git a/includes.container/image-info/base-image-name b/includes.container/image-info/base-image-name index 702982a..abda916 100644 --- a/includes.container/image-info/base-image-name +++ b/includes.container/image-info/base-image-name @@ -1 +1 @@ -This will be replaced by the github workflow to what is set in the recipe. For example: vanilla-os/desktop \ No newline at end of file +This will be replaced by the github workflow to what is set in the recipe. For example: vanilla-os/desktop. diff --git a/includes.container/image-info/image-name b/includes.container/image-info/image-name index 31cb583..4c36942 100644 --- a/includes.container/image-info/image-name +++ b/includes.container/image-info/image-name @@ -1 +1 @@ -This will be replaced by the github workflow to what is set in the workflow. For example: taukakao/custom \ No newline at end of file +This will be replaced by the github workflow to what is set in the workflow. For example: taukakao/custom.