Skip to content

Commit b6fd09a

Browse files
committed
New workflow without using private templates
1 parent f422188 commit b6fd09a

File tree

1 file changed

+155
-78
lines changed

1 file changed

+155
-78
lines changed
+155-78
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,84 @@
1-
name: Deploy-staging
1+
name: Deploy-staging-ecs
22

33
on:
44
workflow_dispatch:
55
inputs:
6-
custom_tag:
6+
tags:
77
description: 'Set custom tag for image'
88
required: true
99
type: string
1010
environment:
11-
type: choice
12-
description: 'The environment to deploy to'
13-
options:
14-
- staging
15-
- production
11+
type: string
1612
required: true
1713
default: 'staging'
14+
aws_region:
15+
description: AWS region
16+
required: false
17+
type: string
18+
aws_ecr_region:
19+
description: AWS region where ECR is set up
20+
required: false
21+
type: string
22+
aws_ecr_account_id:
23+
description: AWS ECR account id (if ECR is on a separate AWS account)
24+
required: false
25+
type: string
26+
timeout_minutes:
27+
required: false
28+
type: number
29+
default: 30
30+
context:
31+
required: false
32+
type: string
33+
default: "."
34+
file:
35+
required: false
36+
type: string
37+
target:
38+
required: false
39+
type: string
40+
description: Target stage to build
41+
platforms:
42+
required: false
43+
type: string
44+
default: "linux/amd64"
45+
provenance:
46+
required: false
47+
type: boolean
48+
default: false
49+
build_args:
50+
required: false
51+
type: string
52+
ssm_param_store_prefix:
53+
required: false
54+
type: string
55+
ssm_param_store_destination_path:
56+
required: false
57+
type: string
58+
secret_files:
59+
description: Docker build secrets
60+
required: false
61+
type: string
62+
aws_role_to_assume:
63+
description: AWS IAM role to assume
64+
required: false
65+
type: string
66+
task_def_path:
67+
required: true
68+
type: string
69+
default: '.aws/ecs/task-definition-app-staging.json'
70+
container_name:
71+
required: true
72+
type: string
73+
description: Name of the container in task definition
74+
default: 'js-react-example'
75+
ecs_service:
76+
required: true
77+
type: string
78+
default: 'js-react-example'
79+
ecs_cluster:
80+
required: true
81+
type: string
1882
push:
1983
branches:
2084
- main
@@ -23,78 +87,91 @@ on:
2387
- '**.md'
2488

