summaryrefslogtreecommitdiffstats
path: root/.github/workflows/build-push.yml
blob: 666c9e39d6fab4dcafe6d4d11f3928485c210299 (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
name: Build and push containers
on:
  push:
    branches:
      - master
    tags:
      - 'v*'
  pull_request:

env:
  BUILDBOT_VERSION: 3.8.0

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  build-test-push:
    name: Build, test and push containers
    runs-on: ubuntu-latest

    permissions:
      packages: write

    strategy:
      fail-fast: ${{ github.event_name == 'pull_request' }}
      matrix:
        container_flavor:
          - master
          - worker

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Environment variables
        run: |
          echo "GIT_SHA_SHORT=${GITHUB_SHA::8}" >> $GITHUB_ENV

      - name: Build container and export it to local Docker
        uses: docker/build-push-action@v4
        with:
          load: true
          tags: local/${{ matrix.container_flavor }}
          file: docker/build${{ matrix.container_flavor }}/Dockerfile
          build-args: |
            BUILDBOT_VERSION=${{ env.BUILDBOT_VERSION }}
            OPENWRT_VERSION=${{ env.GIT_SHA_SHORT }}

      - name: Test master Docker container
        if: matrix.container_flavor == 'master'
        run: |
          docker run --detach --name test-master local/master
          sleep 5
          docker logs test-master | tee master.log
          grep "buildmaster configured in /master" master.log

      - name: Test worker Docker container
        if: matrix.container_flavor == 'worker'
        run: |
          docker run --detach --env BUILDWORKER_NAME=X --env BUILDWORKER_PASSWORD=Y --name test-worker local/worker
          sleep 5
          docker logs test-worker | tee worker.log
          grep "worker configured in /builder" worker.log

      - name: Docker meta
        id: meta
        if: github.event_name != 'pull_request' || github.repository_owner != 'openwrt'
        uses: docker/metadata-action@v4
        with:
          images: name=ghcr.io/${{ github.repository }}/build${{ matrix.container_flavor }}-v${{ env.BUILDBOT_VERSION }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        if: github.event_name != 'pull_request' || github.repository_owner != 'openwrt'
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build container again and push it
        uses: docker/build-push-action@v4
        if: github.event_name != 'pull_request' || github.repository_owner != 'openwrt'
        with:
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          file: docker/build${{ matrix.container_flavor }}/Dockerfile
          build-args: |
            BUILDBOT_VERSION=${{ env.BUILDBOT_VERSION }}
            OPENWRT_VERSION=${{ env.GIT_SHA_SHORT }}