1
1
name : Release
2
+ permissions :
3
+ packages : write
4
+ contents : write
2
5
on :
6
+ # Triggered on new GitHub Release
3
7
release :
4
8
types : [published]
9
+ # Triggered on every successful Build action
10
+ workflow_run :
11
+ workflows : ["Build"]
12
+ branches : [main,master]
13
+ types :
14
+ - completed
15
+ # Manual trigger for rollback to specific release or redeploy latest
16
+ workflow_dispatch :
17
+ inputs :
18
+ version :
19
+ default : latest
20
+ description : Tag you want to release.
21
+ required : true
22
+
5
23
jobs :
6
- push_to_ecr :
7
- runs-on : ubuntu-20.04
24
+ push_to_registry :
25
+ runs-on : ubuntu-22.04
26
+ if : ${{ github.event.workflow_run.conclusion != 'failure' }}
8
27
steps :
9
- - name : Checkout
10
- uses : actions/checkout@v2
28
+ # Checkout latest or specific tag
29
+ - name : checkout
30
+ if : ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
31
+ uses : actions/checkout@v3
32
+ - name : checkout tag
33
+ if : ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }}
34
+ uses : actions/checkout@v3
35
+ with :
36
+ ref : refs/tags/${{ github.event.inputs.version }}
11
37
12
- - name : repository name fix
38
+ # Assign environment variables used in subsequent steps
39
+ - name : Env variable assignment
13
40
run : echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
41
+ # TAG_NAME defaults to 'latest' if not a release or manual deployment
42
+ - name : Assign version
43
+ run : |
44
+ echo "TAG_NAME=latest" >> $GITHUB_ENV
45
+ if [ "${{ github.event.release.tag_name }}" != "" ]; then
46
+ echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
47
+ fi;
48
+ if [ "${{ github.event.inputs.version }}" != "" ]; then
49
+ echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV
50
+ fi;
14
51
15
- - name : Configure AWS credentials
16
- uses : aws-actions/configure-aws-credentials@v1
52
+ - name : Login to GitHub Container Registry
53
+ uses : docker/login-action@v2
17
54
with :
18
- aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
19
- aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
20
- aws-region : ${{ secrets.AWS_REGION }}
21
-
22
- - name : Login to Amazon ECR
23
- id : login_ecr
24
- uses : aws-actions/amazon-ecr-login@v1
25
-
26
- - name : Create ECR repo if not exists.
27
- env :
28
- ECR_REPOSITORY : ${{ env.image_repository_name }}
29
- run : aws ecr describe-repositories --repository-names ${ECR_REPOSITORY} || aws ecr create-repository --repository-name ${ECR_REPOSITORY}
55
+ registry : ghcr.io
56
+ username : ${{ github.actor }}
57
+ password : ${{ secrets.GITHUB_TOKEN }}
30
58
31
- - name : Build and push to ECR
32
- id : push_image_to_ecr
33
- uses : docker/build-push-action@v2.2.2
59
+ # Build and push new docker image, skip for manual redeploy other than 'latest'
60
+ - name : Build and push Docker images
61
+ uses : docker/build-push-action@v3
62
+ if : ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
34
63
with :
35
64
file : Dockerfile
36
65
context : .
37
66
push : true
38
- tags : ${{ steps.login_ecr.outputs.registry }} /${{ env.image_repository_name }}:${{ github.event.release.tag_name }}
67
+ tags : ghcr.io /${{ env.image_repository_name }}:${{ env.TAG_NAME }}
39
68
40
- deploy_ecs :
41
- needs : push_to_ecr
42
- runs-on : ubuntu-20.04
69
+ deploy_via_ssh :
70
+ needs : push_to_registry
71
+ runs-on : ubuntu-22.04
72
+ if : ${{ github.event.workflow_run.conclusion != 'failure' }}
43
73
steps :
74
+ # Checkout latest or specific tag
44
75
- name : checkout
45
- uses : actions/checkout@v2
46
-
47
- - name : Configure AWS credentials
48
- uses : aws-actions/configure-aws-credentials@v1
76
+ if : ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }}
77
+ uses : actions/checkout@v3
78
+ - name : checkout tag
79
+ if : ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }}
80
+ uses : actions/checkout@v3
49
81
with :
50
- aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
51
- aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
52
- aws-region : ${{ secrets.AWS_REGION }}
53
-
54
- - name : Login to Amazon ECR
55
- id : login_ecr
56
- uses : aws-actions/amazon-ecr-login@v1
82
+ ref : refs/tags/${{ github.event.inputs.version }}
57
83
58
- - name : Repository name fix and env values setup
84
+ - name : repository name fix and env
59
85
run : |
60
86
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
61
- echo "domain=${{ secrets.HOST_DOMAIN }}" >> $GITHUB_ENV
87
+ echo "domain=${{ secrets.DEPLOY_HOST }}" >> $GITHUB_ENV
62
88
echo "letsencrypt_email=${{ secrets.LETSENCRYPT_EMAIL }}" >> $GITHUB_ENV
63
- echo "app_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]' | cut -d'/' -f2)" >> $GITHUB_ENV
64
- echo "cluster_name=${{ secrets.AWS_ECS_CLUSTER }}" >> $GITHUB_ENV
65
- echo "image_url=${{ steps.login_ecr.outputs.registry }}/$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]'):${{ github.event.release.tag_name }}" >> $GITHUB_ENV
66
- echo "aws_region=${{ secrets.AWS_REGION }}" >> $GITHUB_ENV
89
+ echo "TAG_NAME=latest" >> $GITHUB_ENV
90
+ if [ "${{ github.event.release.tag_name }}" != "" ]; then
91
+ echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
92
+ fi;
93
+ if [ "${{ github.event.inputs.version }}" != "" ]; then
94
+ echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV
95
+ fi;
67
96
68
- - name : Populate task definition template
97
+ # Populate docker-compose.yml with variables from build process, including TAG_NAME.
98
+ - name : docker-compose file prep
69
99
uses : danielr1996/envsubst-action@1.0.0
70
100
env :
71
- RELEASE_VERSION : ${{ github.event.release.tag_name }}
72
- APP_NAME : ${{ env.app_name }}
73
- IMAGE_URL : ${{ env.image_url }}
101
+ RELEASE_VERSION : ${{ env.TAG_NAME }}
102
+ IMAGE_REPO : ${{ env.image_repository_name }}
103
+ APP_NAME : ${{ github.event.repository.name }}
74
104
HOST_DOMAIN : ${{ env.domain }}
75
105
LETSENCRYPT_EMAIL : ${{ env.letsencrypt_email }}
76
- AWS_REGION : ${{ env.aws_region }}
77
- CLUSTER_NAME : ${{ env.cluster_name }}
78
106
with :
79
- input : deploy/task-definition -template.json
80
- output : deploy/task-definition.json
107
+ input : . deploy/docker-compose -template.yml
108
+ output : . deploy/${{ github.event.repository.name }}-docker-compose.yml
81
109
82
- - name : Create task definition if doesn't exist
83
- run : aws ecs describe-task-definition --task-definition ${{ env.app_name }} || aws ecs register-task-definition --cli-input-json file://deploy/task-definition.json
84
-
85
- - name : Create ECS Service if not exists.
86
- run : aws ecs describe-services --cluster ${{ env.cluster_name }} --services ${{ env.app_name }} | jq '.services[0]' -e || aws ecs create-service --cluster ${{ env.cluster_name }} --service-name ${{ env.app_name }} --task-definition ${{ env.app_name }} --desired-count 1
110
+ # Copy only the docker-compose.yml to remote server home folder
111
+ - name : copy compose file via scp
112
+ uses : appleboy/scp-action@v0.1.3
113
+ with :
114
+ host : ${{ secrets.DEPLOY_HOST }}
115
+ username : ${{ secrets.DEPLOY_USERNAME }}
116
+ port : 22
117
+ key : ${{ secrets.DEPLOY_KEY }}
118
+ source : " .deploy/${{ github.event.repository.name }}-docker-compose.yml"
119
+ target : " ~/"
120
+ - name : Run remote db migrations
121
+ uses : appleboy/ssh-action@v0.1.5
122
+ env :
123
+ APPTOKEN : ${{ secrets.GITHUB_TOKEN }}
124
+ USERNAME : ${{ secrets.DEPLOY_USERNAME }}
125
+ with :
126
+ host : ${{ secrets.DEPLOY_HOST }}
127
+ username : ${{ secrets.DEPLOY_USERNAME }}
128
+ key : ${{ secrets.DEPLOY_KEY }}
129
+ port : 22
130
+ envs : APPTOKEN,USERNAME
131
+ script : |
132
+ echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
133
+ docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
134
+ docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up ${{ github.event.repository.name }}-migration
87
135
88
- - name : Deploy new revision of the task definition
89
- uses : aws-actions/amazon-ecs-deploy-task-definition@v1
136
+ # Deploy Docker image with ServiceStack application using `docker compose up` remotely
137
+ - name : remote docker-compose up via ssh
138
+ uses : appleboy/ssh-action@v0.1.5
139
+ env :
140
+ APPTOKEN : ${{ secrets.GITHUB_TOKEN }}
141
+ USERNAME : ${{ secrets.DEPLOY_USERNAME }}
90
142
with :
91
- task-definition : deploy/task-definition.json
92
- service : ${{ env.app_name }}
93
- cluster : ${{ env.cluster_name }}
94
- force-new-deployment : true
143
+ host : ${{ secrets.DEPLOY_HOST }}
144
+ username : ${{ secrets.DEPLOY_USERNAME }}
145
+ key : ${{ secrets.DEPLOY_KEY }}
146
+ port : 22
147
+ envs : APPTOKEN,USERNAME
148
+ script : |
149
+ echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin
150
+ docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull
151
+ docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up -d
0 commit comments