-
Notifications
You must be signed in to change notification settings - Fork 0
89 lines (81 loc) · 3.21 KB
/
deploy-existing-release.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
name: Deploy Existing Release
on:
workflow_dispatch:
# Ensure only one instance of either this or the publish workflow is running at a time
# This ensures that we don't put production into an inconsistent state
concurrency:
group: 'prod-deployment'
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
packages: write
contents: write # Write is required to create/update releases
steps:
- name: Ensure pre-requisites for deployment are met
id: validate_deployment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
/*
* We load 20 releases here, it's unlikely that we will want to roll back
* to a release older than that.
*/
const PAGE_SIZE = 20;
const releases = await github.rest.repos.listReleases({
owner,
repo,
per_page: PAGE_SIZE,
});
/*
* Pre-releases should not be considered for deployment here, the `publish-new-release` action
* should be used instead.
*/
const availableReleases = releases.data.filter((release) => !release.prerelease);
const targetRelease = availableReleases.find(
(release) => release.tag_name === "${{github.ref_name}}"
);
if (!targetRelease) {
throw new Error(`No recent release found for tag: ${{github.ref_name}}`);
}
console.log(
`Found release ${targetRelease.id}, proceeding with deployment: ${targetRelease.html_url}`
);
return targetRelease.id;
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy to Production
# To emulate deployments here we are simply shifting the latest tag to the appropriate docker image.
# In a real world scenario, you would replace this with your actual deployment steps.
run: |
docker pull ghcr.io/${{ github.repository }}:${{github.ref_name}}
docker tag ghcr.io/${{ github.repository }}:${{github.ref_name}} ghcr.io/${{ github.repository }}:latest
docker push ghcr.io/${{ github.repository }}:latest
- name: Update latest pointer
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
const previousLatestRelease = await github.rest.repos.getLatestRelease({
owner,
repo,
});
await github.rest.repos.updateRelease({
owner,
repo,
release_id: previousLatestRelease.data.id,
make_latest: false
});
await github.rest.repos.updateRelease({
owner,
repo,
release_id: ${{ steps.validate_deployment.outputs.result }},
make_latest: true
});