Added cache system to frontend #38
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: "Terraform" | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
env: | |
TF_VAR_access_key_id: ${{ secrets.SCW_ACCESS_KEY_ID }} | |
TF_VAR_secret_key: ${{ secrets.SCW_ACCESS_KEY }} | |
TF_VAR_project_id: ${{ secrets.SCW_PROJECT_ID }} | |
AWS_ACCESS_KEY_ID: ${{ secrets.SCW_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }} | |
CERT_CRT: ${{ secrets.CERT_CRT }} | |
CERT_KEY: ${{ secrets.CERT_KEY }} | |
jobs: | |
frontend: | |
name: "Frontend" | |
runs-on: ubuntu-latest | |
env: | |
WORKING_DIRECTORY: packages/frontend | |
outputs: | |
image-tag: ${{ steps.extract-tag.outputs.image-tag }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Cache node modules | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
id: cache-npm | |
uses: actions/cache@v3 | |
env: | |
cache-name: cache-node-modules | |
with: | |
path: ~/.npm | |
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }} | |
restore-keys: | | |
${{ runner.os }}-build-${{ env.cache-name }}- | |
${{ runner.os }}-build- | |
${{ runner.os }}- | |
- if: steps.cache-npm.outputs.cache-hit != 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push' | |
name: List the state of node modules | |
continue-on-error: true | |
run: npm list | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Install dependencies | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
run: npm install | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Log in to Scaleway Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: rg.fr-par.scw.cloud/app-namespace | |
username: ${{ secrets.SCW_ACCESS_KEY_ID }} | |
password: ${{ secrets.SCW_ACCESS_KEY }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: rg.fr-par.scw.cloud/app-namespace/frontend | |
tags: | | |
latest | |
type=sha | |
- name: Build and push | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
uses: docker/build-push-action@v4 | |
with: | |
context: ${{ env.WORKING_DIRECTORY }} | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
- name: Extract second tag (sha) | |
id: extract-tag | |
run: | | |
TAGS=(${{ steps.meta.outputs.tags }}) | |
echo "image-tag=${TAGS[1]}" >> "$GITHUB_OUTPUT" | |
backend: | |
name: "Backend" | |
runs-on: ubuntu-latest | |
env: | |
WORKING_DIRECTORY: packages/backend | |
outputs: | |
image-tag: ${{ steps.extract-tag.outputs.image-tag }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Cache node modules | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
id: cache-npm | |
uses: actions/cache@v3 | |
env: | |
cache-name: cache-node-modules | |
with: | |
path: ~/.npm | |
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }} | |
restore-keys: | | |
${{ runner.os }}-build-${{ env.cache-name }}- | |
${{ runner.os }}-build- | |
${{ runner.os }}- | |
- if: steps.cache-npm.outputs.cache-hit != 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push' | |
name: List the state of node modules | |
continue-on-error: true | |
run: npm list | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Install dependencies | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
run: npm install | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Log in to Scaleway Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: rg.fr-par.scw.cloud/app-namespace | |
username: ${{ secrets.SCW_ACCESS_KEY_ID }} | |
password: ${{ secrets.SCW_ACCESS_KEY }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: rg.fr-par.scw.cloud/app-namespace/backend | |
tags: | | |
latest | |
type=sha | |
- name: Build and push | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
uses: docker/build-push-action@v4 | |
with: | |
context: ${{ env.WORKING_DIRECTORY }} | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
- name: Extract second tag (sha) | |
id: extract-tag | |
run: | | |
TAGS=(${{ steps.meta.outputs.tags }}) | |
echo "image-tag=${TAGS[1]}" >> "$GITHUB_OUTPUT" | |
deployment: | |
name: "Terraform" | |
runs-on: ubuntu-latest | |
needs: | |
- backend | |
- frontend | |
permissions: | |
pull-requests: write | |
env: | |
WORKING_DIRECTORY: packages/infrastructure/project | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v2 | |
- name: Terraform Format | |
id: fmt | |
run: terraform fmt -check | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Terraform Init | |
id: init | |
run: terraform init | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Creating certificate | |
run: | | |
mkdir -p certs | |
echo "${{ env.CERT_CRT }}" > certs/server.crt | |
echo "${{ env.CERT_KEY }}" > certs/server.key | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Terraform Validate | |
id: validate | |
run: terraform validate -no-color | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Terraform Plan | |
id: plan | |
if: github.event_name == 'pull_request' | |
run: | | |
export TF_VAR_backend_image=${{ needs.backend.outputs.image-tag }} | |
export TF_VAR_frontend_image=${{ needs.frontend.outputs.image-tag }} | |
terraform plan -no-color -input=false | |
continue-on-error: true | |
working-directory: ${{ env.WORKING_DIRECTORY }} | |
- name: Update Pull Request | |
uses: actions/github-script@v6 | |
if: github.event_name == 'pull_request' | |
env: | |
PLAN: ${{ steps.plan.outputs.stdout }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` | |
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\` | |
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\` | |
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\` | |
<details><summary>Show Plan</summary> | |
\`\`\`terraform\n | |
${process.env.PLAN} | |
\`\`\` | |
</details> | |
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
- name: Terraform Plan Status | |
if: steps.plan.outcome == 'failure' | |
run: exit 1 | |
- name: Terraform Apply | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
run: | | |
export TF_VAR_backend_image=${{ needs.backend.outputs.image-tag }} | |
export TF_VAR_frontend_image=${{ needs.frontend.outputs.image-tag }} | |
terraform apply -auto-approve -input=false | |
working-directory: ${{ env.WORKING_DIRECTORY }} |