@@ -4,34 +4,50 @@ name: docker-build-push
44on :
55 workflow_call :
66 inputs :
7- registry :
8- required : true
7+ provider :
8+ required : false
99 type : string
1010 images :
1111 required : true
1212 type : string
13- aws-region :
14- required : true
13+ aws_region :
14+ required : false
1515 type : string
1616 ECR_REPOSITORY :
17- required : true
17+ required : false
1818 type : string
1919 IMAGE_TAG :
2020 required : true
2121 type : string
22+ BUILD_PATH :
23+ required : false
24+ type : string
25+ default : ' .'
26+ WORKING_DIRECTORY :
27+ required : false
28+ type : string
29+ assume_role_arn :
30+ required : false
31+ type : string
2232 secrets :
2333 AWS_ACCESS_KEY_ID :
24- description : ' aws access keys'
25- required : true
34+ required : false
35+ description : ' AWS Access Key ID to install AWS CLI.'
36+ BUILD_ROLE :
37+ required : false
38+ description : ' AWS OIDC role for aws authentication.'
2639 AWS_SECRET_ACCESS_KEY :
27- description : ' aws secret access keys'
28- required : true
40+ required : false
41+ description : ' AWS Secret access key to install AWS CLI'
42+ AWS_SESSION_TOKEN :
43+ required : false
44+ description : ' AWS Session Token to install AWS CLI'
2945 DOCKERHUB_USERNAME :
3046 description : ' dockerhub username'
31- required : true
47+ required : false
3248 DOCKERHUB_PASSWORD :
3349 description : ' dockerhub password'
34- required : true
50+ required : false
3551
3652jobs :
3753 build-image :
@@ -44,54 +60,71 @@ jobs:
4460 uses : actions/checkout@v4
4561
4662 - name : Login to Docker Hub
63+ if : ${{ inputs.provider == 'DOCKERHUB' }}
4764 uses : docker/login-action@v3
4865 with :
4966 username : ${{ secrets.DOCKERHUB_USERNAME }}
5067 password : ${{ secrets.DOCKERHUB_PASSWORD }}
5168
5269 - name : Push docker image to DOCKERHUB
53- if : ${{ inputs.registry == 'DOCKERHUB' }}
70+ if : ${{ inputs.provider == 'DOCKERHUB' }}
5471 env :
5572 IMAGE_TAG : ${{ inputs.IMAGE_TAG }}
5673 images : ${{ inputs.images }}
74+ BUILD_PATH : ${{ inputs.BUILD_PATH }}
5775 run : |
58- docker build -t $images:$IMAGE_TAG .
76+ docker build -t $images:$IMAGE_TAG $BUILD_PATH
5977 docker push $images:$IMAGE_TAG
6078
6179 - name : Configure AWS credentials
80+ if : ${{ inputs.provider == 'aws' }}
6281 uses : aws-actions/configure-aws-credentials@v4
6382 with :
6483 aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
6584 aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
66- aws-region : ${{ inputs.aws-region }}
85+ aws-session-token : ${{ secrets.AWS_SESSION_TOKEN }}
86+ role-to-assume : ${{ secrets.BUILD_ROLE }}
87+ aws-region : ${{ inputs.aws_region }}
88+ role-duration-seconds : ${{ inputs.role-duration-seconds }}
89+ role-skip-session-tagging : true
90+
91+ - name : Verify awscli
92+ if : ${{ inputs.provider == 'aws' }}
93+ run : |
94+ aws sts get-caller-identity
95+
6796
6897 - name : Login to Amazon ECR
98+ if : ${{ inputs.provider == 'aws' }}
6999 id : login-ecr
70100 uses : aws-actions/amazon-ecr-login@v2
71101
72102 - name : Push docker image to Amazon ECR
73- if : ${{ inputs.registry == 'ECR ' }}
103+ if : ${{ inputs.provider == 'aws ' }}
74104 id : docker-build
75105 env :
76106 ECR_REGISTRY : ${{ steps.login-ecr.outputs.registry }}
77107 ECR_REPOSITORY : ${{ inputs.ECR_REPOSITORY }}
78108 IMAGE_TAG : ${{ inputs.IMAGE_TAG }}
109+ BUILD_PATH : ${{ inputs.BUILD_PATH }}
110+ working-directory : ${{ inputs.WORKING_DIRECTORY }}
79111 run : |
80- docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
112+ docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $BUILD_PATH
81113 docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
82114
83115 - name : Push docker image to Amazon ECR and DOCKERHUB
84- if : ${{ inputs.registry == 'DOCKERHUB,ECR ' }}
116+ if : ${{ inputs.provider == 'DOCKERHUB,aws ' }}
85117 env :
86118 # # For ECR env variable
87119 ECR_REGISTRY : ${{ steps.login-ecr.outputs.registry }}
88120 ECR_REPOSITORY : ${{ inputs.ECR_REPOSITORY }}
89121 IMAGE_TAG : ${{ inputs.IMAGE_TAG }}
122+ BUILD_PATH : ${{ inputs.BUILD_PATH }}
90123 # # For DOCKERHUB env variable
91124 images : ${{ inputs.images }}
92125 run : |
93- docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
126+ docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $BUILD_PATH
94127 docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
95- docker build -t $images:$IMAGE_TAG .
128+ docker build -t $images:$IMAGE_TAG $BUILD_PATH
96129 docker push $images:$IMAGE_TAG
97130 ...
0 commit comments