Skip to content

Commit 2d3157e

Browse files
Inital action
1 parent 8d35303 commit 2d3157e

File tree

4 files changed

+202
-1
lines changed

4 files changed

+202
-1
lines changed

.gitignore

Whitespace-only changes.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright 2024 bbharathkumarreddy96@gmail.com
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,61 @@
11
# ec2-docker-service-deploy-action
2+
23
EC2 Docker service deploy and optional in-action security group ip whitelisting
4+
5+
## Usage
6+
7+
```yaml
8+
name: ci
9+
10+
on:
11+
push:
12+
13+
jobs:
14+
qemu:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Docker QEMU Buildx ECR
18+
uses: bbharathkumarreddy/ec2-docker-service-deploy-action@v0.1
19+
with:
20+
ssh-host: ${{ vars.ssh-host }}
21+
ssh-key: ${{ secrets.ssh-key }}
22+
aws-access-key-id: ${{ secrets.aws-access-key-id }}
23+
aws-secret-access-key: ${{ secrets.aws-secret-access-key }}
24+
aws-region: ${{ vars.aws-region }}
25+
security-group-id: ${{ vars.aws-security-group-id }}
26+
ecr-image-uri: ${{ vars.ecr-uri }}
27+
docker-service: my-service
28+
replicas: 2
29+
docker-network: my-network
30+
docker-published-port: 8000
31+
docker-target-port: 8000
32+
```
33+
34+
## Customizing
35+
36+
### inputs
37+
38+
The following inputs can be used as `step.with` keys:
39+
40+
| Name | Type | Default | Description |
41+
| ----------------------- | ------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------- |
42+
| `ssh-host` | String | | SSH hostname or ip address. |
43+
| `ssh-key` | String | | SSH key to connect to host machine. |
44+
| `ssh-port` | String | `22` | SSH port to deploy the service. |
45+
| `ssh-username` | String | `ec2-user` | SSH username. |
46+
| `aws-access-key-id` | String | | Pass the AWS access key id. |
47+
| `aws-secret-access-key` | String | | Pass the AWS secret access key. |
48+
| `aws-region` | String | | Pass the AWS region. |
49+
| `security-group-id:` | String | | AWS security group id to whitelist github action's ip address temporarily. |
50+
| `ecr-image-uri` | String | | ECR Container image URI with tag. |
51+
| `docker-service` | String | `my-service` | Docker service name. |
52+
| `replicas` | String | `1` | Number of service replicas. |
53+
| `docker-network` | String | `ingress` | Docker network. |
54+
| `docker-published-port` | String | `80` | Docker port to publish. |
55+
| `docker-target-port` | String | `80` | Docker target port of container. |
56+
| `docker-opts` | String | | More docker options to create docker service. eg. `--log-driver=awslogs --log-opt awslogs-group=my-logs --log-opt awslogs-region=us-east-1`. |
57+
| `prune` | Boolean | `true` | On prune true, Will cleanup exitied containers and unused images. |
58+
59+
## Contributing
60+
61+
Want to contribute? ✅

