build before apply action (#222) #43
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 Apply' | |
on: | |
push: | |
branches: [ main ] | |
env: | |
USERNAME: justingebert | |
IMAGE_NAME: hypnobuddy-backend | |
jobs: | |
docker: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Determine tag | |
id: prep | |
run: | | |
if [[ $GITHUB_EVENT_NAME == 'pull_request' ]]; then | |
echo ::set-output name=tag::pr-${{ github.event.number }} | |
else | |
echo ::set-output name=tag::latest | |
fi | |
- name: Build and push | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./Backend | |
push: true | |
tags: ${{ env.USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.prep.outputs.tag }} | |
terraform-apply: | |
name: 'Terraform Apply' | |
runs-on: ubuntu-latest | |
steps: | |
- name: 'Checkout' | |
uses: actions/checkout@v4 | |
- name: 'Setup Terraform' | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} | |
- name: 'Terraform Init' | |
run: terraform init | |
working-directory: ./Backend/terraform | |
- name: 'Terraform Delete (this should be removed later -> get last tag from docker gh action)' | |
run: terraform destroy -auto-approve | |
working-directory: ./Backend/terraform | |
env: | |
TF_VAR_gcp_credentials: ${{ secrets.GCP_CREDENTIALS_JSON }} | |
- name: 'Terraform Apply' | |
run: terraform apply -auto-approve | |
working-directory: ./Backend/terraform | |
env: | |
TF_VAR_gcp_credentials: ${{ secrets.GCP_CREDENTIALS_JSON }} |