1name: Alchemy
2description: Deploy Alchemy stages for pull requests and the main branch, and destroy pull request stages when closed.
3author: alchemy-run
4branding:
5 icon: upload-cloud
6 color: green
7inputs:
8 working-directory:
9 description: Directory where the Alchemy project should be run.
10 required: false
11 default: .
12 main-branch:
13 description: Branch that deploys as the persistent prod stage and is never destroyed.
14 required: false
15 default: main
16outputs:
17 action:
18 description: "The lifecycle action that ran: deploy, destroy, or skip."
19 value: ${{ steps.resolve.outputs.action }}
20 stage:
21 description: The Alchemy stage name.
22 value: ${{ steps.resolve.outputs.stage }}
23 pull-request:
24 description: The pull request number for PR stages.
25 value: ${{ steps.resolve.outputs.pull-request }}
26runs:
27 using: composite
28 steps:
29 - id: resolve
30 shell: bash
31 env:
32 MAIN_BRANCH: ${{ inputs.main-branch }}
33 EVENT_NAME: ${{ github.event_name }}
34 EVENT_ACTION: ${{ github.event.action }}
35 REF_NAME: ${{ github.ref_name }}
36 PR_NUMBER: ${{ github.event.number }}
37 run: |
38 if [ "$EVENT_NAME" = "pull_request" ]; then
39 echo "stage=staging-$PR_NUMBER" >> "$GITHUB_OUTPUT"
40 echo "pull-request=$PR_NUMBER" >> "$GITHUB_OUTPUT"
41
42 if [ "$EVENT_ACTION" = "closed" ]; then
43 echo "action=destroy" >> "$GITHUB_OUTPUT"
44 else
45 echo "action=deploy" >> "$GITHUB_OUTPUT"
46 fi
47 exit 0
48 fi
49
50 if [ "$REF_NAME" = "$MAIN_BRANCH" ]; then
51 echo "stage=prod" >> "$GITHUB_OUTPUT"
52 echo "pull-request=" >> "$GITHUB_OUTPUT"
53 echo "action=deploy" >> "$GITHUB_OUTPUT"
54 exit 0
55 fi
56
57 echo "stage=" >> "$GITHUB_OUTPUT"
58 echo "pull-request=" >> "$GITHUB_OUTPUT"
59 echo "action=skip" >> "$GITHUB_OUTPUT"
60
61 - if: ${{ steps.resolve.outputs.action == 'deploy' }}
62 shell: bash
63 working-directory: ${{ inputs.working-directory }}
64 env:
65 STAGE: ${{ steps.resolve.outputs.stage }}
66 PULL_REQUEST_VALUE: ${{ steps.resolve.outputs.pull-request }}
67 run: |
68 if [ -n "$PULL_REQUEST_VALUE" ]; then
69 export PULL_REQUEST="$PULL_REQUEST_VALUE"
70 fi
71 alchemy deploy --stage "$STAGE" --yes
72
73 - if: ${{ steps.resolve.outputs.action == 'destroy' }}
74 shell: bash
75 working-directory: ${{ inputs.working-directory }}
76 env:
77 STAGE: ${{ steps.resolve.outputs.stage }}
78 PULL_REQUEST_VALUE: ${{ steps.resolve.outputs.pull-request }}
79 run: |
80 if [ -n "$PULL_REQUEST_VALUE" ]; then
81 export PULL_REQUEST="$PULL_REQUEST_VALUE"
82 fi
83 alchemy destroy --stage "$STAGE" --yes