-
Notifications
You must be signed in to change notification settings - Fork 16
130 lines (118 loc) · 4.17 KB
/
Copy pathdocker-build-push.yml
File metadata and controls
130 lines (118 loc) · 4.17 KB
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: 🐳 docker-build-push
on:
workflow_call:
inputs:
provider:
required: false
type: string
images:
required: false
type: string
aws_region:
required: false
type: string
ECR_REPOSITORY:
required: false
type: string
IMAGE_TAG:
required: true
type: string
BUILD_PATH:
required: false
type: string
default: '.'
WORKING_DIRECTORY:
required: false
type: string
assume_role_arn:
required: false
type: string
secrets:
AWS_ACCESS_KEY_ID:
required: false
description: 'AWS Access Key ID to install AWS CLI.'
BUILD_ROLE:
required: false
description: 'AWS OIDC role for aws authentication.'
AWS_SECRET_ACCESS_KEY:
required: false
description: 'AWS Secret access key to install AWS CLI'
AWS_SESSION_TOKEN:
required: false
description: 'AWS Session Token to install AWS CLI'
DOCKERHUB_USERNAME:
description: 'dockerhub username'
required: false
DOCKERHUB_PASSWORD:
description: 'dockerhub password'
required: false
jobs:
build-image:
name: 🏗️ Build Images
runs-on: ubuntu-latest
steps:
- name: 📦 Checkout Repository
uses: actions/checkout@v6
- name: 🔑 Login to Docker Hub
if: ${{ inputs.provider == 'DOCKERHUB' }}
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: 🚀 Push docker image to DOCKERHUB
if: ${{ inputs.provider == 'DOCKERHUB' }}
env:
IMAGE_TAG: ${{ inputs.IMAGE_TAG }}
images: ${{ inputs.images }}
BUILD_PATH: ${{ inputs.BUILD_PATH }}
run: |
docker build -t $images:$IMAGE_TAG $BUILD_PATH
docker push $images:$IMAGE_TAG
- name: 🔧 Configure AWS credentials
if: ${{ inputs.provider == 'aws' }}
uses: aws-actions/configure-aws-credentials@v6
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
role-to-assume: ${{ secrets.BUILD_ROLE }}
aws-region: ${{ inputs.aws_region }}
role-duration-seconds: ${{ inputs.role-duration-seconds }}
role-skip-session-tagging: true
- name: 🕵️ Verify awscli
if: ${{ inputs.provider == 'aws' }}
run: |
aws sts get-caller-identity
- name: 🔑 Login to Amazon ECR
if: ${{ inputs.provider == 'aws' }}
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: 🚢 Push docker image to Amazon ECR
if: ${{ inputs.provider == 'aws' }}
id: docker-build
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ inputs.ECR_REPOSITORY }}
IMAGE_TAG: ${{ inputs.IMAGE_TAG }}
BUILD_PATH: ${{ inputs.BUILD_PATH }}
working-directory: ${{ inputs.WORKING_DIRECTORY }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $BUILD_PATH
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: 🚀🚢 Push docker image to Amazon ECR and DOCKERHUB
if: ${{ inputs.provider == 'DOCKERHUB,aws' }}
env:
## For ECR env variable
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ inputs.ECR_REPOSITORY }}
IMAGE_TAG: ${{ inputs.IMAGE_TAG }}
BUILD_PATH: ${{ inputs.BUILD_PATH }}
## For DOCKERHUB env variable
images: ${{ inputs.images }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $BUILD_PATH
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker build -t $images:$IMAGE_TAG $BUILD_PATH
docker push $images:$IMAGE_TAG
...