|
| 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