4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

githubでtagつけたら、自動でDocker buildしてAWS ECRにpushするgithub actions

Last updated at Posted at 2020-03-24

初版20200325

php+nginxアプリケーションの場合なので2つのコンテナをpushしている。

name: Build and Push Image to ECR

# 条件
on:
  push:
    tags: 
      - v* # tagが"v"からはじまってる場合に絞ってる。

# 処理
jobs:
  build:
    name: Build and Push
    runs-on: ubuntu-latest
    steps:
      # tag名を変数化
      - name: Get the version (tag)
        id: get_version
        run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)
      # 指定タグでチェックアウト
      - uses: actions/checkout@v1
        with:
          ref: refs/tags/${{ steps.get_version.outputs.VERSION }}
          token: ${{ secrets.GitHub_PAT }}
      # AWS credentials設定
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-northeast-1
      # ECRログイン
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
      # ビルド, push処理
      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPO_APP: ${{ secrets.AWS_ECR_REPO_APP }}
          ECR_REPO_NGINX: ${{ secrets.AWS_ECR_REPO_NGINX }}
          XGITHUB_USER: ${{ secrets.XGITHUB_USER }}
          XGITHUB_PASS: ${{ secrets.XGITHUB_PASS }}
          XGITHUB_TAG: ${{ steps.get_version.outputs.VERSION }}
        run: |
          # 必要に応じディレクトリ移動してビルド
          cd docker
          docker-compose build

          # Tagging app & push
          docker tag $ECR_REPO_APP:latest $ECR_REPO_APP:$XGITHUB_TAG
          docker tag $ECR_REPO_APP:$XGITHUB_TAG $ECR_REGISTRY/$ECR_REPO_APP:$XGITHUB_TAG
          docker tag $ECR_REPO_APP:latest $ECR_REGISTRY/$ECR_REPO_APP:latest
          echo "Tagged ... ${ECR_REGISTRY}/${ECR_REPO_APP}:${XGITHUB_TAG}"
          docker push $ECR_REGISTRY/$ECR_REPO_APP:$XGITHUB_TAG
          docker push $ECR_REGISTRY/$ECR_REPO_APP:latest
          echo "Pushed ... ${ECR_REGISTRY}/${ECR_REPO_APP}:${XGITHUB_TAG}"

          # Tagging nginx & push(上記と同じ処理をrepositry変えて繰り返してるだけ)
          docker tag $ECR_REPO_NGINX:latest $ECR_REPO_NGINX:$XGITHUB_TAG
          docker tag $ECR_REPO_NGINX:$XGITHUB_TAG $ECR_REGISTRY/$ECR_REPO_NGINX:$XGITHUB_TAG
          docker tag $ECR_REPO_NGINX:latest $ECR_REGISTRY/$ECR_REPO_NGINX:latest
          echo "Tagged ... ${ECR_REGISTRY}/${ECR_REPO_NGINX}:${XGITHUB_TAG}"
          docker push $ECR_REGISTRY/$ECR_REPO_NGINX:$XGITHUB_TAG
          docker push $ECR_REGISTRY/$ECR_REPO_NGINX:latest
          echo "Pushed ... ${ECR_REGISTRY}/${ECR_REPO_NGINX}:${XGITHUB_TAG}"
      # Slack通知とか必要に応じて
#     - name: Notify push finish to Slack
#       run: curl -X POST -H 'Content-type:application/json' --data '{"text":"'"${GITHUB_REPOSITORY}"'\n【FINISH】Pushed Image to ECR!!"}' ${{ secrets.SLACK_WEBHOOK }}
  • secrets はgithub repositryのSettings > Secretsから便宜設定
    • AWS_ACCESS_KEY_ID
    • AWS_ECR_REPO_APP
    • AWS_ECR_REPO_NGINX
    • AWS_SECRET_ACCESS_KEY
    • XGITHUB_PASS
    • XGITHUB_USER
  • 面倒なのでとりあえずgithub user/passを指定しちゃってるが実際は鍵認証などでやるべき(secrets.GitHub_PATでできそうな感じする)
  • 他ビルドに必要なenvがあればsecretsに設定する
4
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?