-
Notifications
You must be signed in to change notification settings - Fork 0
109 lines (89 loc) · 3.28 KB
/
jenkins-ecs-docker-build-terraform-apply.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
name: jenkins-ecs-docker-build-terraform-apply
env:
IMAGE_NAME: shanali38/aws-terraform-jenkins
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches:
- "main"
paths:
- "docker/*"
pull_request:
types:
- closed
branches:
- "main"
paths:
- "docker/*"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
docker-build-push:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: ./terraform/jenkins-ecs
outputs:
image_tag: ${{ steps.generate_img_tag.outputs.image_tag }}
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout Repository
uses: actions/checkout@v3
- name: Generate Image Tag
id: generate_img_tag
run: |
GITHUB_SHA_SHORT=`(echo ${GITHUB_SHA} | cut -c1-6)` >> $GITHUB_ENV
DATE=`(date +"%Y.%m.%d")` >> $GITHUB_ENV
DOCKER_IMAGE_TAG=${DATE}.${GITHUB_SHA_SHORT}
echo DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG} >> $GITHUB_ENV
echo "::set-output name=image_tag::$DOCKER_IMAGE_TAG"
- name: Print Image Tag
run: echo ${DOCKER_IMAGE_TAG}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v3
with:
file: ./docker/Dockerfile
push: true
tags: ${{ env.IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG }},${{ env.IMAGE_NAME }}:latest
terraform-apply:
needs: docker-build-push
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: ./terraform/jenkins-ecs
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout Repository
uses: actions/checkout@v3
- name: Print Image Tag From Output
run: echo ${{ needs.docker-build-push.outputs.image_tag }}
# Install the preferred version of Terraform CLI
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.2.0
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
id: init
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: terraform init
# On push to main, build or change infrastructure according to Terraform configuration files
- name: Terraform Apply
#if: github.ref == 'refs/heads/main' && github.event_name == 'push'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: terraform apply -var="docker_image=${{ env.IMAGE_NAME }}:${{ needs.docker-build-push.outputs.image_tag }}" -auto-approve