-
Notifications
You must be signed in to change notification settings - Fork 2
133 lines (123 loc) · 4.8 KB
/
build_test.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
on:
push:
branches:
- "33-Scaffold-and-use-ECR"
permissions:
pull-requests: write
name: ✨ Build Test 🧐
jobs:
TF-Apply:
name: 🏗️ Apply Infra
runs-on: ubuntu-latest
# https://stackoverflow.com/questions/59175332/using-output-from-a-previous-job-in-a-new-one-in-a-github-action
outputs:
apigw: ${{steps.APIGW.outputs.NEXT_PUBLIC_APIGW}}
registry_url: ${{steps.APIGW.outputs.REGISTRY_URL}}
steps:
- name: Check Out
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
terraform_version: 1.9.2
- name: Look around nd Init 👁️👁️
run: |
echo Looking around 👁️👁️
ls
cd terraform
echo Looking around 👁️👁️
ls
terraform init
- name: 🏗️ Terraform Apply
run: |
cd terraform
terraform apply -var="bucket-name=${{ secrets.S3_BUCKET }}" -var="db-name=${{ secrets.DB_NAME }}" -var="db-username=${{ secrets.DB_USER }}" -auto-approve
- run: echo ${{ steps.plan.outputs.stdout }}
- run: echo ${{ steps.plan.outputs.stderr }}
- run: echo ${{ steps.plan.outputs.exitcode }}
- name: Fetch API GW
id: APIGW
run: |
cd terraform
echo "NEXT_PUBLIC_APIGW=$(terraform output -raw api-route)" >> $GITHUB_OUTPUT
echo "REGISTRY_URL=$(terraform output -raw ecr-push-url)" >> $GITHUB_OUTPUT
web-deploy:
name: 💫 Deploy
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
needs: ["TF-Apply"]
steps:
- name: Check Out
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Use Node.js
uses: actions/setup-node@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4.0.2
with:
aws-region: us-west-2
aws-access-key-id: ${{secrets.AWS_ACCESS_KEY_ID}}
aws-secret-access-key: ${{secrets.AWS_SECRET_ACCESS_KEY}}
- name: 😪 Installing Dependencies
run: |
cd web
npm install
- name: ⚗️ Write env variables
run: |
cd web
echo "NEXT_PUBLIC_APIGW=${{needs.TF-Apply.outputs.apigw}}" > .env
- name: 🔨 Build Static Project
run: |
cd web
STATIC=1 npm run build
- name: 🚀 Upload Package
run: |
aws s3 sync ./web/out s3://${{ secrets.S3_BUCKET }}/ --region us-west-2
aws s3 sync ./web/out s3://${{ secrets.S3_BUCKET }}-failover/ --region us-west-2
# https://octopus.com/blog/githubactions-docker-ecr
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: 🐳 Build Image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ecr-ex-config
IMAGE_TAG: latest
run: |
# Build a docker container and push it to ECR
cd web
docker build --output type=local,dest=./alpine_storefront -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
echo "Pushing image to ECR..."
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "artifact-url=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
# - name: 🐳 Build Image
# uses: docker/build-push-action@v6
# with:
# context: ./web/
# file: ./web/Dockerfile
# push: true # if we wanted to push the image to DockerHub or a local registry
# tags: ecr-ex-config
# # cache-to: type=local,dest=user/app:cache
# outputs: type=local,dest=./alpine_storefront,type=image,dest=${{needs.TF-Apply.outputs.registry_url}}/ecr-ex-config:latest
# secret-envs: NEXT_PUBLIC_APIGW=${{needs.TF-Apply.outputs.apigw}}
- name: Move Image to copyable location
run: |
stat ./alpine_storefront
tar -zcf alpine_storefront.tar.gz ./alpine_storefront
sudo cp ./alpine_storefront.tar.gz /home/
- name: Upload Artifact
uses: actions/upload-artifact@v4
id: artifact-upload-step
with:
name: storefront_image
path: /home/alpine_storefront.tar.gz
overwrite: true
if-no-files-found: error
- name: Echo artifact url and send to SSM parameter
run: |
echo ARTIFACT_URL=${{steps.artifact-upload-step.outputs.artifact-url}}
aws ssm put-parameter --name "ecr_artifact_url" --value "${{steps.artifact-upload-step.outputs.artifact-url}}" --type String --overwrite