-
Notifications
You must be signed in to change notification settings - Fork 2
89 lines (88 loc) · 3.32 KB
/
shared-build.yaml
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
name: 'Shared CI flow'
on:
workflow_call:
inputs:
docker_image_version_suffix_label:
type: string
description: Version label to be used for docker build
required: false
default: ''
docker_push:
type: boolean
description: Whether to push image after build
required: false
default: true
outputs:
build_date:
description: build_date
value: ${{ jobs.build.outputs.build_date }}
env:
REGISTRY: ghcr.io
jobs:
build:
runs-on: ubuntu-latest
outputs:
image_repository: ${{ steps.setOutput.outputs.image_repository }}
package_version: ${{ steps.setOutput.outputs.package_version }}
build_date: ${{ steps.setOutput.outputs.build_date }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Use Node.js Current
uses: actions/setup-node@v3
with:
node-version: 18.12
- name: Restore cached dependencies
uses: actions/cache@v3
with:
path: node_modules
key: node-modules-${{ hashFiles('package.json') }}
- name: Install dependencies
uses: bahmutov/npm-install@v1
- name: Set environment variables
run: |
mkdir yq && wget https://github.com/mikefarah/yq/releases/download/v4.21.1/yq_linux_amd64.tar.gz -q -O - | tar xz -C yq && sudo mv yq/yq_linux_amd64 /usr/bin/yq
yq -i ".version = \"$(yq .version package.json)\", .buildDate = \"$(date +%Y%m%d.%H%M)\"" metrics.yml
echo IMAGE_REPOSITORY=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
echo PACKAGE_VERSION=$(yq .version metrics.yml) >> $GITHUB_ENV
echo BUILD_DATE=$(yq .buildDate metrics.yml) >> $GITHUB_ENV
- name: Lint code
run: npm run lint
- name: Test
run: npm run test:ci
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to GitHub Docker Registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build And Push Image with suffix label
if: inputs.docker_image_version_suffix_label != ''
uses: docker/build-push-action@v3
with:
platforms: linux/amd64,linux/arm64
push: ${{ inputs.docker_push }}
context: .
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_REPOSITORY }}/app:${{ env.BUILD_DATE }}-${{ inputs.docker_image_version_suffix_label }}
${{ env.REGISTRY }}/${{ env.IMAGE_REPOSITORY }}/app:${{ inputs.docker_image_version_suffix_label }}
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
- name: Build And Push Image
if: inputs.docker_image_version_suffix_label == ''
uses: docker/build-push-action@v3
with:
platforms: linux/amd64,linux/arm64
push: ${{ inputs.docker_push }}
context: .
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_REPOSITORY }}/app:${{ env.BUILD_DATE }}
${{ env.REGISTRY }}/${{ env.IMAGE_REPOSITORY }}/app:latest
labels: |
org.opencontainers.image.source=https://github.com/${{ github.repository }}
- name: Set output variables
id: setOutput
run: |
echo "build_date=${{ env.BUILD_DATE }}" >> $GITHUB_OUTPUT