summaryrefslogtreecommitdiff
path: root/.gitea/workflows/gentoo-utils.yml
blob: 81170395f4127c12ec56e6f5237971c27d5eea35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
name: Gentoo Utils

on: [push]

defaults:
  run:
    shell: bash -l {0}

jobs:
  build-oci-image:
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      - name: Restore git cache
        uses: actions/cache@v4
        with:
          path: .git
          key: gitea-repo-${{ gitea.repository }}-${{ gitea.ref }}

      - name: Checkout repo
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Check for changes before building
        id: image-changes
        # build image only if 1. changes are detected or 2. an image for the working branch doesnt exist
        run: |
          branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
          default_branch_name="${{ gitea.event.repository.default_branch }}"
          image_tag=latest
          comparison_hash="${{ gitea.event.before }}"

          if [[ "$branch_name" != "$default_branch_name" ]]; then
            image_tag=$branch_name
          fi
          # slugify
          image_tag="$(echo "$image_tag" | sed -E 's/[^a-zA-Z0-9]/-/g')"

          # rebase breaks gitea.event.before, so check to make sure the hash provided exists
          if ! git merge-base --is-ancestor $comparison_hash $branch_name >/dev/null 2>&1; then
            comparison_hash=$(git merge-base origin/$default_branch_name $branch_name)
          fi
            
          if ! git diff $comparison_hash ${{ gitea.sha }} --no-patch --exit-code .docker; then
            build_image=true
          else
            if ! docker manifest inspect ${{ vars.REGISTRY_URL }}/${{ gitea.repository }}:${image_tag} >/dev/null 2>&1; then
              build_image=true
            else
              build_image=false
            fi
          fi

          echo "default_branch_name=$default_branch_name" >> $GITEA_OUTPUT
          echo "branch_name=$branch_name" >> $GITEA_OUTPUT
          echo "image_tag=$image_tag" >> $GITEA_OUTPUT
          echo "comparison_hash=$comparison_hash" >> $GITEA_OUTPUT
          echo "build_image=$build_image" >> $GITEA_OUTPUT
          cat $GITEA_OUTPUT
      - name: Set up Docker buildx
        if: steps.image-changes.outputs.build_image == 'true'
        uses: docker/setup-buildx-action@v3
        with:
          driver-opts: network=runners-net

      - name: Log in to Github Container Registry
        if: steps.image-changes.outputs.build_image == 'true'
        uses: docker/login-action@v3
        with:
          registry: ${{ vars.REGISTRY_URL }}
          username: ${{ vars.CI_BOT_USERNAME }}
          password: ${{ secrets.CI_BOT_TOKEN }}

      - name: Build and push
        if: steps.image-changes.outputs.build_image == 'true'
        uses: docker/build-push-action@v6
        with:
          push: true
          tags: ${{ vars.REGISTRY_URL }}/${{ gitea.repository }}:${{ steps.image-changes.outputs.image_tag }}
          context: "{{defaultContext}}:.docker"
          cache-from: type=gha
          cache-to: type=gha,mode=max
    outputs:
      image_tag: ${{ steps.image-changes.outputs.image_tag }}

  build:
    runs-on: brutalisk
    container:
      image: git.epenguin.net/gentoo-utils/gentoo-utils-gitea:latest
    needs: build-oci-image
    steps:
      - name: Checkout repo
        uses: actions/checkout@v5

      - name: build and check
        run: ./check.sh