-
Notifications
You must be signed in to change notification settings - Fork 0
129 lines (113 loc) · 3.77 KB
/
build.yml
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
name: Build and push image
on:
pull_request:
branches:
- "main"
push:
branches:
- main
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
name: Build and push image
runs-on: [self-hosted, build]
outputs:
version: ${{ steps.build.version.outputs.version }}
permissions:
contents: write
packages: write
steps:
# - name: Clear up some disk space
#run: |
# sudo rm -rf /usr/share/dotnet
#sudo rm -rf "$AGENT_TOOLSDIRECTORY"
#df -h
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: |
/tmp/.buildx-cache/cache/test
/tmp/.buildx-cache/cache/latest
/tmp/.buildx-cache/cache-new/test
/tmp/.buildx-cache/cache-new/latest
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Test build
if: github.event_name == 'pull_request'
uses: docker/build-push-action@v3
with:
context: .
file: ./build/docker/agent/Dockerfile
load: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}:test
cache-from: type=local,src=/tmp/.buildx-cache/cache/test/
cache-to: type=local,dest=/tmp/.buildx-cache/cache-new/test/,mode=max
build-args: |
USERNAME=paf
USER_UID=1000
USER_GID=1000
- name: Build and push Docker image
id: build
if: github.event_name != 'pull_request'
uses: docker/build-push-action@v3
with:
context: .
file: ./build/docker/agent/Dockerfile
push: true
# tag 'latest' and version on push to main, otherwise use the commit hash
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-from: type=local,src=/tmp/.buildx-cache/cache/latest/
cache-to: type=local,dest=/tmp/.buildx-cache/cache-new/latest/,mode=max
build-args: |
USERNAME=paf
USER_UID=1000
USER_GID=1000
- name: Save pull request artifact
if: github.event_name == 'pull_request'
env:
PR_ID: ${{ github.event.number }}
run: |
mkdir -p ./artifact
printf '{
"is_pr": true,
"pr_id": $PR_ID
}' >> ./artifact/artifact.json
- name: Save merge artifact
if: github.event_name != 'pull_request'
run: |
mkdir -p ./artifact
printf '{
"is_pr": false,
"pr_id": -1
}' >> ./artifact/artifact.json
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: artifact
path: ./artifact/.artifact.json
retention-days: 1
- name: Clean up PR cache
if: github.event_name == 'pull_request'
run: |
rm -rf /tmp/.buildx-cache/cache/test
mv /tmp/.buildx-cache/cache-new/test /tmp/.buildx-cache/cache/test
- name: Clean up merge cache
if: github.event_name != 'pull_request'
run: |
rm -rf /tmp/.buildx-cache/cache/latest
mv /tmp/.buildx-cache/cache-new/latest /tmp/.buildx-cache/cache/latest
- name: Prune all images older than 1 days from self-hosted runner
run: docker image prune -a -f --filter "until=24h"