-
Notifications
You must be signed in to change notification settings - Fork 0
121 lines (109 loc) · 3.69 KB
/
build-and-push-docker.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
118
119
120
121
name: Build Docker image and push.
on:
workflow_call:
inputs:
runs-on:
required: true
description: Platform to execute on
type: string
push-to-public-dockerhub:
required: false
type: boolean
default: false
description: Is docker image should be pushed to
org:
required: false
type: string
image:
required: false
type: string
file:
required: false
type: string
default: ./Dockerfile
environment:
required: true
type: string
repository:
required: false
default: crb-${{ github.event.repository.name }}
type: string
secrets:
ORG_AWS_ACCESS_KEY_ID:
required: true
ORG_AWS_SECRET_ACCESS_KEY:
required: true
DOCKERHUB_USERNAME:
required: true
DOCKERHUB_TOKEN:
required: true
AWS_REGION:
required: true
NPM_TOKEN:
required: false
outputs:
version:
description: Docker image tag.
value: ${{ jobs.build-docker-image-and-push-to-registers.outputs.version }}
jobs:
build-docker-image-and-push-to-registers:
name: Build docker image, push to both aws and dockerhub registers.
runs-on: ${{ fromJSON(inputs.runs-on) }}
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v3
- name: Process version
id: version
run: |
echo "::set-output name=version::${{ inputs.environment }}-${{ github.run_attempt}}-$(git rev-parse --short HEAD)"
- name: Configure AWS credentials for push images
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.ORG_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.ORG_AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and push docker image (private & public repos)
if: ${{ inputs.push-to-public-dockerhub }}
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ inputs.repository }}
uses: docker/build-push-action@v2
with:
context: ./
file: ${{ inputs.file }}
push: true
no-cache: true
build-args: |
NPM_TOKEN=${{ secrets.NPM_TOKEN }}
ENV_FILE_NAME=${{ inputs.environment }}
tags: |
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ steps.version.outputs.version }}
${{ inputs.org }}/${{ inputs.image }}:${{ steps.version.outputs.version }}
${{ inputs.org }}/${{ inputs.image }}:${{ inputs.environment }}-latest
- name: Build and push docker image (private repo)
if: ${{ !inputs.push-to-public-dockerhub }}
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ inputs.repository }}
uses: docker/build-push-action@v2
with:
context: ./
file: ${{ inputs.file }}
push: true
no-cache: true
build-args: |
NPM_TOKEN=${{ secrets.NPM_TOKEN }}
ENV_FILE_NAME=${{ inputs.environment }}
tags: |
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ steps.version.outputs.version }}