Build a CI/CD workflow with Github Actions - Sesi 2

Build a CI/CD workflow with Github Actions - Sesi 2

11 min read
1082 wordsTechnology
Github ActionsCI/CDDevOpsAutomation

github actions adalah tools yang digunakan untuk mengautomasi workflow pada repository github kita. Dengan github actions, kita bisa membuat CI/CD pipeline yang akan membantu kita dalam proses pengembangan software.

Build a CI/CD workflow with Github Actions - Sesi 2

Create Dockerfile

lihat dokumentasi Docker Images Golang untuk membuat Dockerfile.

FROM golang:alpine3.16
 
WORKDIR /app
 
COPY go.mod ./
COPY go.sum ./
RUN go mod download
 
COPY *.go ./
 
RUN go build -o /aplikasi
 
EXPOSE 8144
 
ENTRYPOINT ["/aplikasi"]

kita akan mencoba test build dan running di lokal terlebih dahulu apakah aplikasi berjalan dengan baik atau tidak.

$ go build -o aplikasi
$ ./aplikasi
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
 
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)
 
[GIN-debug] GET    /                         --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on 0.0.0.0:8144

selanjut nya build image aplikasi nya.

$ docker build . -t golang-apiserver
$ docker images
REPOSITORY                        TAG       IMAGE ID       CREATED          SIZE
golang-apiserver                  latest    3eb6a831e1da   30 seconds ago   529MB

coba test running kembali

$ docker run -itd -p 8144:8144 golang-apiserver

Create Repository Docker Hub & Access Token

Login ke Docker Hub, login atau sign up jika belum memiliki akun nya. Ketika sudah login kita buat satu repository dengan nama golang-apiserver.

untuk mengakses Docker hub dari Github Actions kita memerlukan acccess token yang di generate dalam Docker Hub.

Pilih menu Account Settings > Security > New Access Token.

Copy Token yang sudah di buat.

Create Repository Secret Github

Selanjutnya kembali ke github > select repository > settings> Secrets and variables.

pilih New repository secret

pada bagian secret paste access token yang sudah di berikan di Docker Hub. Access Token ini seperti password untuk masuk ke akun Docker Hub milik kita.

Selanjutnya create new repository secret lagi namun secret yang dari key server instance AWS EC2.

create repository secret lagi untuk ssh user.

create repository secret lagi untuk ssh ip

Setup Github Actions

Masuk ke menu Actions Pilih Go untuk memulai Github Actions. untuk mempelajari github actions workflows bisa ke dokumentasi ini Github Workflows Trigger

ada sedikit perubahan dari file go.yml

# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
 
name: Go
 
on:
  push:
    branches: [ "main" ]
 
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
 
    - name: Set up Go
      uses: actions/setup-go@v3
      with:
        go-version: 1.19
 
    - name: Test
      run: go test -v ./...

start commit

jika sudah commit bakalan ada 1 actions workflow yang berjalan, bisa di lihat ke menu Actions.

sampai sini masih sampai base bawaan next kita akan build and push container nya.


Conclusion

Pada sesi kedua ini kita sudah membuat Dockerfile untuk aplikasi golang kita, membuat repository di Docker Hub, membuat access token untuk mengakses Docker Hub dari Github Actions, dan setup Github Actions untuk build aplikasi golang kita. Selanjutnya kita akan melanjutkan ke sesi berikutnya untuk melakukan build dan push container ke Docker Hub serta deploy ke server AWS EC2.

Written by Muhamad Dani Ramanda

Published on January 29, 2023

Share this article:

© 2025. All rights reserved.

Built with Astro and Claude AI