|
| 1 | +name: Apply |
| 2 | + |
| 3 | +# Only one workflow can run at a time |
| 4 | +# If there is newer workflow in progress, cancel older ones |
| 5 | +concurrency: |
| 6 | + group: apply |
| 7 | + cancel-in-progress: true |
| 8 | + |
| 9 | +# Put 'on' in quotes to avoid YAML parsing error |
| 10 | +"on": |
| 11 | + # Enable manual triggering |
| 12 | + workflow_dispatch: {} |
| 13 | + # Run on commits to main branch |
| 14 | + push: |
| 15 | + branches: |
| 16 | + - main |
| 17 | + # Run only on changes to relevant files |
| 18 | + paths: |
| 19 | + - .github/workflows/apply.yaml |
| 20 | + - src/** |
| 21 | + - .sops.yaml |
| 22 | + - flake.lock |
| 23 | + - "*.nix" |
| 24 | + - Taskfile.dist.yaml |
| 25 | + |
| 26 | +jobs: |
| 27 | + apply: |
| 28 | + name: Apply |
| 29 | + # Pin version of Ubuntu to avoid breaking changes |
| 30 | + runs-on: ubuntu-24.04 |
| 31 | + # Use reasonable timeout to avoid stuck workflows |
| 32 | + timeout-minutes: 10 |
| 33 | + # Use main environment |
| 34 | + environment: |
| 35 | + name: main |
| 36 | + env: |
| 37 | + NIX_CACHE_DIR: /home/runner/.nixcache/ |
| 38 | + TERRAFORM_BACKEND_CONFIG: /home/runner/config.tfbackend |
| 39 | + TERRAFORM_CACHE_DIR: /home/runner/.terraformcache/ |
| 40 | + permissions: |
| 41 | + # Needed to checkout code |
| 42 | + contents: read |
| 43 | + steps: |
| 44 | + - name: Checkout code |
| 45 | + |
| 46 | + - name: Setup Nix cache |
| 47 | + |
| 48 | + id: cache-nix |
| 49 | + with: |
| 50 | + path: ${{ env.NIX_CACHE_DIR }} |
| 51 | + key: apply-nix |
| 52 | + - name: Setup Terraform cache |
| 53 | + |
| 54 | + id: cache-terraform |
| 55 | + with: |
| 56 | + path: ${{ env.TERRAFORM_CACHE_DIR }} |
| 57 | + key: apply-terraform |
| 58 | + # Create Terraform cache directory if not imported from cache |
| 59 | + - name: Create Terraform cache directory |
| 60 | + if: steps.cache-terraform.outputs.cache-hit != 'true' |
| 61 | + run: >- |
| 62 | + mkdir |
| 63 | + --parents |
| 64 | + ${{ env.TERRAFORM_CACHE_DIR }} |
| 65 | + - name: Install Nix |
| 66 | + |
| 67 | + with: |
| 68 | + github_access_token: ${{ github.token }} |
| 69 | + install_url: https://releases.nixos.org/nix/nix-2.28.5/install |
| 70 | + # See: https://github.com/cachix/install-nix-action/issues/56 |
| 71 | + - name: Import Nix store cache |
| 72 | + if: steps.cache-nix.outputs.cache-hit == 'true' |
| 73 | + run: >- |
| 74 | + nix-store |
| 75 | + --import |
| 76 | + < ${{ env.NIX_CACHE_DIR }}/archive.nar |
| 77 | + - name: Create backend configuration |
| 78 | + run: |- |
| 79 | + cat <<EOF > ${{ env.TERRAFORM_BACKEND_CONFIG }} |
| 80 | + EOF |
| 81 | + - name: Initialize |
| 82 | + env: |
| 83 | + SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }} |
| 84 | + TF_PLUGIN_CACHE_DIR: ${{ env.TERRAFORM_CACHE_DIR }} |
| 85 | + run: >- |
| 86 | + nix |
| 87 | + develop |
| 88 | + ./#terraform |
| 89 | + --command |
| 90 | + -- |
| 91 | + task |
| 92 | + init |
| 93 | + -- |
| 94 | + -input=false |
| 95 | + -backend-config=${{ env.TERRAFORM_BACKEND_CONFIG }} |
| 96 | + - name: Apply |
| 97 | + env: |
| 98 | + SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }} |
| 99 | + TF_PLUGIN_CACHE_DIR: ${{ env.TERRAFORM_CACHE_DIR }} |
| 100 | + run: >- |
| 101 | + nix |
| 102 | + develop |
| 103 | + ./#terraform |
| 104 | + --command |
| 105 | + -- |
| 106 | + task |
| 107 | + apply |
| 108 | + -- |
| 109 | + -input=false |
| 110 | + -auto-approve |
| 111 | + # See: https://github.com/cachix/install-nix-action/issues/56 |
| 112 | + - name: Export Nix store cache |
| 113 | + if: "!cancelled()" |
| 114 | + run: >- |
| 115 | + mkdir |
| 116 | + --parents |
| 117 | + ${{ env.NIX_CACHE_DIR }} |
| 118 | + && |
| 119 | + nix-store |
| 120 | + --export $(find /nix/store/ -maxdepth 1 -name '*-*') |
| 121 | + > ${{ env.NIX_CACHE_DIR }}/archive.nar |
0 commit comments