-
Notifications
You must be signed in to change notification settings - Fork 0
130 lines (119 loc) · 4.92 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
130
name: Build, publish and run tests
on:
push:
branches: [ 'main' ]
pull_request:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
runs-on: self-hosted
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: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Bump version and push tag
# only run on push to main
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
id: tag
uses: mathieudutour/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
release_branches: main
- name: Get commit hash
id: hash
run: echo "hash=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v3
with:
context: .
file: ./build/docker/build/Dockerfile
push: true
# tag 'latest' and version on push to main, otherwise use the commit hash
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.new_version == '' && steps.hash.outputs.hash || steps.tag.outputs.new_version }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'latest' || steps.hash.outputs.hash }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Output version
id: version
# take either the created tag or the commit hash
run: echo "version=${{ steps.tag.outputs.new_version == '' && steps.hash.outputs.hash || steps.tag.outputs.new_version }}" >> $GITHUB_OUTPUT
drive:
runs-on: self-hosted
needs: build-and-push-image
# run only on pull request for now
if: github.event_name == 'pull_request'
env:
AGENT_VERSION: ${{ needs.build-and-push-image.outputs.version }}
COMPOSE_FILE: ./build/docker-compose_cicd.yaml
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Print environment variables (DEBUG)
run: |
echo "AGENT_VERSION=${AGENT_VERSION}"
echo "COMPOSE_FILE=${COMPOSE_FILE}"
- name: Get commit hash
id: hash
run: echo "hash=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Set AGENT_VERSION from hash (workaround)
run: echo "AGENT_VERSION=${{ steps.hash.outputs.hash }}" >> $GITHUB_ENV
- name: Run docker-compose
run: docker compose up --quiet-pull --exit-code-from agent
- name: Copy results
run: docker compose cp agent:/tmp/simulation_results.json .
- name: Stop docker-compose
# always run this step, to clean up even on error
if: always()
run: docker compose down -v
# add rendered JSON as comment to the pull request
- name: Add simulation results as comment
if: github.event_name == 'pull_request'
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# this script reads the simulation_results.json and creates a comment on the pull request with the results.
script: |
const fs = require('fs');
// read the simulation results
const results = fs.readFileSync('./simulation_results.json', 'utf8');
let resultsJson = JSON.parse(results);
// create a markdown table of the results
let resultsTable = resultsJson.values.map((values, i) => {
return `| ${resultsJson.labels[i]} | ${values} |`;
});
// create a markdown table header
let resultsTableHeader = `| Metric | Value |`;
// create a markdown table divider
let resultsTableDivider = `| --- | --- |`;
// add everything to the resultsTable
resultsTable = resultsTableHeader + '\n' + resultsTableDivider + '\n' + resultsTable.join('\n');
// add the results as a comment to the pull request
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "## Simulation results\n" + resultsTable
});
- name: Prune all images older than 30 days from self-hosted runner
run: docker image prune -a --force --filter "until=720h"