Created
April 8, 2021 20:26
-
-
Save acidjazz/9deace6d6ca64bb5c9614bd6a9f80a41 to your computer and use it in GitHub Desktop.
deploy using ssm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy | |
on: | |
push: | |
branches: | |
- staging | |
- production | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set service name | |
shell: bash | |
run: echo "::set-env name=SERVICE::service-import" | |
- name: Extract branch name | |
shell: bash | |
run: echo "::set-env name=BRANCH::$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')" | |
- name: Set Region for staging | |
run: echo "::set-env name=REGION::us-east-2" | |
if: env.BRANCH == 'staging' | |
- name: Set Region for production | |
run: echo "::set-env name=REGION::us-east-1" | |
if: env.BRANCH == 'production' | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.REGION }} | |
- name: Deploy to environment | |
run: | | |
commandId=$(aws ssm send-command \ | |
--region "${REGION}" \ | |
--targets "Key=tag:ssm,Values=${SERVICE}-${BRANCH}" \ | |
--document-name "AWS-RunShellScript" \ | |
--comment "${SERVICE} to ${BRANCH}" \ | |
--parameters '{"commands": ["su - ec2-user -c \"cd '"${SERVICE}"'; composer '"${BRANCH}"' \""]}' \ | |
--output text \ | |
--query "Command.CommandId") | |
status="InProgress" | |
echo ["$commandId"] Status: "$status" | |
while [ "$status" = InProgress ] | |
do | |
status=$(aws ssm list-commands \ | |
--region "${REGION}" \ | |
--command-id "$commandId" \ | |
--query "Commands[*].Status" \ | |
| tr -cd '[:alpha:]') | |
printf . | |
done | |
echo | |
echo ["$commandId"] Status: "$status" | |
aws ssm list-command-invocations \ | |
--region "${REGION}" \ | |
--command-id "$commandId" \ | |
--query="CommandInvocations[*].CommandPlugins[*].Output" \ | |
--details \ | |
| sed 1,2d | sed "s/^[ \t]*\"//" | sed 's/\\n/\'$'\n/g' | head -n -3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment