-
Notifications
You must be signed in to change notification settings - Fork 0
117 lines (100 loc) · 3.62 KB
/
docker-build-and-push.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
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
name: github-actions/docker/build-and-push
on:
pull_request:
branches:
- develop
types: [labeled, synchronize, opened]
push:
branches:
- develop
env:
REGISTRY: ${{ secrets.HARBOR_URL }}
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
if: contains(github.event.pull_request.labels.*.name, 'deploy') || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main'
name: Docker build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14
- uses: actions/cache@v2
with:
path: node_modules
key: node-14-${{ hashFiles('yarn.lock') }}
- name: Install packages
run: yarn
- name: Build (PR)
if: contains(github.event.pull_request.labels.*.name, 'deploy')
run: yarn build
env:
ROOT_PATH: /pr/${{ github.event.pull_request.number }}
- name: Build (deployment)
if: "!contains(github.event.pull_request.labels.*.name, 'deploy')"
run: yarn build
- name: Upload build
uses: actions/upload-artifact@v2
with:
name: .next
path: .next
push:
if: contains(github.event.pull_request.labels.*.name, 'deploy') || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main'
name: Docker push
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14
- uses: actions/cache@v2
with:
path: node_modules
key: node-14-production-${{ hashFiles('yarn.lock') }}
- name: Install packages
run: yarn --production
- name: Download build
uses: actions/download-artifact@v2
with:
name: .next
path: .next
- name: Set docker registry (PR)
run: echo "DOCKER_REGISTRY=$REGISTRY/pr" >> $GITHUB_ENV
if: contains(github.event.pull_request.labels.*.name, 'deploy')
- name: Set docker registry (deployment)
run: echo "DOCKER_REGISTRY=$REGISTRY/develop" >> $GITHUB_ENV
if: "!contains(github.event.pull_request.labels.*.name, 'deploy')"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Short sha
id: sha
run: echo "::set-output name=sha_short::$(echo ${GITHUB_HEAD_SHA:0:7})"
env:
GITHUB_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}
tags: type=raw,value=${{ steps.sha.outputs.sha_short }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ secrets.HARBOR_USER_ID }}
password: ${{ secrets.HARBOR_USER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: ${{ contains(github.event.pull_request.labels.*.name, 'deploy') || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max