Skip to content

Commit fd4ce53

Browse files
authored
tmp: deploy on PR (#45)
* tmp: deploy on PR * Get PR associated with deployment * tmp * fix env * tmp * debug * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp * tmp: remove * tmp * tmp * revert tmp * tmp * tmp: revert dispatch
1 parent 2e15311 commit fd4ce53

File tree

5 files changed

+108
-108
lines changed

5 files changed

+108
-108
lines changed

.github/actions/deploy/action.yml

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,9 @@ inputs:
77
image:
88
required: true
99
description: "Which image to deploy"
10-
version:
11-
required: true
12-
description: "Which version to deploy"
1310
fly_token:
1411
required: true
1512
description: "Access token for flyctl"
16-
slack_webhook_url:
17-
required: true
18-
description: "Webhook URL for slack"
19-
gh_token:
20-
description: 'The GitHub token to use for the API requests'
21-
required: true
2213

2314
runs:
2415
using: 'composite'
@@ -34,11 +25,7 @@ runs:
3425
fi
3526
3627
echo "path=$config_path" >> $GITHUB_OUTPUT
37-
- name: Notify (attempt)
38-
uses: ./.github/actions/slack
39-
with:
40-
message: "deploying ${{ inputs.image }} to ${{ inputs.environment }} :rocket:"
41-
webhook_url: ${{ inputs.slack_webhook_url }}
28+
4229
- name: Deploy flyctl
4330
shell: bash
4431
run: |
@@ -53,84 +40,3 @@ runs:
5340
-i ${{ inputs.image }} \
5441
--yes \
5542
--verbose
56-
57-
- name: Get input commit or latest commit
58-
id: commit
59-
shell: bash
60-
env:
61-
GH_TOKEN: ${{ inputs.gh_token }}
62-
run: |
63-
echo "url=${{ github.server_url }}/${{ github.repository }}/commits/${{ github.sha }}" >> $GITHUB_OUTPUT
64-
65-
- name: Get The PRs associated with the commit
66-
id: prs
67-
shell: bash
68-
env:
69-
GH_TOKEN: ${{ inputs.gh_token }}
70-
run: |
71-
data=$(gh api graphql \
72-
-F owner='${{ github.repository_owner }}' \
73-
-F name='${{ github.event.repository.name }}' \
74-
-F ref='${{ github.sha }}' \
75-
-F query='query($name: String!, $owner: String!, $ref: GitObjectID!) {
76-
repository(owner: $owner, name: $name) {
77-
object(oid: $ref) {
78-
...on Commit {
79-
associatedPullRequests(first: 10) {
80-
nodes {
81-
number
82-
}
83-
}
84-
}
85-
}
86-
}
87-
}')
88-
89-
prs=$(echo $data | jq -r '.data.repository.object.associatedPullRequests.nodes[].number')
90-
91-
echo "prs=$prs" >> $GITHUB_OUTPUT
92-
echo "prs $prs"
93-
echo "data $data"
94-
95-
- name: Get ther run URL
96-
id: run
97-
shell: bash
98-
run: |
99-
echo "url=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> $GITHUB_OUTPUT
100-
101-
- name: Comment PR
102-
shell: bash
103-
env:
104-
GH_TOKEN: ${{ inputs.gh_token }}
105-
run: |
106-
cat <<EOF >> comment.txt
107-
This PR was shipped to (${{ inputs.environment }})
108-
- [commit](${{ steps.commit.outputs.url }})
109-
- version: ${{ inputs.version }}
110-
- image: ${{ inputs.image }}
111-
112-
[View the run](${{ steps.run.outputs.url }})
113-
EOF
114-
115-
for pr in $(echo '${{ steps.prs.outputs.prs }}'); do
116-
gh api \
117-
--method POST \
118-
-H "Accept: application/vnd.github.v3+json" \
119-
/repos/${{ github.repository }}/pulls/$pr/reviews \
120-
-f body="$(cat comment.txt)" \
121-
-F event=COMMENT
122-
done
123-
124-
- name: Notify (scuccessful deployment)
125-
if: success()
126-
uses: ./.github/actions/slack
127-
with:
128-
message: ":tada: Successfully deployed ${{ inputs.image }} to ${{ inputs.environment }} :white_check_mark:!"
129-
webhook_url: ${{ inputs.slack_webhook_url }}
130-
- name: Notify (failed deployment)
131-
if: failure()
132-
uses: ./.github/actions/slack
133-
with:
134-
message: ":fail: Failed to deploy to ${{ inputs.environment }} :red_flag:!"
135-
webhook_url: ${{ inputs.slack_webhook_url }}
136-

.github/workflows/deploy.development.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,4 @@ jobs:
2929
with:
3030
environment: development
3131
image: ${{ steps.build.outputs.image_version }}
32-
version: ${{ steps.build.outputs.version }}
3332
fly_token: ${{ secrets.FLY_TOKEN }}
34-
slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
35-
gh_token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/deploy.production.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,4 @@ jobs:
2929
with:
3030
environment: production
3131
image: ${{ steps.build.outputs.image_version }}
32-
version: ${{ steps.build.outputs.version }}
3332
fly_token: ${{ secrets.FLY_TOKEN }}
34-
slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
35-
gh_token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/deploy.staging.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,4 @@ jobs:
2727
with:
2828
environment: staging
2929
image: ${{ steps.build.outputs.image_version }}
30-
version: ${{ steps.build.outputs.version }}
3130
fly_token: ${{ secrets.FLY_TOKEN }}
32-
slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
33-
gh_token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/deployment.yml

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,114 @@
1-
name: On Deployment
1+
name: Deployment (notifications)
22

33
on:
4-
deployment
5-
4+
deployment_status:
65
jobs:
7-
link:
6+
pre_deployment:
7+
runs-on: ubuntu-latest
8+
if: |
9+
github.event.deployment_status.state == 'pending' ||
10+
github.event.deployment_status.state == 'in_progress'
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Notify (attempt)
14+
uses: ./.github/actions/slack
15+
with:
16+
message: "deploying ${{ github.event.deployment_status.log_url }} to ${{ github.event.deployment_status.environment }}..."
17+
webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
18+
19+
post_deploment:
820
runs-on: ubuntu-latest
21+
if: |
22+
github.event.deployment_status.state == 'success' ||
23+
github.event.deployment_status.state == 'failure' ||
24+
github.event.deployment_status.state == 'error'
925
steps:
1026
- uses: actions/checkout@v2
1127
- uses: ./.github/actions/context
28+
- id: status
29+
shell: bash
30+
run: |
31+
if [[ "${{ github.event.deployment_status.state }}" == "success" ]]; then
32+
echo "status=success" >> $GITHUB_OUTPUT
33+
elif [[ "${{ github.event.deployment_status.state }}" == "failure" || "${{ github.event.deployment_status.state }}" == "error" ]]; then
34+
echo "status=failure" >> $GITHUB_OUTPUT
35+
fi
36+
- name: Get The PRs associated with the commit
37+
id: prs
38+
shell: bash
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
run: |
42+
data=$(gh api graphql \
43+
-F owner='${{ github.repository_owner }}' \
44+
-F name='${{ github.event.repository.name }}' \
45+
-F ref='${{ github.sha }}' \
46+
-F query='query($name: String!, $owner: String!, $ref: GitObjectID!) {
47+
repository(owner: $owner, name: $name) {
48+
object(oid: $ref) {
49+
...on Commit {
50+
associatedPullRequests(first: 10) {
51+
nodes {
52+
number
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}')
59+
60+
prs=$(echo $data | jq -r '.data.repository.object.associatedPullRequests.nodes[].number')
61+
62+
echo "prs=$prs" >> $GITHUB_OUTPUT
63+
echo "prs $prs"
64+
echo "data $data"
65+
66+
- name: Create Comment Text
67+
id: comment
68+
shell: bash
69+
run: |
70+
touch comment.txt
71+
72+
environment='**${{ github.event.deployment_status.environment}}**'
73+
environment_url='${{ github.event.deployment_status.environment_url }}'
74+
commit_url='${{ github.server_url}}/${{ github.repository}}/commit/${{ github.sha }}'
75+
created_at='${{ github.event.deployment_status.created_at }}'
76+
updated_at='*${{ github.event.deployment_status.updated_at }}*'
77+
log_url='${{ github.event.deployment_status.log_url }}'
78+
79+
if [[ "${{ steps.status.outputs.status }}" == "success" ]]; then
80+
echo ":white_check_mark: Deplyoment of <$commit_url> to ([$environment]($environment_url)) successful at $updated_at ([logs]($log_url))" >> comment.txt
81+
elif [[ "${{ steps.status.outputs.status }}" == "failure" ]]; then
82+
echo ":x: Deployment of <$commit_url> to ($environment) failed with status (${{ github.event.deployment_status.state }}) at $updated_at ([logs]($log_url))" >> comment.txt
83+
fi
84+
85+
{
86+
echo 'text<<EOF' >> $GITHUB_OUTPUT
87+
cat comment.txt >> $GITHUB_OUTPUT
88+
echo EOF
89+
} >> $GITHUB_OUTPUT
90+
91+
- name: Comment PR
92+
shell: bash
93+
if: ${{ steps.comment.outputs.text != '' }}
94+
env:
95+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
96+
run: |
97+
if [[ -f comment.txt ]]; then
98+
for pr in $(echo '${{ steps.prs.outputs.prs }}'); do
99+
gh api \
100+
--method POST \
101+
-H "Accept: application/vnd.github.v3+json" \
102+
/repos/${{ github.repository }}/pulls/$pr/reviews \
103+
-f body='${{ steps.comment.outputs.text}}' \
104+
-F event=COMMENT
105+
done
106+
fi
107+
108+
- name: Notify in Slack
109+
uses: ./.github/actions/slack
110+
with:
111+
message: '${{ steps.comment.outputs.text }}'
112+
webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
113+
114+

0 commit comments

Comments
 (0)