2589
jobs:
26-
context:
27-
name: Setup context
28-
runs-on: ubuntu-latest
29-
environment: ${{ inputs.environment }}
30-
outputs:
31-
aws_region: ${{ steps.get.outputs.aws_region }}
32-
aws_ecr_uri: ${{ steps.get.outputs.aws_ecr_uri }}
33-
aws_ecr_region: ${{ steps.get.outputs.aws_ecr_region }}
34-
aws_ecr_account_id: ${{ steps.get.outputs.aws_ecr_account_id }}
35-
aws_ecs_cluster: ${{ steps.get.outputs.aws_ecs_cluster }}
36-
steps:
37-
- id: get
38-
run: |
39-
echo "aws_region=${{ vars.AWS_REGION }}" >> "$GITHUB_OUTPUT"
40-
echo "aws_ecr_uri=${{ vars.AWS_ECR_URI }}" >> "$GITHUB_OUTPUT"
41-
echo "aws_ecr_region=${{ vars.AWS_ECR_REGION }}" >> "$GITHUB_OUTPUT"
42-
echo "aws_ecr_account_id=${{ vars.AWS_ECR_ACCOUNT_ID }}" >> "$GITHUB_OUTPUT"
43-
echo "aws_ecs_cluster=${{ vars.AWS_ECS_CLUSTER }}" >> "$GITHUB_OUTPUT"
44-
4590
build-push-image:
46-
if: ${{ github.event_name == 'push' }}
4791
name: Build deploy image and push to registry
48-
uses: infinum/devops-pipelines/.github/workflows/[email protected]
49-
needs: context
50-
with:
51-
environment: ${{ inputs.environment }}
52-
cloud: AWS
53-
tags: ${{ needs.context.outputs.aws_ecr_uri }}:${{ github.sha }}
54-
aws_ecr_region: ${{ needs.context.outputs.aws_ecr_region }}
55-
aws_ecr_account_id: ${{ needs.context.outputs.aws_ecr_account_id }}
56-
target: deploy
57-
secrets: inherit
58-
59-
build-push-image-manual:
60-
if: ${{ github.event_name == 'workflow_dispatch' }}
61-
name: Run on workflow dispatch
62-
uses: infinum/devops-pipelines/.github/workflows/[email protected]
63-
needs: context
64-
with:
65-
environment: ${{ inputs.environment }}
66-
cloud: AWS
67-
tags: ${{ needs.context.outputs.aws_ecr_uri }}:${{ inputs.custom_tag }}
68-
aws_ecr_region: ${{ needs.context.outputs.aws_ecr_region }}
69-
aws_ecr_account_id: ${{ needs.context.outputs.aws_ecr_account_id }}
70-
secrets: inherit
92+
runs-on: ubuntu-latest
93+
environment: ${{ vars.environment }}
94+
timeout-minutes: ${{ vars.timeout_minutes }}
95+
steps:
96+
- name: Check out code
97+
uses: actions/checkout@v4
98+
99+
- name: Set up Docker Buildx
100+
uses: docker/setup-buildx-action@v3
101+
with:
102+
driver: docker-container
103+
104+
- name: Configure AWS credentials (with OIDC or access keys)
105+
uses: aws-actions/configure-aws-credentials@v4
106+
with:
107+
role-to-assume: ${{ vars.aws_role_to_assume || '' }}
108+
aws-region: ${{ vars.aws_region }}
109+
role-session-name: "github-actions-session"
110+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID || '' }}
111+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY || '' }}
112+
113+
- name: Pull secrets from AWS SSM parameter store
114+
if: ${{ vars.ssm_param_store_prefix }}
115+
uses: infinum/[email protected]
116+
with:
117+
prefix: ${{ vars.ssm_param_store_prefix }}
118+
destination_path: ${{ vars.ssm_param_store_destination_path }}
119+
120+
- name: Configure custom region for AWS ECR
121+
if: ${{ vars.aws_ecr_region != vars.aws_region }}
122+
uses: aws-actions/configure-aws-credentials@v4
123+
with:
124+
aws-region: ${{ vars.aws_ecr_region }}
125+
126+
- name: Login to Amazon ECR
127+
id: login-ecr
128+
uses: aws-actions/amazon-ecr-login@v2
129+
with:
130+
registries: ${{ vars.aws_ecr_account_id }}
131+
132+
- name: Build and push
133+
uses: docker/build-push-action@v6
134+
with:
135+
context: ${{ vars.context }}
136+
platforms: ${{ vars.platforms }}
137+
provenance: ${{ vars.provenance }}
138+
file: ${{ vars.file }}
139+
target: ${{ vars.target }}
140+
push: true
141+
build-args: ${{ vars.build_args }}
142+
tags: ${{ vars.aws_ecr_uri }}:${{ vars.tags }}
143+
cache-from: type=gha
144+
cache-to: type=gha,mode=max
145+
secret-files: ${{ vars.secret_files }}
146+
147+
deploy-to-ecs:
148+
name: 'Deploy to ECS'
149+
runs-on: ubuntu-latest
150+
environment: ${{ vars.environment }}
151+
timeout-minutes: ${{ vars.timeout_minutes }}
152+
steps:
153+
- name: Check out code
154+
uses: actions/checkout@v4
71155

72-
deploy-image:
73-
if: ${{ github.event_name == 'push' }}
74-
name: Deploy backend
75-
uses: infinum/devops-pipelines/.github/workflows/[email protected]
76-
needs: [context, build-push-image]
77-
with:
78-
image_uri: ${{ needs.context.outputs.aws_ecr_uri }}:${{ github.sha }}
79-
environment: ${{ inputs.environment }}
80-
aws_region: ${{ needs.context.outputs.aws_region }}
81-
ecs_cluster: ${{ needs.context.outputs.aws_ecs_cluster }}
82-
ecs_service: js-react-example
83-
task_def_path: .aws/ecs/task-definition-app-staging.json
84-
container_name: js-react-example
85-
secrets: inherit
156+
- name: Configure AWS credentials
157+
uses: aws-actions/configure-aws-credentials@v4
158+
with:
159+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
160+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
161+
aws-region: ${{ vars.aws_region }}
86162

87-
deploy-image-manual:
88-
if: ${{ github.event_name == 'workflow_dispatch' }}
89-
name: Deploy backend
90-
uses: infinum/devops-pipelines/.github/workflows/[email protected]
91-
needs: [context, build-push-image-manual]
92-
with:
93-
image_uri: ${{ needs.context.outputs.aws_ecr_uri }}:${{ inputs.custom_tag }}
94-
environment: ${{ inputs.environment }}
95-
aws_region: ${{ needs.context.outputs.aws_region }}
96-
ecs_cluster: ${{ needs.context.outputs.aws_ecs_cluster }}
97-
ecs_service: js-react-example
98-
task_def_path: .aws/ecs/task-definition-app-staging.json
99-
container_name: js-react-example
100-
secrets: inherit
163+
- name: Fill in the new image ID in the Amazon ECS task definition
164+
id: gen-task-def
165+
uses: aws-actions/[email protected]
166+
with:
167+
task-definition: ${{ vars.task_def_path }}
168+
container-name: ${{ vars.container_name }}
169+
image: ${{ vars.aws_ecr_uri }}:${{ vars.tags }}
170+
171+
- name: Deploy Amazon ECS task definition
172+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
173+
with:
174+
task-definition: ${{ steps.gen-task-def.outputs.task-definition }}
175+
service: ${{ vars.ecs_service }}
176+
cluster: ${{ vars.ecs_cluster }}
177+
wait-for-service-stability: true

0 commit comments

Comments
 (0)