-
Notifications
You must be signed in to change notification settings - Fork 0
70 lines (58 loc) · 2.68 KB
/
docker-deploy.yml
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
name: Build and Deploy Docker Image
on:
push:
branches:
- main
pull_request:
types: [closed]
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Log in to AWS ECR
uses: aws-actions/amazon-ecr-login@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build Docker image
id: build-image
run: |
docker build -t my-app:${{ github.sha }} .
echo "DOCKER_IMAGE=my-app:${{ github.sha }}" >> $GITHUB_ENV
- name: Determine ECR Repository
id: ecr-repo
run: |
if [[ "${{ github.event.pull_request.merged }}" == "true" && "${{ github.event.pull_request.labels }}" == *"production"* ]]; then
echo "ECR_REPOSITORY=${{ secrets.PROD_ECR_URI }}" >> $GITHUB_ENV
elif [[ "${{ github.event.pull_request.merged }}" != "true" && "${{ github.event.pull_request.labels }}" == *"development"* ]]; then
echo "ECR_REPOSITORY=${{ secrets.DEV_ECR_URI }}" >> $GITHUB_ENV
elif [[ "${{ github.event.pull_request.merged }}" != "true" && -z "${{ github.event.pull_request.labels }}" ]]; then
echo "ECR_REPOSITORY=${{ secrets.DEV_ECR_URI }}" >> $GITHUB_ENV
elif [[ "${{ github.event.pull_request.merged }}" == "true" && -z "${{ github.event.pull_request.labels }}" ]]; then
echo "ECR_REPOSITORY=${{ secrets.PROD_ECR_URI }}" >> $GITHUB_ENV
fi
- name: Tag Docker image
run: docker tag ${{ env.DOCKER_IMAGE }} ${{ env.ECR_REPOSITORY }}:${{ github.sha }}
- name: Push Docker image to Dev (with development label or no label)
if: |
(github.event_name == 'push' && (github.event.pull_request.labels != 'production')) ||
(github.event_name == 'pull_request' && github.event.pull_request.merged == false && env.ECR_REPOSITORY == env.DEV_ECR_URI)
run: docker push ${{ env.ECR_REPOSITORY }}:${{ github.sha }}
- name: Push Docker image to Prod (after merge with production label or no label)
if: |
github.event_name == 'pull_request' && github.event.pull_request.merged == true &&
(env.ECR_REPOSITORY == env.PROD_ECR_URI)
run: docker push ${{ env.ECR_REPOSITORY }}:${{ github.sha }}