action.yml

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
name: "EC2 Docker service deploy action"
2+
description: "EC2 Docker service deploy and optional in-action security group ip whitelisting"
3+
author: "bbharathkumarreddy@96@gmail.com"
4+
branding:
5+
icon: "package"
6+
color: "blue"
7+
8+
inputs:
9+
ssh-host:
10+
description: "SSH hostname or ip address."
11+
required: true
12+
ssh-key:
13+
description: "SSH key to connect to host machine."
14+
required: false
15+
ssh-port:
16+
description: "SSH port to deploy the service."
17+
default: 22
18+
required: false
19+
ssh-username:
20+
description: "SSH username."
21+
default: "ec2-user"
22+
required: false
23+
aws-access-key-id:
24+
description: "AWS access key id."
25+
required: false
26+
aws-secret-access-key:
27+
description: "AWS Secret access key."
28+
required: false
29+
aws-region:
30+
description: "AWS region to deploy."
31+
default: "us-east-1"
32+
required: false
33+
security-group-id:
34+
description: "AWS security group id to whitelist github action's ip address temporarily."
35+
required: false
36+
ecr-image-uri:
37+
description: "ECR Container image URI with tag."
38+
required: true
39+
docker-service:
40+
description: "Docker service name."
41+
default: "my-service"
42+
required: false
43+
replicas:
44+
description: "Number of service replicas."
45+
default: 1
46+
required: false
47+
docker-network:
48+
description: "Docker network."
49+
default: "ingress"
50+
required: false
51+
docker-published-port:
52+
description: "Docker port to publish."
53+
default: 80
54+
required: false
55+
docker-target-port:
56+
description: "Docker target port of container."
57+
default: 80
58+
required: false
59+
docker-opts:
60+
description: "More docker options to create docker service. eg. --log-driver=awslogs --log-opt awslogs-group=my-logs --log-opt awslogs-region=us-east-1"
61+
required: false
62+
prune:
63+
description: "On prune true, Will cleanup exitied containers and unused images."
64+
default: true
65+
required: false
66+
67+
runs:
68+
using: "composite"
69+
steps:
70+
- name: Get Github action IP
71+
id: "ip"
72+
if: ${{ inputs.security-group-id != '' }}
73+
uses: haythem/public-ip@v1.2
74+
75+
- name: Configure AWS credentials
76+
if: ${{ inputs.security-group-id != '' }}
77+
uses: aws-actions/configure-aws-credentials@v4
78+
with:
79+
aws-access-key-id: ${{ inputs.aws-access-key-id }}
80+
aws-secret-access-key: ${{ inputs.aws-secret-access-key }}
81+
aws-region: ${{ inputs.aws-region }}
82+
83+
- name: Add Github Actions IP to Security group
84+
shell: bash
85+
if: ${{ inputs.security-group-id != '' }}
86+
run: |
87+
aws ec2 authorize-security-group-ingress --group-id ${{ inputs.security-group-id }} --protocol tcp --port ${{ inputs.ssh-port }} --cidr ${{ steps.ip.outputs.ipv4 }}/32
88+
env:
89+
AWS_ACCESS_KEY_ID: ${{ inputs.aws-access-key-id }}
90+
AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
91+
AWS_DEFAULT_REGION: ${{ inputs.aws-region }}
92+
93+
- name: Login to Amazon ECR
94+
id: login-ecr
95+
uses: aws-actions/amazon-ecr-login@v2
96+
97+
- name: EC2 Docker Service Deploy
98+
uses: appleboy/ssh-action@master
99+
with:
100+
host: ${{ inputs.ssh-host }}
101+
username: ${{ inputs.ssh-username }}
102+
key: ${{ inputs.ssh-key }}
103+
script: |
104+
sudo docker login --username AWS -p $(aws ecr get-login-password --region ${{ inputs.aws-region }}) ${{ steps.login-ecr.outputs.registry }}
105+
sudo docker pull ${{ inputs.ecr-image-uri }}
106+
if sudo -S docker service inspect ${{ inputs.docker-service }} &> /dev/null; then
107+
echo "Updating existing service."
108+
sudo -S docker service update \
109+
--force \
110+
--replicas ${{ inputs.replicas }} \
111+
${{ inputs.docker-service }}
112+
else
113+
echo "Creating new service."
114+
sudo -S docker service create \
115+
--name ${{ inputs.docker-service }} \
116+
--replicas ${{ inputs.replicas }} \
117+
--network ${{ inputs.docker-network }} \
118+
--publish published=${{ inputs.published-port }},target=${{ inputs.docker-target-port }} \
119+
${{ inputs.docker-opts }} \
120+
${{ inputs.ecr-image-uri }}
121+
fi
122+
123+
- name: EC2 Prune conatiner and images
124+
if: ${{ inputs.prune == 'true' }}
125+
uses: appleboy/ssh-action@master
126+
with:
127+
host: ${{ inputs.ssh-host }}
128+
username: ${{ inputs.ssh-username }}
129+
key: ${{ inputs.ssh-key }}
130+
script: |
131+
sudo -S docker container prune -f
132+
sudo -S docker image prune -af
133+
134+
- name: Remove Github Actions IP from security group
135+
shell: bash
136+
if: ${{ inputs.security-group-id != '' }} && always()
137+
run: |
138+
aws ec2 revoke-security-group-ingress --group-id ${{ inputs.security-group-id }} --protocol tcp --port ${{ inputs.ssh-port }} --cidr ${{ steps.ip.outputs.ipv4 }}/32
139+
env:
140+
AWS_ACCESS_KEY_ID: ${{ inputs.aws-access-key-id }}
141+
AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
142+
AWS_DEFAULT_REGION: ${{ inputs.aws-region }}

0 commit comments

Comments
 (0)