-
Notifications
You must be signed in to change notification settings - Fork 31
140 lines (119 loc) · 4.27 KB
/
deploy_via_ecr.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
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
131
132
133
134
135
136
137
138
139
name: Deploy Spring Boot App
on:
push:
branches:
- master
env:
ENV_GITHUB: ${{ vars.ENV_GITHUB }}
IMAGE_TAG: ${{ github.run_number }}
CONTAINER_NAME: hikaricp
REGISTRY: docker.io
IMAGE_NAME: ${{ github.actor }}/hikaricp:latest
jobs:
project-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'corretto'
- name: Set up the Maven dependencies caching
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Install Maven dependencies
run: mvn clean install
- name: Run tests
run: mvn clean --batch-mode --update-snapshots verify
push-to-ecr:
runs-on: ubuntu-latest
needs: project-build
env:
IMAGE_TAG: ${{ github.run_number }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log into the Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Scrape build info
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
push: true
labels: ${{ steps.meta.outputs.labels }}
# tags: hendisantika/bmi:${{ steps.meta.outputs.tags }}
# tags: ${{ steps.meta.outputs.tags }}
# tags: hendisantika/bmi:latest
tags: hendisantika/hikaricp:${{ github.run_number }}
build-args: |
GIT_VERSION_TAG=${{ env.RELEASE_VERSION }}
GIT_COMMIT_MESSAGE=${{ github.event.head_commit.message }}
GIT_VERSION_HASH=${{ github.sha }}
- run: docker build --tag hikaricp:$IMAGE_TAG .
- name: Push to ECR
env:
ECR_REGISTRY: ${{ steps.login-pf-aws-ecr.outputs.registry }}
ECR_REPOSITORY: ${{secrets.AWS_ECR_REPO}}
IMAGE_TAG: ${{ github.run_number }}
id: ecr
uses: jwalton/gh-ecr-push@v1
with:
access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
region: ${{ secrets.AWS_REGION }}
image: hikaricp:$IMAGE_TAG
deploy:
needs: push-to-ecr
name: deploy image
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: install ssh keys
# check this thread to understand why its needed:
# <https://stackoverflow.com/a/70447517>
run: |
install -m 600 -D /dev/null ~/.ssh/id_rsa
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Docker pull & run from github
uses: appleboy/ssh-action@master
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.AWS_ECR_REPO }}
IMAGE_TAG: ${{ github.run_number }}
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: ECR_REGISTRY, CONTAINER_NAME, IMAGE_TAG
allenvs: true
script: |
docker pull $ECR_REGISTRY/$CONTAINER_NAME:$IMAGE_TAG
docker system prune -af
docker stop hikaricp
docker rm hikaricp
docker run -d --rm --name hikaricp -p 9001:9001 $ECR_REGISTRY/$CONTAINER_NAME:$IMAGE_TAG